Mercurial > cpython-lru_cache-weakref
annotate Python/bltinmodule.c @ 606:c763fc64fcab legacy-trunk
long_format() is now declared in longobject.h.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 19 Jan 1992 16:25:49 +0000 |
| parents | 9f06c6689f0d |
| children | 59f9325bb8ec |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
139
diff
changeset
|
1 /*********************************************************** |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
139
diff
changeset
|
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
139
diff
changeset
|
3 Netherlands. |
|
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 | 25 /* Built-in functions */ |
| 26 | |
| 27 #include "allobjects.h" | |
| 28 | |
| 29 #include "node.h" | |
| 30 #include "graminit.h" | |
| 31 #include "errcode.h" | |
| 32 #include "sysmodule.h" | |
| 119 | 33 #include "bltinmodule.h" |
| 86 | 34 #include "import.h" |
| 35 #include "pythonrun.h" | |
| 36 #include "compile.h" /* For ceval.h */ | |
| 37 #include "ceval.h" | |
| 38 #include "modsupport.h" | |
| 39 | |
| 40 static object * | |
| 41 builtin_abs(self, v) | |
| 42 object *self; | |
| 43 object *v; | |
| 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 | 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 | 51 } |
| 52 | |
| 53 static object * | |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
54 builtin_apply(self, v) |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
55 object *self; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
56 object *v; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
57 { |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
58 object *func, *args; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
59 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) { |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
60 err_setstr(TypeError, "apply() requires (func,args)"); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
61 return NULL; |
|
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 func = gettupleitem(v, 0); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
64 args = gettupleitem(v, 1); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
65 return call_object(func, args); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
66 } |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
67 |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
68 static object * |
| 86 | 69 builtin_chr(self, v) |
| 70 object *self; | |
| 71 object *v; | |
| 72 { | |
| 73 long x; | |
| 74 char s[1]; | |
| 75 if (v == NULL || !is_intobject(v)) { | |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
76 err_setstr(TypeError, "chr() requires int argument"); |
| 86 | 77 return NULL; |
| 78 } | |
| 79 x = getintvalue(v); | |
| 80 if (x < 0 || x >= 256) { | |
| 463 | 81 err_setstr(ValueError, "chr() arg not in range(256)"); |
| 86 | 82 return NULL; |
| 83 } | |
| 84 s[0] = x; | |
| 85 return newsizedstringobject(s, 1); | |
| 86 } | |
| 87 | |
| 88 static object * | |
| 89 builtin_dir(self, v) | |
| 90 object *self; | |
| 91 object *v; | |
| 92 { | |
| 93 object *d; | |
| 94 if (v == NULL) { | |
| 95 d = getlocals(); | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
96 INCREF(d); |
| 86 | 97 } |
| 98 else { | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
99 d = getattr(v, "__dict__"); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
100 if (d == NULL) { |
| 86 | 101 err_setstr(TypeError, |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
102 "dir() argument must have __dict__ attribute"); |
| 86 | 103 return NULL; |
| 104 } | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
105 } |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
106 if (is_dictobject(d)) { |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
107 v = getdictkeys(d); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
108 if (sortlist(v) != 0) { |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
109 DECREF(v); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
110 v = NULL; |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
111 } |
| 86 | 112 } |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
113 else { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
114 v = newlistobject(0); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
115 } |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
116 DECREF(d); |
| 86 | 117 return v; |
| 118 } | |
| 119 | |
| 120 static object * | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
121 builtin_divmod(self, args) |
| 86 | 122 object *self; |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
123 object *args; |
| 86 | 124 { |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
125 object *v, *w, *x; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
126 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
127 err_setstr(TypeError, "divmod() requires 2 arguments"); |
| 86 | 128 return NULL; |
| 129 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
130 v = gettupleitem(args, 0); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
131 w = gettupleitem(args, 1); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
132 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
|
133 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
|
134 err_setstr(TypeError, "divmod() requires numeric arguments"); |
| 86 | 135 return NULL; |
| 136 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
137 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
|
138 return NULL; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
139 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
|
140 DECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
141 DECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
142 return x; |
| 86 | 143 } |
| 144 | |
| 145 static object * | |
| 146 exec_eval(v, start) | |
| 147 object *v; | |
| 148 int start; | |
| 149 { | |
| 150 object *str = NULL, *globals = NULL, *locals = NULL; | |
| 151 int n; | |
| 152 if (v != NULL) { | |
| 153 if (is_stringobject(v)) | |
| 154 str = v; | |
| 155 else if (is_tupleobject(v) && | |
| 156 ((n = gettuplesize(v)) == 2 || n == 3)) { | |
| 157 str = gettupleitem(v, 0); | |
| 158 globals = gettupleitem(v, 1); | |
| 159 if (n == 3) | |
| 160 locals = gettupleitem(v, 2); | |
| 161 } | |
| 162 } | |
| 163 if (str == NULL || !is_stringobject(str) || | |
| 164 globals != NULL && !is_dictobject(globals) || | |
| 165 locals != NULL && !is_dictobject(locals)) { | |
| 166 err_setstr(TypeError, | |
| 167 "exec/eval arguments must be string[,dict[,dict]]"); | |
| 168 return NULL; | |
| 169 } | |
| 170 return run_string(getstringvalue(str), start, globals, locals); | |
| 171 } | |
| 172 | |
| 173 static object * | |
| 174 builtin_eval(self, v) | |
| 175 object *self; | |
| 176 object *v; | |
| 177 { | |
| 178 return exec_eval(v, eval_input); | |
| 179 } | |
| 180 | |
| 181 static object * | |
| 182 builtin_exec(self, v) | |
| 183 object *self; | |
| 184 object *v; | |
| 185 { | |
| 186 return exec_eval(v, file_input); | |
| 187 } | |
| 188 | |
| 189 static object * | |
| 190 builtin_float(self, v) | |
| 191 object *self; | |
| 192 object *v; | |
| 193 { | |
| 194 if (v == NULL) { | |
| 195 /* */ | |
| 196 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
197 else if (is_intobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
198 long x = getintvalue(v); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
199 return newfloatobject((double)x); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
200 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
201 else if (is_longobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
202 return newfloatobject(dgetlongvalue(v)); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
203 } |
| 86 | 204 else if (is_floatobject(v)) { |
| 205 INCREF(v); | |
| 206 return v; | |
| 207 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
208 err_setstr(TypeError, "float() argument must be int, long or float"); |
| 86 | 209 return NULL; |
| 210 } | |
| 211 | |
| 212 static object * | |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
213 builtin_hex(self, v) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
214 object *self; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
215 object *v; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
216 { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
217 if (v != NULL) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
218 if (is_intobject(v)) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
219 char buf[20]; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
220 long x = getintvalue(v); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
221 if (x >= 0) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
222 sprintf(buf, "0x%lx", x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
223 else |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
224 sprintf(buf, "-0x%lx", -x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
225 return newstringobject(buf); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
226 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
227 if (is_longobject(v)) { |
|
606
c763fc64fcab
long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents:
545
diff
changeset
|
228 return long_format(v, 16); |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
229 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
230 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
231 err_setstr(TypeError, "hex() requires int/long argument"); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
232 return NULL; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
233 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
234 |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
235 static object * |
| 86 | 236 builtin_input(self, v) |
| 237 object *self; | |
| 238 object *v; | |
| 239 { | |
| 240 FILE *in = sysgetfile("stdin", stdin); | |
| 241 FILE *out = sysgetfile("stdout", stdout); | |
| 242 node *n; | |
| 243 int err; | |
| 244 object *m, *d; | |
| 245 flushline(); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
246 if (v != NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
247 if (printobject(v, out, PRINT_RAW) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
248 return NULL; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
249 } |
| 86 | 250 m = add_module("__main__"); |
| 251 d = getmoduledict(m); | |
| 252 return run_file(in, "<stdin>", expr_input, d, d); | |
| 253 } | |
| 254 | |
| 255 static object * | |
| 256 builtin_int(self, v) | |
| 257 object *self; | |
| 258 object *v; | |
| 259 { | |
| 260 if (v == NULL) { | |
| 261 /* */ | |
| 262 } | |
| 263 else if (is_intobject(v)) { | |
| 264 INCREF(v); | |
| 265 return v; | |
| 266 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
267 else if (is_longobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
268 long x; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
269 x = getlongvalue(v); |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
270 if (err_occurred()) |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
271 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
272 return newintobject(x); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
273 } |
| 86 | 274 else if (is_floatobject(v)) { |
| 275 double x = getfloatvalue(v); | |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
276 /* XXX should check for overflow */ |
| 86 | 277 return newintobject((long)x); |
| 278 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
279 err_setstr(TypeError, "int() argument must be int, long or float"); |
| 86 | 280 return NULL; |
| 281 } | |
| 282 | |
| 283 static object * | |
| 284 builtin_len(self, v) | |
| 285 object *self; | |
| 286 object *v; | |
| 287 { | |
| 288 long len; | |
| 289 typeobject *tp; | |
| 290 if (v == NULL) { | |
| 291 err_setstr(TypeError, "len() without argument"); | |
| 292 return NULL; | |
| 293 } | |
| 294 tp = v->ob_type; | |
| 295 if (tp->tp_as_sequence != NULL) { | |
| 296 len = (*tp->tp_as_sequence->sq_length)(v); | |
| 297 } | |
| 298 else if (tp->tp_as_mapping != NULL) { | |
| 299 len = (*tp->tp_as_mapping->mp_length)(v); | |
| 300 } | |
| 301 else { | |
| 302 err_setstr(TypeError, "len() of unsized object"); | |
| 303 return NULL; | |
| 304 } | |
| 305 return newintobject(len); | |
| 306 } | |
| 307 | |
| 308 static object * | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
309 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
|
310 object *self; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
311 object *v; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
312 { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
313 if (v == NULL) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
314 /* */ |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
315 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
316 else if (is_intobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
317 return newlongobject(getintvalue(v)); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
318 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
319 else if (is_longobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
320 INCREF(v); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
321 return v; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
322 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
323 else if (is_floatobject(v)) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
324 double x = getfloatvalue(v); |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
325 return dnewlongobject(x); |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
326 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
327 err_setstr(TypeError, "long() argument must be int, long or float"); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
328 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
329 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
330 |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
331 static object * |
| 86 | 332 min_max(v, sign) |
| 333 object *v; | |
| 334 int sign; | |
| 335 { | |
| 336 int i, n, cmp; | |
| 337 object *w, *x; | |
| 338 sequence_methods *sq; | |
| 339 if (v == NULL) { | |
| 340 err_setstr(TypeError, "min() or max() without argument"); | |
| 341 return NULL; | |
| 342 } | |
| 343 sq = v->ob_type->tp_as_sequence; | |
| 344 if (sq == NULL) { | |
| 345 err_setstr(TypeError, "min() or max() of non-sequence"); | |
| 346 return NULL; | |
| 347 } | |
| 348 n = (*sq->sq_length)(v); | |
| 349 if (n == 0) { | |
| 463 | 350 err_setstr(ValueError, "min() or max() of empty sequence"); |
| 86 | 351 return NULL; |
| 352 } | |
| 353 w = (*sq->sq_item)(v, 0); /* Implies INCREF */ | |
| 354 for (i = 1; i < n; i++) { | |
| 355 x = (*sq->sq_item)(v, i); /* Implies INCREF */ | |
| 356 cmp = cmpobject(x, w); | |
| 357 if (cmp * sign > 0) { | |
| 358 DECREF(w); | |
| 359 w = x; | |
| 360 } | |
| 361 else | |
| 362 DECREF(x); | |
| 363 } | |
| 364 return w; | |
| 365 } | |
| 366 | |
| 367 static object * | |
| 368 builtin_min(self, v) | |
| 369 object *self; | |
| 370 object *v; | |
| 371 { | |
| 372 return min_max(v, -1); | |
| 373 } | |
| 374 | |
| 375 static object * | |
| 376 builtin_max(self, v) | |
| 377 object *self; | |
| 378 object *v; | |
| 379 { | |
| 380 return min_max(v, 1); | |
| 381 } | |
| 382 | |
| 383 static object * | |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
384 builtin_oct(self, v) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
385 object *self; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
386 object *v; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
387 { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
388 if (v != NULL) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
389 if (is_intobject(v)) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
390 char buf[20]; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
391 long x = getintvalue(v); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
392 if (x >= 0) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
393 sprintf(buf, "0%lo", x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
394 else |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
395 sprintf(buf, "-0%lo", -x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
396 return newstringobject(buf); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
397 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
398 if (is_longobject(v)) { |
|
606
c763fc64fcab
long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents:
545
diff
changeset
|
399 return long_format(v, 8); |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
400 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
401 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
402 err_setstr(TypeError, "oct() requires int/long argument"); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
403 return NULL; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
404 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
405 |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
406 static object * |
| 86 | 407 builtin_open(self, v) |
| 408 object *self; | |
| 409 object *v; | |
| 410 { | |
| 411 object *name, *mode; | |
| 412 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 || | |
| 413 !is_stringobject(name = gettupleitem(v, 0)) || | |
| 414 !is_stringobject(mode = gettupleitem(v, 1))) { | |
| 415 err_setstr(TypeError, "open() requires 2 string arguments"); | |
| 416 return NULL; | |
| 417 } | |
| 418 v = newfileobject(getstringvalue(name), getstringvalue(mode)); | |
| 419 return v; | |
| 420 } | |
| 421 | |
| 422 static object * | |
| 423 builtin_ord(self, v) | |
| 424 object *self; | |
| 425 object *v; | |
| 426 { | |
| 427 if (v == NULL || !is_stringobject(v)) { | |
| 428 err_setstr(TypeError, "ord() must have string argument"); | |
| 429 return NULL; | |
| 430 } | |
| 431 if (getstringsize(v) != 1) { | |
| 463 | 432 err_setstr(ValueError, "ord() arg must have length 1"); |
| 86 | 433 return NULL; |
| 434 } | |
| 435 return newintobject((long)(getstringvalue(v)[0] & 0xff)); | |
| 436 } | |
| 437 | |
| 438 static object * | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
439 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
|
440 object *self; |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
441 object *args; |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
442 { |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
443 object *v, *w, *x; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
444 if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
445 err_setstr(TypeError, "pow() requires 2 arguments"); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
446 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
447 } |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
448 v = gettupleitem(args, 0); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
449 w = gettupleitem(args, 1); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
450 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
|
451 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
|
452 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
|
453 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
454 } |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
455 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
|
456 return NULL; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
457 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
|
458 DECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
459 DECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
460 return x; |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
461 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
462 |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
463 static object * |
| 86 | 464 builtin_range(self, v) |
| 465 object *self; | |
| 466 object *v; | |
| 467 { | |
| 468 static char *errmsg = "range() requires 1-3 int arguments"; | |
| 469 int i, n; | |
| 470 long ilow, ihigh, istep; | |
| 471 if (v != NULL && is_intobject(v)) { | |
| 472 ilow = 0; ihigh = getintvalue(v); istep = 1; | |
| 473 } | |
| 474 else if (v == NULL || !is_tupleobject(v)) { | |
| 475 err_setstr(TypeError, errmsg); | |
| 476 return NULL; | |
| 477 } | |
| 478 else { | |
| 479 n = gettuplesize(v); | |
| 480 if (n < 1 || n > 3) { | |
| 481 err_setstr(TypeError, errmsg); | |
| 482 return NULL; | |
| 483 } | |
| 484 for (i = 0; i < n; i++) { | |
| 485 if (!is_intobject(gettupleitem(v, i))) { | |
| 486 err_setstr(TypeError, errmsg); | |
| 487 return NULL; | |
| 488 } | |
| 489 } | |
| 490 if (n == 3) { | |
| 491 istep = getintvalue(gettupleitem(v, 2)); | |
| 492 --n; | |
| 493 } | |
| 494 else | |
| 495 istep = 1; | |
| 496 ihigh = getintvalue(gettupleitem(v, --n)); | |
| 497 if (n > 0) | |
| 498 ilow = getintvalue(gettupleitem(v, 0)); | |
| 499 else | |
| 500 ilow = 0; | |
| 501 } | |
| 502 if (istep == 0) { | |
| 463 | 503 err_setstr(ValueError, "zero step for range()"); |
| 86 | 504 return NULL; |
| 505 } | |
| 506 /* XXX ought to check overflow of subtraction */ | |
| 507 if (istep > 0) | |
| 508 n = (ihigh - ilow + istep - 1) / istep; | |
| 509 else | |
| 510 n = (ihigh - ilow + istep + 1) / istep; | |
| 511 if (n < 0) | |
| 512 n = 0; | |
| 513 v = newlistobject(n); | |
| 514 if (v == NULL) | |
| 515 return NULL; | |
| 516 for (i = 0; i < n; i++) { | |
| 517 object *w = newintobject(ilow); | |
| 518 if (w == NULL) { | |
| 519 DECREF(v); | |
| 520 return NULL; | |
| 521 } | |
| 522 setlistitem(v, i, w); | |
| 523 ilow += istep; | |
| 524 } | |
| 525 return v; | |
| 526 } | |
| 527 | |
| 528 static object * | |
| 529 builtin_raw_input(self, v) | |
| 530 object *self; | |
| 531 object *v; | |
| 532 { | |
| 533 FILE *out = sysgetfile("stdout", stdout); | |
| 534 flushline(); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
535 if (v != NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
536 if (printobject(v, out, PRINT_RAW) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
537 return NULL; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
538 } |
|
182
59ffb6d4b34c
Use fileobject's filegetline() to implement unlimited raw_input().
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
539 return filegetline(sysget("stdin"), -1); |
| 86 | 540 } |
| 541 | |
| 542 static object * | |
| 543 builtin_reload(self, v) | |
| 544 object *self; | |
| 545 object *v; | |
| 546 { | |
| 547 return reload_module(v); | |
| 548 } | |
| 549 | |
| 550 static object * | |
| 551 builtin_type(self, v) | |
| 552 object *self; | |
| 553 object *v; | |
| 554 { | |
| 555 if (v == NULL) { | |
| 556 err_setstr(TypeError, "type() requres an argument"); | |
| 557 return NULL; | |
| 558 } | |
| 559 v = (object *)v->ob_type; | |
| 560 INCREF(v); | |
| 561 return v; | |
| 562 } | |
| 563 | |
| 564 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
|
565 {"abs", builtin_abs}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
566 {"apply", builtin_apply}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
567 {"chr", builtin_chr}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
568 {"dir", builtin_dir}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
569 {"divmod", builtin_divmod}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
570 {"eval", builtin_eval}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
571 {"exec", builtin_exec}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
572 {"float", builtin_float}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
573 {"hex", builtin_hex}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
574 {"input", builtin_input}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
575 {"int", builtin_int}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
576 {"len", builtin_len}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
577 {"long", builtin_long}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
578 {"max", builtin_max}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
579 {"min", builtin_min}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
580 {"oct", builtin_oct}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
581 {"open", builtin_open}, /* XXX move to OS module */ |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
582 {"ord", builtin_ord}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
583 {"pow", builtin_pow}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
584 {"range", builtin_range}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
585 {"raw_input", builtin_raw_input}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
586 {"reload", builtin_reload}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
587 {"type", builtin_type}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
588 {NULL, NULL}, |
| 86 | 589 }; |
| 590 | |
| 591 static object *builtin_dict; | |
| 592 | |
| 593 object * | |
| 594 getbuiltin(name) | |
|
332
be3052a77b7f
Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents:
301
diff
changeset
|
595 object *name; |
| 86 | 596 { |
|
332
be3052a77b7f
Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents:
301
diff
changeset
|
597 return dict2lookup(builtin_dict, name); |
| 86 | 598 } |
| 599 | |
| 600 /* Predefined exceptions */ | |
| 601 | |
| 495 | 602 object *AttributeError; |
| 86 | 603 object *EOFError; |
| 495 | 604 object *IOError; |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
605 object *ImportError; |
| 495 | 606 object *IndexError; |
| 607 object *KeyError; | |
| 608 object *KeyboardInterrupt; | |
| 86 | 609 object *MemoryError; |
| 610 object *NameError; | |
| 495 | 611 object *OverflowError; |
| 612 object *RuntimeError; | |
| 613 object *SyntaxError; | |
| 86 | 614 object *SystemError; |
| 545 | 615 object *SystemExit; |
| 495 | 616 object *TypeError; |
| 617 object *ValueError; | |
| 463 | 618 object *ZeroDivisionError; |
| 619 | |
| 86 | 620 static object * |
| 495 | 621 newstdexception(name) |
| 622 char *name; | |
| 86 | 623 { |
| 495 | 624 object *v = newstringobject(name); |
| 86 | 625 if (v == NULL || dictinsert(builtin_dict, name, v) != 0) |
| 626 fatal("no mem for new standard exception"); | |
| 627 return v; | |
| 628 } | |
| 629 | |
| 630 static void | |
| 631 initerrors() | |
| 632 { | |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
633 AttributeError = newstdexception("AttributeError"); |
| 495 | 634 EOFError = newstdexception("EOFError"); |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
635 IOError = newstdexception("IOError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
636 ImportError = newstdexception("ImportError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
637 IndexError = newstdexception("IndexError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
638 KeyError = newstdexception("KeyError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
639 KeyboardInterrupt = newstdexception("KeyboardInterrupt"); |
| 495 | 640 MemoryError = newstdexception("MemoryError"); |
| 641 NameError = newstdexception("NameError"); | |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
642 OverflowError = newstdexception("OverflowError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
643 RuntimeError = newstdexception("RuntimeError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
644 SyntaxError = newstdexception("SyntaxError"); |
| 495 | 645 SystemError = newstdexception("SystemError"); |
| 545 | 646 SystemExit = newstdexception("SystemExit"); |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
647 TypeError = newstdexception("TypeError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
648 ValueError = newstdexception("ValueError"); |
| 495 | 649 ZeroDivisionError = newstdexception("ZeroDivisionError"); |
| 86 | 650 } |
| 651 | |
| 652 void | |
| 653 initbuiltin() | |
| 654 { | |
| 655 object *m; | |
| 656 m = initmodule("builtin", builtin_methods); | |
| 657 builtin_dict = getmoduledict(m); | |
| 658 INCREF(builtin_dict); | |
| 659 initerrors(); | |
| 660 (void) dictinsert(builtin_dict, "None", None); | |
| 661 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
662 |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
663 /* 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
|
664 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
|
665 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
|
666 (and then no reference count is incremented). |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
667 XXX This should be distributed over the various numeric types, |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
668 XXX but for now I don't see how to implement that. |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
669 XXX So, for now, if you add a new numeric type, |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
670 XXX you must add to this function as well. */ |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
671 |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
672 int |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
673 coerce(pv, pw) |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
674 object **pv, **pw; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
675 { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
676 register object *v = *pv; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
677 register object *w = *pw; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
678 if (v->ob_type == w->ob_type) { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
679 INCREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
680 INCREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
681 return 0; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
682 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
683 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
|
684 w->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
|
685 err_setstr(TypeError, "mixing number and non-number"); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
686 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
687 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
688 if (is_floatobject(v) || is_floatobject(w)) { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
689 v = builtin_float((object *)0, v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
690 w = builtin_float((object *)0, w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
691 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
692 else if (is_longobject(v) || is_longobject(w)) { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
693 v = builtin_long((object *)0, v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
694 w = builtin_long((object *)0, w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
695 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
696 else { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
697 err_setstr(TypeError, "can't coerce numeric types?!?!?"); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
698 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
699 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
700 if (v == NULL || w == NULL) { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
701 XDECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
702 XDECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
703 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
704 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
705 *pv = v; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
706 *pw = w; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
707 return 0; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
708 } |
