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