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