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