annotate Python/bltinmodule.c @ 1354:fccd415e2eb8 legacy-trunk

* bltinmodule.c: removed exec() built-in function. * Grammar: add exec statement; allow testlist in expr statement. * ceval.c, compile.c, opcode.h: support exec statement; avoid optimizing locals when it is used * fileobject.{c,h}: add getfilename() internal function.
author Guido van Rossum <guido@python.org>
date Mon, 18 Oct 1993 17:06:59 +0000
parents 6ee380349c84
children 1808ca5d8883
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
1 /***********************************************************
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1215
diff changeset
3 Amsterdam, The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 139
diff changeset
24
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Built-in functions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 #include "allobjects.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #include "node.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #include "graminit.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #include "sysmodule.h"
119
9fee1ca6c8fe File name shortening.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
32 #include "bltinmodule.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #include "import.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "pythonrun.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include "ceval.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #include "modsupport.h"
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
37 #include "compile.h"
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
38 #include "eval.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 builtin_abs(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 {
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
45 number_methods *nm;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
46 if (v == NULL || (nm = v->ob_type->tp_as_number) == NULL) {
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
47 err_setstr(TypeError, "abs() requires numeric argument");
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
48 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
50 return (*nm->nb_absolute)(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
54 builtin_apply(self, args)
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
55 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
56 object *args;
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
57 {
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
58 object *func, *arglist;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
59 if (!getargs(args, "(OO)", &func, &arglist))
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
60 return NULL;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
61 return call_object(func, arglist);
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
62 }
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
63
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
64 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
65 builtin_chr(self, args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
67 object *args;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 long x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 char s[1];
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
71 if (!getargs(args, "l", &x))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 if (x < 0 || x >= 256) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
74 err_setstr(ValueError, "chr() arg not in range(256)");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 s[0] = x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 return newsizedstringobject(s, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 static object *
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
82 builtin_cmp(self, args)
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
83 object *self;
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
84 object *args;
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
85 {
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
86 object *a, *b;
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
87 if (!getargs(args, "(OO)", &a, &b))
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
88 return NULL;
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
89 return newintobject((long)cmpobject(a, b));
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
90 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
91
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
92 static object *
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
93 builtin_coerce(self, args)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
94 object *self;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
95 object *args;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
96 {
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
97 object *v, *w;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
98 object *res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
99
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
100 if (!getargs(args, "(OO)", &v, &w))
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
101 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
102 if (coerce(&v, &w) < 0)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
103 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
104 res = mkvalue("(OO)", v, w);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
105 DECREF(v);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
106 DECREF(w);
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
107 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
108 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
109
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
110 static object *
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
111 builtin_compile(self, args)
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
112 object *self;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
113 object *args;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
114 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
115 char *str;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
116 char *filename;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
117 char *startstr;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
118 int start;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
119 if (!getargs(args, "(sss)", &str, &filename, &startstr))
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
120 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
121 if (strcmp(startstr, "exec") == 0)
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
122 start = file_input;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
123 else if (strcmp(startstr, "eval") == 0)
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
124 start = eval_input;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
125 else {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
126 err_setstr(ValueError,
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
127 "compile() mode must be 'exec' or 'eval'");
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
128 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
129 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
130 return compile_string(str, filename, start);
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
131 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
132
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
133 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 builtin_dir(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 object *d;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 d = getlocals();
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
141 INCREF(d);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 else {
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
144 d = getattr(v, "__dict__");
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
145 if (d == NULL) {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 err_setstr(TypeError,
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
147 "dir() argument must have __dict__ attribute");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
150 }
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
151 if (is_dictobject(d)) {
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
152 v = getdictkeys(d);
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
153 if (sortlist(v) != 0) {
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
154 DECREF(v);
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
155 v = NULL;
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
156 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 }
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
158 else {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
159 v = newlistobject(0);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
160 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
161 DECREF(d);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
164
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 static object *
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
166 builtin_divmod(self, args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 object *self;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
168 object *args;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 {
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
170 object *v, *w, *x;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
171 if (!getargs(args, "(OO)", &v, &w))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 return NULL;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
173 if (v->ob_type->tp_as_number == NULL ||
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
174 w->ob_type->tp_as_number == NULL) {
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
175 err_setstr(TypeError, "divmod() requires numeric arguments");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
178 if (coerce(&v, &w) != 0)
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
179 return NULL;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
180 x = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
181 DECREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
182 DECREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
183 return x;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 exec_eval(v, start)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 int start;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 object *str = NULL, *globals = NULL, *locals = NULL;
694
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
192 char *s;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 int n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 if (v != NULL) {
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
195 if (is_tupleobject(v) &&
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 ((n = gettuplesize(v)) == 2 || n == 3)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 str = gettupleitem(v, 0);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 globals = gettupleitem(v, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if (n == 3)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 locals = gettupleitem(v, 2);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 }
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
202 else
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
203 str = v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 }
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
205 if (str == NULL || (!is_stringobject(str) && !is_codeobject(str)) ||
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 globals != NULL && !is_dictobject(globals) ||
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 locals != NULL && !is_dictobject(locals)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 err_setstr(TypeError,
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
209 "exec/eval arguments must be (string|code)[,dict[,dict]]");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 }
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
212 if (is_codeobject(str))
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
213 return eval_code((codeobject *) str, globals, locals,
1289
4d292c7857d4 Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents: 1288
diff changeset
214 (object *)NULL, (object *)NULL);
694
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
215 s = getstringvalue(str);
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
216 if (strlen(s) != getstringsize(str)) {
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
217 err_setstr(ValueError, "embedded '\\0' in string arg");
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
218 return NULL;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
219 }
694
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
220 if (start == eval_input) {
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
221 while (*s == ' ' || *s == '\t')
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
222 s++;
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
223 }
5c18d6c57827 Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents: 673
diff changeset
224 return run_string(s, start, globals, locals);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 builtin_eval(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 return exec_eval(v, eval_input);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
235 static object *
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
236 builtin_execfile(self, v)
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
237 object *self;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
238 object *v;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
239 {
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
240 object *str = NULL, *globals = NULL, *locals = NULL, *w;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
241 FILE* fp;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
242 char *s;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
243 int n;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
244 if (v != NULL) {
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
245 if (is_stringobject(v))
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
246 str = v;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
247 else if (is_tupleobject(v) &&
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
248 ((n = gettuplesize(v)) == 2 || n == 3)) {
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
249 str = gettupleitem(v, 0);
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
250 globals = gettupleitem(v, 1);
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
251 if (n == 3)
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
252 locals = gettupleitem(v, 2);
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
253 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
254 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
255 if (str == NULL || !is_stringobject(str) ||
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
256 globals != NULL && !is_dictobject(globals) ||
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
257 locals != NULL && !is_dictobject(locals)) {
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
258 err_setstr(TypeError,
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
259 "execfile arguments must be filename[,dict[,dict]]");
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
260 return NULL;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
261 }
972
7b37b5e71493 fix *serious* (new) bug in evalfile: forgot to initialize s!!!
Guido van Rossum <guido@python.org>
parents: 959
diff changeset
262 s = getstringvalue(str);
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
263 if (strlen(s) != getstringsize(str)) {
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
264 err_setstr(ValueError, "embedded '\\0' in string arg");
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
265 return NULL;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
266 }
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
267 BGN_SAVE
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
268 fp = fopen(s, "r");
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
269 END_SAVE
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
270 if (fp == NULL) {
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
271 err_setstr(IOError, "execfile cannot open the file argument");
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
272 return NULL;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
273 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
274 w = run_file(fp, getstringvalue(str), file_input, globals, locals);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
275 BGN_SAVE
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
276 fclose(fp);
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
277 END_SAVE
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
278 return w;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
279 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
280
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
281 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 builtin_float(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 {
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
286 number_methods *nb;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
287
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
288 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
289 nb->nb_float == NULL) {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
290 err_setstr(TypeError,
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
291 "float() argument can't be converted to float");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
292 return NULL;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
293 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
294 return (*nb->nb_float)(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
296
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
298 builtin_getattr(self, args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
299 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
300 object *args;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
301 {
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
302 object *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
303 object *name;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
304 if (!getargs(args, "(OS)", &v, &name))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
305 return NULL;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
306 return getattro(v, name);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
307 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
308
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
309 static object *
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
310 builtin_hasattr(self, args)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
311 object *self;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
312 object *args;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
313 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
314 object *v;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
315 object *name;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
316 if (!getargs(args, "(OS)", &v, &name))
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
317 return NULL;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
318 v = getattro(v, name);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
319 if (v == NULL) {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
320 err_clear();
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
321 return newintobject(0L);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
322 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
323 DECREF(v);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
324 return newintobject(1L);
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
325 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
326
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
327 static object *
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
328 builtin_id(self, args)
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
329 object *self;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
330 object *args;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
331 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
332 object *v;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
333 if (!getargs(args, "O", &v))
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
334 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
335 return newintobject((long)v);
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
336 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
337
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
338 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
339 builtin_setattr(self, args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
340 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
341 object *args;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
342 {
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
343 object *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
344 object *name;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
345 object *value;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
346 if (!getargs(args, "(OSO)", &v, &name, &value))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
347 return NULL;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
348 if (setattro(v, name, value) != 0)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
349 return NULL;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
350 INCREF(None);
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
351 return None;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
352 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
353
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
354 static object *
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
355 builtin_hash(self, args)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
356 object *self;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
357 object *args;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
358 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
359 object *v;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
360 long x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
361 if (!getargs(args, "O", &v))
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
362 return NULL;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
363 x = hashobject(v);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
364 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
365 return NULL;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
366 return newintobject(x);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
367 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
368
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
369 static object *
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
370 builtin_hex(self, v)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
371 object *self;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
372 object *v;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
373 {
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
374 number_methods *nb;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
375
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
376 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
377 nb->nb_hex == NULL) {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
378 err_setstr(TypeError,
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
379 "hex() argument can't be converted to hex");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
380 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
381 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
382 return (*nb->nb_hex)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
383 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
384
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
385 static object *builtin_raw_input PROTO((object *, object *));
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
386
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
387 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 builtin_input(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 {
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
392 object *line = builtin_raw_input(self, v);
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
393 if (line == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
394 return line;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
395 v = exec_eval(line, eval_input);
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
396 DECREF(line);
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
397 return v;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 builtin_int(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 {
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
405 number_methods *nb;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
406
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
407 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
408 nb->nb_int == NULL) {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
409 err_setstr(TypeError,
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
410 "int() argument can't be converted to int");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
411 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
413 return (*nb->nb_int)(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
415
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 builtin_len(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 long len;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 typeobject *tp;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 err_setstr(TypeError, "len() without argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 tp = v->ob_type;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 if (tp->tp_as_sequence != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 len = (*tp->tp_as_sequence->sq_length)(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
431 else if (tp->tp_as_mapping != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 len = (*tp->tp_as_mapping->mp_length)(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 err_setstr(TypeError, "len() of unsized object");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 }
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
438 if (len < 0)
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
439 return NULL;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
440 else
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
441 return newintobject(len);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
443
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 static object *
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
445 builtin_long(self, v)
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
446 object *self;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
447 object *v;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
448 {
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
449 number_methods *nb;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
450
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
451 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
452 nb->nb_long == NULL) {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
453 err_setstr(TypeError,
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
454 "long() argument can't be converted to long");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
455 return NULL;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
456 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
457 return (*nb->nb_long)(v);
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
458 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
459
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
460 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 min_max(v, sign)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 int sign;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 int i, n, cmp;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
466 object *w, *x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 sequence_methods *sq;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 err_setstr(TypeError, "min() or max() without argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 sq = v->ob_type->tp_as_sequence;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 if (sq == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 err_setstr(TypeError, "min() or max() of non-sequence");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 n = (*sq->sq_length)(v);
1096
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
478 if (n < 0)
ce718457b172 * classobject.c: in instance_lenth, test result of call_object
Guido van Rossum <guido@python.org>
parents: 1094
diff changeset
479 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 if (n == 0) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
481 err_setstr(ValueError, "min() or max() of empty sequence");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 w = (*sq->sq_item)(v, 0); /* Implies INCREF */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 for (i = 1; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 x = (*sq->sq_item)(v, i); /* Implies INCREF */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 cmp = cmpobject(x, w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 if (cmp * sign > 0) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 DECREF(w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 w = x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 DECREF(x);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
494 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 return w;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
497
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 builtin_min(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 return min_max(v, -1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
505
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 builtin_max(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 return min_max(v, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
513
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 static object *
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
515 builtin_oct(self, v)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
516 object *self;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
517 object *v;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
518 {
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
519 number_methods *nb;
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
520
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
521 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
522 nb->nb_oct == NULL) {
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
523 err_setstr(TypeError,
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
524 "oct() argument can't be converted to oct");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
525 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
526 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
527 return (*nb->nb_oct)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
528 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
529
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
530 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
531 builtin_open(self, args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
533 object *args;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 {
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
535 char *name, *mode;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
536 if (!getargs(args, "(ss)", &name, &mode))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 return NULL;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
538 return newfileobject(name, mode);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
540
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 static object *
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
542 builtin_ord(self, args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 object *self;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
544 object *args;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 {
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
546 char *s;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
547 int len;
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
548 if (!getargs(args, "s#", &s, &len))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 return NULL;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
550 if (len != 1) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
551 err_setstr(ValueError, "ord() arg must have length 1");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 }
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
554 return newintobject((long)(s[0] & 0xff));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
556
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 static object *
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
558 builtin_pow(self, args)
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
559 object *self;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
560 object *args;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
561 {
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
562 object *v, *w, *x;
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
563 if (!getargs(args, "(OO)", &v, &w))
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
564 return NULL;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
565 if (v->ob_type->tp_as_number == NULL ||
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
566 w->ob_type->tp_as_number == NULL) {
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
567 err_setstr(TypeError, "pow() requires numeric arguments");
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
568 return NULL;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
569 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
570 if (coerce(&v, &w) != 0)
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
571 return NULL;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
572 x = (*v->ob_type->tp_as_number->nb_power)(v, w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
573 DECREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
574 DECREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
575 return x;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
576 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
577
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
578 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 builtin_range(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 static char *errmsg = "range() requires 1-3 int arguments";
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 int i, n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 long ilow, ihigh, istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 if (v != NULL && is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 ilow = 0; ihigh = getintvalue(v); istep = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 else if (v == NULL || !is_tupleobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 n = gettuplesize(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 if (n < 1 || n > 3) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 for (i = 0; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 if (!is_intobject(gettupleitem(v, i))) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 if (n == 3) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 istep = getintvalue(gettupleitem(v, 2));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 --n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 istep = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 ihigh = getintvalue(gettupleitem(v, --n));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 if (n > 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 ilow = getintvalue(gettupleitem(v, 0));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 ilow = 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 if (istep == 0) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
618 err_setstr(ValueError, "zero step for range()");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 /* XXX ought to check overflow of subtraction */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
622 if (istep > 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 n = (ihigh - ilow + istep - 1) / istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 n = (ihigh - ilow + istep + 1) / istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 if (n < 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 n = 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 v = newlistobject(n);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 for (i = 0; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 object *w = newintobject(ilow);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 if (w == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 DECREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
635 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 setlistitem(v, i, w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 ilow += istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
642
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 builtin_raw_input(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 {
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
648 object *f = sysget("stdout");
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
649 if (f == NULL) {
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
650 err_setstr(RuntimeError, "lost sys.stdout");
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
651 return NULL;
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
652 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 flushline();
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
654 if (v != NULL) {
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
655 if (writeobject(v, f, PRINT_RAW) != 0)
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
656 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
657 }
182
59ffb6d4b34c Use fileobject's filegetline() to implement unlimited raw_input().
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
658 return filegetline(sysget("stdin"), -1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
660
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 builtin_reload(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 return reload_module(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
668
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 static object *
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
670 builtin_repr(self, v)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
671 object *self;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
672 object *v;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
673 {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
674 if (v == NULL) {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
675 err_badarg();
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
676 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
677 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
678 return reprobject(v);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
679 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
680
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
681 static object *
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
682 builtin_round(self, args)
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
683 object *self;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
684 object *args;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
685 {
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
686 extern double floor PROTO((double));
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
687 extern double ceil PROTO((double));
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
688 double x;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
689 double f;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
690 int ndigits = 0;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
691 int sign = 1;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
692 int i;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
693 if (!getargs(args, "d", &x)) {
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
694 err_clear();
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
695 if (!getargs(args, "(di)", &x, &ndigits))
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
696 return NULL;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
697 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
698 f = 1.0;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
699 for (i = ndigits; --i >= 0; )
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
700 f = f*10.0;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
701 for (i = ndigits; ++i <= 0; )
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
702 f = f*0.1;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
703 if (x >= 0.0)
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
704 return newfloatobject(floor(x*f + 0.5) / f);
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
705 else
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
706 return newfloatobject(ceil(x*f - 0.5) / f);
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
707 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
708
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
709 static object *
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
710 builtin_str(self, v)
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
711 object *self;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
712 object *v;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
713 {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
714 if (v == NULL) {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
715 err_badarg();
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
716 return NULL;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
717 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
718 if (is_stringobject(v)) {
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
719 INCREF(v);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
720 return v;
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
721 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
722 else
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
723 return reprobject(v);
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
724 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
725
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
726 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 builtin_type(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 if (v == NULL) {
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
732 err_setstr(TypeError, "type() requires an argument");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
735 v = (object *)v->ob_type;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 INCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
739
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 static struct methodlist builtin_methods[] = {
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
741 {"abs", builtin_abs},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
742 {"apply", builtin_apply},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
743 {"chr", builtin_chr},
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
744 {"cmp", builtin_cmp},
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
745 {"coerce", builtin_coerce},
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
746 {"compile", builtin_compile},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
747 {"dir", builtin_dir},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
748 {"divmod", builtin_divmod},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
749 {"eval", builtin_eval},
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
750 {"execfile", builtin_execfile},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
751 {"float", builtin_float},
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
752 {"getattr", builtin_getattr},
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
753 {"hasattr", builtin_hasattr},
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
754 {"hash", builtin_hash},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
755 {"hex", builtin_hex},
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
756 {"id", builtin_id},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
757 {"input", builtin_input},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
758 {"int", builtin_int},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
759 {"len", builtin_len},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
760 {"long", builtin_long},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
761 {"max", builtin_max},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
762 {"min", builtin_min},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
763 {"oct", builtin_oct},
662
aaff650d7e13 Get rid of comment about open().
Guido van Rossum <guido@python.org>
parents: 644
diff changeset
764 {"open", builtin_open},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
765 {"ord", builtin_ord},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
766 {"pow", builtin_pow},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
767 {"range", builtin_range},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
768 {"raw_input", builtin_raw_input},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
769 {"reload", builtin_reload},
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
770 {"repr", builtin_repr},
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
771 {"round", builtin_round},
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
772 {"setattr", builtin_setattr},
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
773 {"str", builtin_str},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
774 {"type", builtin_type},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
775 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
778 static object *builtin_dict;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
779
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 getbuiltin(name)
332
be3052a77b7f Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents: 301
diff changeset
782 object *name;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
783 {
332
be3052a77b7f Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents: 301
diff changeset
784 return dict2lookup(builtin_dict, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
786
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 /* Predefined exceptions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
788
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1263
diff changeset
789 object *AccessError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
790 object *AttributeError;
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1263
diff changeset
791 object *ConflictError;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 object *EOFError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
793 object *IOError;
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
794 object *ImportError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
795 object *IndexError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
796 object *KeyError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
797 object *KeyboardInterrupt;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 object *MemoryError;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 object *NameError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
800 object *OverflowError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
801 object *RuntimeError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
802 object *SyntaxError;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
803 object *SystemError;
545
9f06c6689f0d Added SystemExit.
Guido van Rossum <guido@python.org>
parents: 505
diff changeset
804 object *SystemExit;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
805 object *TypeError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
806 object *ValueError;
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
807 object *ZeroDivisionError;
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
808
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 static object *
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
810 newstdexception(name)
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
811 char *name;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
812 {
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
813 object *v = newstringobject(name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 fatal("no mem for new standard exception");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
817 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
818
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 static void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 initerrors()
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1263
diff changeset
822 AccessError = newstdexception("AccessError");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
823 AttributeError = newstdexception("AttributeError");
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1263
diff changeset
824 ConflictError = newstdexception("ConflictError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
825 EOFError = newstdexception("EOFError");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
826 IOError = newstdexception("IOError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
827 ImportError = newstdexception("ImportError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
828 IndexError = newstdexception("IndexError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
829 KeyError = newstdexception("KeyError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
830 KeyboardInterrupt = newstdexception("KeyboardInterrupt");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
831 MemoryError = newstdexception("MemoryError");
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
832 NameError = newstdexception("NameError");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
833 OverflowError = newstdexception("OverflowError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
834 RuntimeError = newstdexception("RuntimeError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
835 SyntaxError = newstdexception("SyntaxError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
836 SystemError = newstdexception("SystemError");
545
9f06c6689f0d Added SystemExit.
Guido van Rossum <guido@python.org>
parents: 505
diff changeset
837 SystemExit = newstdexception("SystemExit");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
838 TypeError = newstdexception("TypeError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
839 ValueError = newstdexception("ValueError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
840 ZeroDivisionError = newstdexception("ZeroDivisionError");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
842
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
844 initbuiltin()
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
846 object *m;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
847 m = initmodule("builtin", builtin_methods);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 builtin_dict = getmoduledict(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 INCREF(builtin_dict);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 initerrors();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 (void) dictinsert(builtin_dict, "None", None);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
853
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
854 /* Coerce two numeric types to the "larger" one.
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
855 Increment the reference count on each argument.
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
856 Return -1 and raise an exception if no coercion is possible
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
857 (and then no reference count is incremented).
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
858 */
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
859
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
860 int
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
861 coerce(pv, pw)
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
862 object **pv, **pw;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
863 {
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
864 register object *v = *pv;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
865 register object *w = *pw;
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
866 int res;
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
867
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
868 if (v->ob_type == w->ob_type && !is_instanceobject(v)) {
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
869 INCREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
870 INCREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
871 return 0;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
872 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
873 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
874 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
875 if (res <= 0)
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
876 return res;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
877 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
878 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
879 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
880 if (res <= 0)
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
881 return res;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
882 }
955
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
883 err_setstr(TypeError, "number coercion failed");
03de67080c95 * classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents: 944
diff changeset
884 return -1;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
885 }