annotate Python/bltinmodule.c @ 301:c28fabe21165 legacy-trunk

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