Mercurial > cpython-lru_cache-weakref
annotate Python/bltinmodule.c @ 944:222f04f55083 legacy-trunk
Changes so that user-defined classes can implement operations invoked
by special syntax: you can now define your own numbers, sequences and
mappings.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 12 Aug 1992 15:35:34 +0000 |
| parents | c156d4960c2b |
| children | 03de67080c95 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
139
diff
changeset
|
1 /*********************************************************** |
|
795
0cd197947d81
Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents:
748
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
|
145
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 "ceval.h" | |
| 37 #include "modsupport.h" | |
| 38 | |
| 39 static object * | |
| 40 builtin_abs(self, v) | |
| 41 object *self; | |
| 42 object *v; | |
| 43 { | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
44 number_methods *nm; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
45 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
|
46 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
|
47 return NULL; |
| 86 | 48 } |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
49 return (*nm->nb_absolute)(v); |
| 86 | 50 } |
| 51 | |
| 52 static object * | |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
53 builtin_apply(self, v) |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
54 object *self; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
55 object *v; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
56 { |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
57 object *func, *args; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
58 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2) { |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
59 err_setstr(TypeError, "apply() requires (func,args)"); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
60 return NULL; |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
61 } |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
62 func = gettupleitem(v, 0); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
63 args = gettupleitem(v, 1); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
64 return call_object(func, args); |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
65 } |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
66 |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
67 static object * |
| 86 | 68 builtin_chr(self, v) |
| 69 object *self; | |
| 70 object *v; | |
| 71 { | |
| 72 long x; | |
| 73 char s[1]; | |
| 74 if (v == NULL || !is_intobject(v)) { | |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
75 err_setstr(TypeError, "chr() requires int argument"); |
| 86 | 76 return NULL; |
| 77 } | |
| 78 x = getintvalue(v); | |
| 79 if (x < 0 || x >= 256) { | |
| 463 | 80 err_setstr(ValueError, "chr() arg not in range(256)"); |
| 86 | 81 return NULL; |
| 82 } | |
| 83 s[0] = x; | |
| 84 return newsizedstringobject(s, 1); | |
| 85 } | |
| 86 | |
| 87 static object * | |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
88 builtin_coerce(self, args) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
89 object *self; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
90 object *args; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
91 { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
92 object *v, *w; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
93 object *res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
94 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
95 if (!getargs(args, "(OO)", &v, &w)) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
96 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
97 if (coerce(&v, &w) < 0) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
98 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
99 res = mkvalue("(OO)", v, w); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
100 DECREF(v); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
101 DECREF(w); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
102 return res; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
103 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
104 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
105 static object * |
| 86 | 106 builtin_dir(self, v) |
| 107 object *self; | |
| 108 object *v; | |
| 109 { | |
| 110 object *d; | |
| 111 if (v == NULL) { | |
| 112 d = getlocals(); | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
113 INCREF(d); |
| 86 | 114 } |
| 115 else { | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
116 d = getattr(v, "__dict__"); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
117 if (d == NULL) { |
| 86 | 118 err_setstr(TypeError, |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
119 "dir() argument must have __dict__ attribute"); |
| 86 | 120 return NULL; |
| 121 } | |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
122 } |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
123 if (is_dictobject(d)) { |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
124 v = getdictkeys(d); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
125 if (sortlist(v) != 0) { |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
126 DECREF(v); |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
127 v = NULL; |
|
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
128 } |
| 86 | 129 } |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
130 else { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
131 v = newlistobject(0); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
132 } |
|
385
039aee6cbd78
Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents:
332
diff
changeset
|
133 DECREF(d); |
| 86 | 134 return v; |
| 135 } | |
| 136 | |
| 137 static object * | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
138 builtin_divmod(self, args) |
| 86 | 139 object *self; |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
140 object *args; |
| 86 | 141 { |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
142 object *v, *w, *x; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
143 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
|
144 err_setstr(TypeError, "divmod() requires 2 arguments"); |
| 86 | 145 return NULL; |
| 146 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
147 v = gettupleitem(args, 0); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
148 w = gettupleitem(args, 1); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
149 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
|
150 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
|
151 err_setstr(TypeError, "divmod() requires numeric arguments"); |
| 86 | 152 return NULL; |
| 153 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
154 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
|
155 return NULL; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
156 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
|
157 DECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
158 DECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
159 return x; |
| 86 | 160 } |
| 161 | |
| 162 static object * | |
| 163 exec_eval(v, start) | |
| 164 object *v; | |
| 165 int start; | |
| 166 { | |
| 167 object *str = NULL, *globals = NULL, *locals = NULL; | |
|
694
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
168 char *s; |
| 86 | 169 int n; |
| 170 if (v != NULL) { | |
| 171 if (is_stringobject(v)) | |
| 172 str = v; | |
| 173 else if (is_tupleobject(v) && | |
| 174 ((n = gettuplesize(v)) == 2 || n == 3)) { | |
| 175 str = gettupleitem(v, 0); | |
| 176 globals = gettupleitem(v, 1); | |
| 177 if (n == 3) | |
| 178 locals = gettupleitem(v, 2); | |
| 179 } | |
| 180 } | |
| 181 if (str == NULL || !is_stringobject(str) || | |
| 182 globals != NULL && !is_dictobject(globals) || | |
| 183 locals != NULL && !is_dictobject(locals)) { | |
| 184 err_setstr(TypeError, | |
| 185 "exec/eval arguments must be string[,dict[,dict]]"); | |
| 186 return NULL; | |
| 187 } | |
|
694
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
188 s = getstringvalue(str); |
|
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
189 if (start == eval_input) { |
|
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
190 while (*s == ' ' || *s == '\t') |
|
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
191 s++; |
|
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
192 } |
|
5c18d6c57827
Skip leading whitespace of eval() string argument.
Guido van Rossum <guido@python.org>
parents:
673
diff
changeset
|
193 return run_string(s, start, globals, locals); |
| 86 | 194 } |
| 195 | |
| 196 static object * | |
| 197 builtin_eval(self, v) | |
| 198 object *self; | |
| 199 object *v; | |
| 200 { | |
| 201 return exec_eval(v, eval_input); | |
| 202 } | |
| 203 | |
| 204 static object * | |
| 205 builtin_exec(self, v) | |
| 206 object *self; | |
| 207 object *v; | |
| 208 { | |
| 209 return exec_eval(v, file_input); | |
| 210 } | |
| 211 | |
| 212 static object * | |
| 673 | 213 builtin_execfile(self, v) |
| 214 object *self; | |
| 215 object *v; | |
| 216 { | |
| 217 object *str = NULL, *globals = NULL, *locals = NULL, *w; | |
| 218 FILE* fp; | |
| 219 int n; | |
| 220 if (v != NULL) { | |
| 221 if (is_stringobject(v)) | |
| 222 str = v; | |
| 223 else if (is_tupleobject(v) && | |
| 224 ((n = gettuplesize(v)) == 2 || n == 3)) { | |
| 225 str = gettupleitem(v, 0); | |
| 226 globals = gettupleitem(v, 1); | |
| 227 if (n == 3) | |
| 228 locals = gettupleitem(v, 2); | |
| 229 } | |
| 230 } | |
| 231 if (str == NULL || !is_stringobject(str) || | |
| 232 globals != NULL && !is_dictobject(globals) || | |
| 233 locals != NULL && !is_dictobject(locals)) { | |
| 234 err_setstr(TypeError, | |
| 235 "execfile arguments must be filename[,dict[,dict]]"); | |
| 236 return NULL; | |
| 237 } | |
| 926 | 238 BGN_SAVE |
| 673 | 239 fp = fopen(getstringvalue(str), "r"); |
| 926 | 240 END_SAVE |
| 673 | 241 if (fp == NULL) { |
| 242 err_setstr(IOError, "execfile cannot open the file argument"); | |
| 243 return NULL; | |
| 244 } | |
| 245 w = run_file(fp, getstringvalue(str), file_input, globals, locals); | |
| 926 | 246 BGN_SAVE |
| 673 | 247 fclose(fp); |
| 926 | 248 END_SAVE |
| 673 | 249 return w; |
| 250 } | |
| 251 | |
| 252 static object * | |
| 86 | 253 builtin_float(self, v) |
| 254 object *self; | |
| 255 object *v; | |
| 256 { | |
| 257 if (v == NULL) { | |
| 258 /* */ | |
| 259 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
260 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
|
261 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
|
262 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
|
263 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
264 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
|
265 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
|
266 } |
| 86 | 267 else if (is_floatobject(v)) { |
| 268 INCREF(v); | |
| 269 return v; | |
| 270 } | |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
271 else if (is_instanceobject(v)) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
272 return instance_convert(v, "__float__"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
273 } |
|
223
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, "float() argument must be int, long or float"); |
| 86 | 275 return NULL; |
| 276 } | |
| 277 | |
| 278 static object * | |
|
644
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
279 builtin_getattr(self, v) |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
280 object *self; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
281 object *v; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
282 { |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
283 object *name; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
284 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 || |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
285 (name = gettupleitem(v, 1), !is_stringobject(name))) { |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
286 err_setstr(TypeError, |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
287 "getattr() arguments must be (object, string)"); |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
288 return NULL; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
289 } |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
290 return getattr(gettupleitem(v, 0), getstringvalue(name)); |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
291 } |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
292 |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
293 static object * |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
294 builtin_setattr(self, v) |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
295 object *self; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
296 object *v; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
297 { |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
298 object *name; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
299 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 3 || |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
300 (name = gettupleitem(v, 1), !is_stringobject(name))) { |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
301 err_setstr(TypeError, |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
302 "setattr() arguments must be (object, string, object)"); |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
303 return NULL; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
304 } |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
305 if (setattr(gettupleitem(v, 0), |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
306 getstringvalue(name), gettupleitem(v, 2)) != 0) |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
307 return NULL; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
308 INCREF(None); |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
309 return None; |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
310 } |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
311 |
|
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
312 static object * |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
313 builtin_hex(self, v) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
314 object *self; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
315 object *v; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
316 { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
317 if (v != NULL) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
318 if (is_intobject(v)) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
319 char buf[20]; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
320 long x = getintvalue(v); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
321 if (x >= 0) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
322 sprintf(buf, "0x%lx", x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
323 else |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
324 sprintf(buf, "-0x%lx", -x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
325 return newstringobject(buf); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
326 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
327 if (is_longobject(v)) { |
|
606
c763fc64fcab
long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents:
545
diff
changeset
|
328 return long_format(v, 16); |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
329 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
330 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
331 err_setstr(TypeError, "hex() requires int/long argument"); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
332 return NULL; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
333 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
334 |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
335 static object * |
| 86 | 336 builtin_input(self, v) |
| 337 object *self; | |
| 338 object *v; | |
| 339 { | |
| 340 FILE *in = sysgetfile("stdin", stdin); | |
| 341 FILE *out = sysgetfile("stdout", stdout); | |
|
702
f53dcb9a04d6
Strip leading whitespace from input().
Guido van Rossum <guido@python.org>
parents:
694
diff
changeset
|
342 int c; |
| 86 | 343 object *m, *d; |
| 344 flushline(); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
345 if (v != NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
346 if (printobject(v, out, PRINT_RAW) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
347 return NULL; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
348 } |
| 86 | 349 m = add_module("__main__"); |
| 350 d = getmoduledict(m); | |
| 926 | 351 BGN_SAVE |
|
702
f53dcb9a04d6
Strip leading whitespace from input().
Guido van Rossum <guido@python.org>
parents:
694
diff
changeset
|
352 while ((c = getc(in)) != EOF && (c == ' ' || c == '\t')) |
|
f53dcb9a04d6
Strip leading whitespace from input().
Guido van Rossum <guido@python.org>
parents:
694
diff
changeset
|
353 ; |
|
f53dcb9a04d6
Strip leading whitespace from input().
Guido van Rossum <guido@python.org>
parents:
694
diff
changeset
|
354 ungetc(c, in); |
| 926 | 355 END_SAVE |
| 86 | 356 return run_file(in, "<stdin>", expr_input, d, d); |
| 357 } | |
| 358 | |
| 359 static object * | |
| 360 builtin_int(self, v) | |
| 361 object *self; | |
| 362 object *v; | |
| 363 { | |
| 364 if (v == NULL) { | |
| 365 /* */ | |
| 366 } | |
| 367 else if (is_intobject(v)) { | |
| 368 INCREF(v); | |
| 369 return v; | |
| 370 } | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
371 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
|
372 long x; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
373 x = getlongvalue(v); |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
374 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
|
375 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
376 return newintobject(x); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
377 } |
| 86 | 378 else if (is_floatobject(v)) { |
| 379 double x = getfloatvalue(v); | |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
380 /* XXX should check for overflow */ |
| 86 | 381 return newintobject((long)x); |
| 382 } | |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
383 else if (is_instanceobject(v)) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
384 return instance_convert(v, "__int__"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
385 } |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
386 err_setstr(TypeError, "int() argument must be int, long or float"); |
| 86 | 387 return NULL; |
| 388 } | |
| 389 | |
| 390 static object * | |
| 391 builtin_len(self, v) | |
| 392 object *self; | |
| 393 object *v; | |
| 394 { | |
| 395 long len; | |
| 396 typeobject *tp; | |
| 397 if (v == NULL) { | |
| 398 err_setstr(TypeError, "len() without argument"); | |
| 399 return NULL; | |
| 400 } | |
| 401 tp = v->ob_type; | |
| 402 if (tp->tp_as_sequence != NULL) { | |
| 403 len = (*tp->tp_as_sequence->sq_length)(v); | |
| 404 } | |
| 405 else if (tp->tp_as_mapping != NULL) { | |
| 406 len = (*tp->tp_as_mapping->mp_length)(v); | |
| 407 } | |
| 408 else { | |
| 409 err_setstr(TypeError, "len() of unsized object"); | |
| 410 return NULL; | |
| 411 } | |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
412 if (len < 0) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
413 return NULL; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
414 else |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
415 return newintobject(len); |
| 86 | 416 } |
| 417 | |
| 418 static object * | |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
419 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
|
420 object *self; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
421 object *v; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
422 { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
423 if (v == NULL) { |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
424 /* */ |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
425 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
426 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
|
427 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
|
428 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
429 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
|
430 INCREF(v); |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
431 return v; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
432 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
433 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
|
434 double x = getfloatvalue(v); |
|
253
d4e532ee6f3d
Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents:
223
diff
changeset
|
435 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
|
436 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
437 else if (is_instanceobject(v)) { |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
438 return instance_convert(v, "__long__"); |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
439 } |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
440 err_setstr(TypeError, "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
|
441 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
442 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
443 |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
444 static object * |
| 86 | 445 min_max(v, sign) |
| 446 object *v; | |
| 447 int sign; | |
| 448 { | |
| 449 int i, n, cmp; | |
| 450 object *w, *x; | |
| 451 sequence_methods *sq; | |
| 452 if (v == NULL) { | |
| 453 err_setstr(TypeError, "min() or max() without argument"); | |
| 454 return NULL; | |
| 455 } | |
| 456 sq = v->ob_type->tp_as_sequence; | |
| 457 if (sq == NULL) { | |
| 458 err_setstr(TypeError, "min() or max() of non-sequence"); | |
| 459 return NULL; | |
| 460 } | |
| 461 n = (*sq->sq_length)(v); | |
| 462 if (n == 0) { | |
| 463 | 463 err_setstr(ValueError, "min() or max() of empty sequence"); |
| 86 | 464 return NULL; |
| 465 } | |
| 466 w = (*sq->sq_item)(v, 0); /* Implies INCREF */ | |
| 467 for (i = 1; i < n; i++) { | |
| 468 x = (*sq->sq_item)(v, i); /* Implies INCREF */ | |
| 469 cmp = cmpobject(x, w); | |
| 470 if (cmp * sign > 0) { | |
| 471 DECREF(w); | |
| 472 w = x; | |
| 473 } | |
| 474 else | |
| 475 DECREF(x); | |
| 476 } | |
| 477 return w; | |
| 478 } | |
| 479 | |
| 480 static object * | |
| 481 builtin_min(self, v) | |
| 482 object *self; | |
| 483 object *v; | |
| 484 { | |
| 485 return min_max(v, -1); | |
| 486 } | |
| 487 | |
| 488 static object * | |
| 489 builtin_max(self, v) | |
| 490 object *self; | |
| 491 object *v; | |
| 492 { | |
| 493 return min_max(v, 1); | |
| 494 } | |
| 495 | |
| 496 static object * | |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
497 builtin_oct(self, v) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
498 object *self; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
499 object *v; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
500 { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
501 if (v != NULL) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
502 if (is_intobject(v)) { |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
503 char buf[20]; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
504 long x = getintvalue(v); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
505 if (x >= 0) |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
506 sprintf(buf, "0%lo", x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
507 else |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
508 sprintf(buf, "-0%lo", -x); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
509 return newstringobject(buf); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
510 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
511 if (is_longobject(v)) { |
|
606
c763fc64fcab
long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents:
545
diff
changeset
|
512 return long_format(v, 8); |
|
404
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
513 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
514 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
515 err_setstr(TypeError, "oct() requires int/long argument"); |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
516 return NULL; |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
517 } |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
518 |
|
9cd38d1ac391
Made dir() more robust.
Guido van Rossum <guido@python.org>
parents:
385
diff
changeset
|
519 static object * |
| 86 | 520 builtin_open(self, v) |
| 521 object *self; | |
| 522 object *v; | |
| 523 { | |
| 524 object *name, *mode; | |
| 525 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 || | |
| 526 !is_stringobject(name = gettupleitem(v, 0)) || | |
| 527 !is_stringobject(mode = gettupleitem(v, 1))) { | |
| 528 err_setstr(TypeError, "open() requires 2 string arguments"); | |
| 529 return NULL; | |
| 530 } | |
| 531 v = newfileobject(getstringvalue(name), getstringvalue(mode)); | |
| 532 return v; | |
| 533 } | |
| 534 | |
| 535 static object * | |
| 536 builtin_ord(self, v) | |
| 537 object *self; | |
| 538 object *v; | |
| 539 { | |
| 540 if (v == NULL || !is_stringobject(v)) { | |
| 541 err_setstr(TypeError, "ord() must have string argument"); | |
| 542 return NULL; | |
| 543 } | |
| 544 if (getstringsize(v) != 1) { | |
| 463 | 545 err_setstr(ValueError, "ord() arg must have length 1"); |
| 86 | 546 return NULL; |
| 547 } | |
| 548 return newintobject((long)(getstringvalue(v)[0] & 0xff)); | |
| 549 } | |
| 550 | |
| 551 static object * | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
552 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
|
553 object *self; |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
554 object *args; |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
555 { |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
556 object *v, *w, *x; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
557 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
|
558 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
|
559 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
560 } |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
561 v = gettupleitem(args, 0); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
562 w = gettupleitem(args, 1); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
563 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
|
564 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
|
565 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
|
566 return NULL; |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
567 } |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
568 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
|
569 return NULL; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
570 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
|
571 DECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
572 DECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
573 return x; |
|
223
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
574 } |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
575 |
|
6693c5d87f3a
Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents:
182
diff
changeset
|
576 static object * |
| 86 | 577 builtin_range(self, v) |
| 578 object *self; | |
| 579 object *v; | |
| 580 { | |
| 581 static char *errmsg = "range() requires 1-3 int arguments"; | |
| 582 int i, n; | |
| 583 long ilow, ihigh, istep; | |
| 584 if (v != NULL && is_intobject(v)) { | |
| 585 ilow = 0; ihigh = getintvalue(v); istep = 1; | |
| 586 } | |
| 587 else if (v == NULL || !is_tupleobject(v)) { | |
| 588 err_setstr(TypeError, errmsg); | |
| 589 return NULL; | |
| 590 } | |
| 591 else { | |
| 592 n = gettuplesize(v); | |
| 593 if (n < 1 || n > 3) { | |
| 594 err_setstr(TypeError, errmsg); | |
| 595 return NULL; | |
| 596 } | |
| 597 for (i = 0; i < n; i++) { | |
| 598 if (!is_intobject(gettupleitem(v, i))) { | |
| 599 err_setstr(TypeError, errmsg); | |
| 600 return NULL; | |
| 601 } | |
| 602 } | |
| 603 if (n == 3) { | |
| 604 istep = getintvalue(gettupleitem(v, 2)); | |
| 605 --n; | |
| 606 } | |
| 607 else | |
| 608 istep = 1; | |
| 609 ihigh = getintvalue(gettupleitem(v, --n)); | |
| 610 if (n > 0) | |
| 611 ilow = getintvalue(gettupleitem(v, 0)); | |
| 612 else | |
| 613 ilow = 0; | |
| 614 } | |
| 615 if (istep == 0) { | |
| 463 | 616 err_setstr(ValueError, "zero step for range()"); |
| 86 | 617 return NULL; |
| 618 } | |
| 619 /* XXX ought to check overflow of subtraction */ | |
| 620 if (istep > 0) | |
| 621 n = (ihigh - ilow + istep - 1) / istep; | |
| 622 else | |
| 623 n = (ihigh - ilow + istep + 1) / istep; | |
| 624 if (n < 0) | |
| 625 n = 0; | |
| 626 v = newlistobject(n); | |
| 627 if (v == NULL) | |
| 628 return NULL; | |
| 629 for (i = 0; i < n; i++) { | |
| 630 object *w = newintobject(ilow); | |
| 631 if (w == NULL) { | |
| 632 DECREF(v); | |
| 633 return NULL; | |
| 634 } | |
| 635 setlistitem(v, i, w); | |
| 636 ilow += istep; | |
| 637 } | |
| 638 return v; | |
| 639 } | |
| 640 | |
| 641 static object * | |
| 642 builtin_raw_input(self, v) | |
| 643 object *self; | |
| 644 object *v; | |
| 645 { | |
| 646 FILE *out = sysgetfile("stdout", stdout); | |
| 647 flushline(); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
648 if (v != NULL) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
649 if (printobject(v, out, PRINT_RAW) != 0) |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
650 return NULL; |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
253
diff
changeset
|
651 } |
|
182
59ffb6d4b34c
Use fileobject's filegetline() to implement unlimited raw_input().
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
652 return filegetline(sysget("stdin"), -1); |
| 86 | 653 } |
| 654 | |
| 655 static object * | |
| 656 builtin_reload(self, v) | |
| 657 object *self; | |
| 658 object *v; | |
| 659 { | |
| 660 return reload_module(v); | |
| 661 } | |
| 662 | |
| 663 static object * | |
| 664 builtin_type(self, v) | |
| 665 object *self; | |
| 666 object *v; | |
| 667 { | |
| 668 if (v == NULL) { | |
| 669 err_setstr(TypeError, "type() requres an argument"); | |
| 670 return NULL; | |
| 671 } | |
| 672 v = (object *)v->ob_type; | |
| 673 INCREF(v); | |
| 674 return v; | |
| 675 } | |
| 676 | |
| 677 static struct methodlist builtin_methods[] = { | |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
678 {"abs", builtin_abs}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
679 {"apply", builtin_apply}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
680 {"chr", builtin_chr}, |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
681 {"coerce", builtin_coerce}, |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
682 {"dir", builtin_dir}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
683 {"divmod", builtin_divmod}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
684 {"eval", builtin_eval}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
685 {"exec", builtin_exec}, |
| 673 | 686 {"execfile", builtin_execfile}, |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
687 {"float", builtin_float}, |
|
644
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
688 {"getattr", builtin_getattr}, |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
689 {"hex", builtin_hex}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
690 {"input", builtin_input}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
691 {"int", builtin_int}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
692 {"len", builtin_len}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
693 {"long", builtin_long}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
694 {"max", builtin_max}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
695 {"min", builtin_min}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
696 {"oct", builtin_oct}, |
|
662
aaff650d7e13
Get rid of comment about open().
Guido van Rossum <guido@python.org>
parents:
644
diff
changeset
|
697 {"open", builtin_open}, |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
698 {"ord", builtin_ord}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
699 {"pow", builtin_pow}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
700 {"range", builtin_range}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
701 {"raw_input", builtin_raw_input}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
702 {"reload", builtin_reload}, |
|
644
59f9325bb8ec
Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents:
606
diff
changeset
|
703 {"setattr", builtin_setattr}, |
|
480
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
704 {"type", builtin_type}, |
|
c64c6702549d
Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents:
463
diff
changeset
|
705 {NULL, NULL}, |
| 86 | 706 }; |
| 707 | |
| 708 static object *builtin_dict; | |
| 709 | |
| 710 object * | |
| 711 getbuiltin(name) | |
|
332
be3052a77b7f
Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents:
301
diff
changeset
|
712 object *name; |
| 86 | 713 { |
|
332
be3052a77b7f
Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents:
301
diff
changeset
|
714 return dict2lookup(builtin_dict, name); |
| 86 | 715 } |
| 716 | |
| 717 /* Predefined exceptions */ | |
| 718 | |
| 495 | 719 object *AttributeError; |
| 86 | 720 object *EOFError; |
| 495 | 721 object *IOError; |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
722 object *ImportError; |
| 495 | 723 object *IndexError; |
| 724 object *KeyError; | |
| 725 object *KeyboardInterrupt; | |
| 86 | 726 object *MemoryError; |
| 727 object *NameError; | |
| 495 | 728 object *OverflowError; |
| 729 object *RuntimeError; | |
| 730 object *SyntaxError; | |
| 86 | 731 object *SystemError; |
| 545 | 732 object *SystemExit; |
| 495 | 733 object *TypeError; |
| 734 object *ValueError; | |
| 463 | 735 object *ZeroDivisionError; |
| 736 | |
| 86 | 737 static object * |
| 495 | 738 newstdexception(name) |
| 739 char *name; | |
| 86 | 740 { |
| 495 | 741 object *v = newstringobject(name); |
| 86 | 742 if (v == NULL || dictinsert(builtin_dict, name, v) != 0) |
| 743 fatal("no mem for new standard exception"); | |
| 744 return v; | |
| 745 } | |
| 746 | |
| 747 static void | |
| 748 initerrors() | |
| 749 { | |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
750 AttributeError = newstdexception("AttributeError"); |
| 495 | 751 EOFError = newstdexception("EOFError"); |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
752 IOError = newstdexception("IOError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
753 ImportError = newstdexception("ImportError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
754 IndexError = newstdexception("IndexError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
755 KeyError = newstdexception("KeyError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
756 KeyboardInterrupt = newstdexception("KeyboardInterrupt"); |
| 495 | 757 MemoryError = newstdexception("MemoryError"); |
| 758 NameError = newstdexception("NameError"); | |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
759 OverflowError = newstdexception("OverflowError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
760 RuntimeError = newstdexception("RuntimeError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
761 SyntaxError = newstdexception("SyntaxError"); |
| 495 | 762 SystemError = newstdexception("SystemError"); |
| 545 | 763 SystemExit = newstdexception("SystemExit"); |
|
505
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
764 TypeError = newstdexception("TypeError"); |
|
095d9cf4e3e6
Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents:
495
diff
changeset
|
765 ValueError = newstdexception("ValueError"); |
| 495 | 766 ZeroDivisionError = newstdexception("ZeroDivisionError"); |
| 86 | 767 } |
| 768 | |
| 769 void | |
| 770 initbuiltin() | |
| 771 { | |
| 772 object *m; | |
| 773 m = initmodule("builtin", builtin_methods); | |
| 774 builtin_dict = getmoduledict(m); | |
| 775 INCREF(builtin_dict); | |
| 776 initerrors(); | |
| 777 (void) dictinsert(builtin_dict, "None", None); | |
| 778 } | |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
779 |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
780 /* 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
|
781 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
|
782 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
|
783 (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
|
784 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
|
785 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
|
786 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
|
787 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
|
788 |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
789 int |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
790 coerce(pv, pw) |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
791 object **pv, **pw; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
792 { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
793 register object *v = *pv; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
794 register object *w = *pw; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
795 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
|
796 INCREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
797 INCREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
798 return 0; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
799 } |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
800 if (is_instanceobject(v) || is_instanceobject(w)) |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
801 return instance_coerce(pv, pw); |
|
301
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
802 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
|
803 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
|
804 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
|
805 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
806 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
807 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
|
808 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
|
809 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
|
810 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
811 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
|
812 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
|
813 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
|
814 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
815 else { |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
816 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
|
817 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
818 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
819 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
|
820 XDECREF(v); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
821 XDECREF(w); |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
822 return -1; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
823 } |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
824 *pv = v; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
825 *pw = w; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
826 return 0; |
|
c28fabe21165
Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
827 } |
