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