annotate Python/bltinmodule.c @ 662:aaff650d7e13 legacy-trunk

Get rid of comment about open().
author Guido van Rossum <guido@python.org>
date Wed, 05 Feb 1992 11:17:52 +0000
parents 59f9325bb8ec
children 71d496259ad3
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 *
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
54 builtin_apply(self, v)
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
55 object *self;
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
56 object *v;
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
57 {
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
58 object *func, *args;
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
59 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
60 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
61 return NULL;
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
62 }
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
63 func = gettupleitem(v, 0);
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
64 args = gettupleitem(v, 1);
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
65 return call_object(func, args);
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
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
68 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 builtin_chr(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 object *v;
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 long x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 char s[1];
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 if (v == NULL || !is_intobject(v)) {
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
76 err_setstr(TypeError, "chr() requires int argument");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 x = getintvalue(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 if (x < 0 || x >= 256) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
81 err_setstr(ValueError, "chr() arg not in range(256)");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 s[0] = x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 return newsizedstringobject(s, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 }
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 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 builtin_dir(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 object *d;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 d = getlocals();
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
96 INCREF(d);
86
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 else {
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
99 d = getattr(v, "__dict__");
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
100 if (d == NULL) {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 err_setstr(TypeError,
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
102 "dir() argument must have __dict__ attribute");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
105 }
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
106 if (is_dictobject(d)) {
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
107 v = getdictkeys(d);
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
108 if (sortlist(v) != 0) {
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
109 DECREF(v);
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
110 v = NULL;
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
111 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 }
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
113 else {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
114 v = newlistobject(0);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
115 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
116 DECREF(d);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 static object *
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
121 builtin_divmod(self, args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 object *self;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
123 object *args;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 {
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
125 object *v, *w, *x;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
126 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
127 err_setstr(TypeError, "divmod() requires 2 arguments");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
130 v = gettupleitem(args, 0);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
131 w = gettupleitem(args, 1);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
132 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
133 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
134 err_setstr(TypeError, "divmod() requires numeric arguments");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
137 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
138 return NULL;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
139 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
140 DECREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
141 DECREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
142 return x;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
144
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 exec_eval(v, start)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 int start;
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 object *str = NULL, *globals = NULL, *locals = NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 int n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 if (v != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 if (is_stringobject(v))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 str = v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 else if (is_tupleobject(v) &&
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 ((n = gettuplesize(v)) == 2 || n == 3)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 str = gettupleitem(v, 0);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 globals = gettupleitem(v, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 if (n == 3)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 locals = gettupleitem(v, 2);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 if (str == NULL || !is_stringobject(str) ||
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 globals != NULL && !is_dictobject(globals) ||
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 locals != NULL && !is_dictobject(locals)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 err_setstr(TypeError,
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 "exec/eval arguments must be string[,dict[,dict]]");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 return run_string(getstringvalue(str), start, globals, locals);
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 builtin_eval(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 return exec_eval(v, eval_input);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 builtin_exec(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return exec_eval(v, file_input);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 }
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 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 builtin_float(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 /* */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
197 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
198 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
199 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
200 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
201 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
202 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
203 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 else if (is_floatobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 INCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 }
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
208 err_setstr(TypeError, "float() argument must be int, long or float");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 static object *
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
213 builtin_getattr(self, v)
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
214 object *self;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
215 object *v;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
216 {
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
217 object *name;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
218 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
219 (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
220 err_setstr(TypeError,
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
221 "getattr() arguments must be (object, string)");
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
222 return NULL;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
223 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
224 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
225 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
226
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
227 static object *
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
228 builtin_setattr(self, v)
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
229 object *self;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
230 object *v;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
231 {
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
232 object *name;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
233 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
234 (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
235 err_setstr(TypeError,
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
236 "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
237 return NULL;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
238 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
239 if (setattr(gettupleitem(v, 0),
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
240 getstringvalue(name), gettupleitem(v, 2)) != 0)
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
241 return NULL;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
242 INCREF(None);
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
243 return None;
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
244 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
245
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
246 static object *
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
247 builtin_hex(self, v)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
248 object *self;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
249 object *v;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
250 {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
251 if (v != NULL) {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
252 if (is_intobject(v)) {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
253 char buf[20];
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
254 long x = getintvalue(v);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
255 if (x >= 0)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
256 sprintf(buf, "0x%lx", x);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
257 else
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
258 sprintf(buf, "-0x%lx", -x);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
259 return newstringobject(buf);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
260 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
261 if (is_longobject(v)) {
606
c763fc64fcab long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents: 545
diff changeset
262 return long_format(v, 16);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
263 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
264 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
265 err_setstr(TypeError, "hex() requires int/long argument");
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
266 return NULL;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
267 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
268
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
269 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 builtin_input(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 FILE *in = sysgetfile("stdin", stdin);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 FILE *out = sysgetfile("stdout", stdout);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 node *n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 int err;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 object *m, *d;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 flushline();
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
280 if (v != NULL) {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
281 if (printobject(v, out, PRINT_RAW) != 0)
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
282 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
283 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 m = add_module("__main__");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 d = getmoduledict(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 return run_file(in, "<stdin>", expr_input, d, d);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 builtin_int(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 {
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 /* */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 else if (is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 INCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 }
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
301 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
302 long x;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
303 x = getlongvalue(v);
253
d4e532ee6f3d Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents: 223
diff changeset
304 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
305 return NULL;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
306 return newintobject(x);
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
307 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 else if (is_floatobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 double x = getfloatvalue(v);
253
d4e532ee6f3d Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents: 223
diff changeset
310 /* XXX should check for overflow */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 return newintobject((long)x);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 }
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
313 err_setstr(TypeError, "int() argument must be int, long or float");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 return NULL;
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 builtin_len(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 object *v;
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 long len;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 typeobject *tp;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 err_setstr(TypeError, "len() without argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 tp = v->ob_type;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 if (tp->tp_as_sequence != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 len = (*tp->tp_as_sequence->sq_length)(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 else if (tp->tp_as_mapping != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 len = (*tp->tp_as_mapping->mp_length)(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 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 err_setstr(TypeError, "len() of unsized object");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 return newintobject(len);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
341
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 static object *
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
343 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
344 object *self;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
345 object *v;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
346 {
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
347 if (v == NULL) {
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
348 /* */
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
349 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
350 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
351 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
352 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
353 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
354 INCREF(v);
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
355 return v;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
356 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
357 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
358 double x = getfloatvalue(v);
253
d4e532ee6f3d Fix conversion of double to long; stylistic changes.
Guido van Rossum <guido@python.org>
parents: 223
diff changeset
359 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
360 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
361 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
362 return NULL;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
363 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
364
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
365 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 min_max(v, sign)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 int sign;
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 int i, n, cmp;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 object *w, *x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 sequence_methods *sq;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 err_setstr(TypeError, "min() or max() without argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 sq = v->ob_type->tp_as_sequence;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 if (sq == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 err_setstr(TypeError, "min() or max() of non-sequence");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 n = (*sq->sq_length)(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 if (n == 0) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
384 err_setstr(ValueError, "min() or max() of empty sequence");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 w = (*sq->sq_item)(v, 0); /* Implies INCREF */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 for (i = 1; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 x = (*sq->sq_item)(v, i); /* Implies INCREF */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 cmp = cmpobject(x, w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 if (cmp * sign > 0) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 DECREF(w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 w = x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 DECREF(x);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 return w;
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 builtin_min(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 object *v;
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 return min_max(v, -1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
408
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 builtin_max(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 return min_max(v, 1);
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
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 static object *
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
418 builtin_oct(self, v)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
419 object *self;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
420 object *v;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
421 {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
422 if (v != NULL) {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
423 if (is_intobject(v)) {
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
424 char buf[20];
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
425 long x = getintvalue(v);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
426 if (x >= 0)
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
427 sprintf(buf, "0%lo", x);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
428 else
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
429 sprintf(buf, "-0%lo", -x);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
430 return newstringobject(buf);
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
431 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
432 if (is_longobject(v)) {
606
c763fc64fcab long_format() is now declared in longobject.h.
Guido van Rossum <guido@python.org>
parents: 545
diff changeset
433 return long_format(v, 8);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
434 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
435 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
436 err_setstr(TypeError, "oct() requires int/long argument");
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
437 return NULL;
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
438 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
439
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
440 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 builtin_open(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
445 object *name, *mode;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 if (v == NULL || !is_tupleobject(v) || gettuplesize(v) != 2 ||
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 !is_stringobject(name = gettupleitem(v, 0)) ||
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 !is_stringobject(mode = gettupleitem(v, 1))) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 err_setstr(TypeError, "open() requires 2 string arguments");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 v = newfileobject(getstringvalue(name), getstringvalue(mode));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
455
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 builtin_ord(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 if (v == NULL || !is_stringobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 err_setstr(TypeError, "ord() must have string argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 return NULL;
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 if (getstringsize(v) != 1) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
466 err_setstr(ValueError, "ord() arg must have length 1");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 return newintobject((long)(getstringvalue(v)[0] & 0xff));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
471
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 static object *
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
473 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
474 object *self;
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
475 object *args;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
476 {
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
477 object *v, *w, *x;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
478 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
479 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
480 return NULL;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
481 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
482 v = gettupleitem(args, 0);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
483 w = gettupleitem(args, 1);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
484 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
485 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
486 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
487 return NULL;
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
488 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
489 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
490 return NULL;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
491 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
492 DECREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
493 DECREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
494 return x;
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
495 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
496
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
497 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 builtin_range(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 static char *errmsg = "range() requires 1-3 int arguments";
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 int i, n;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 long ilow, ihigh, istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 if (v != NULL && is_intobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 ilow = 0; ihigh = getintvalue(v); istep = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 else if (v == NULL || !is_tupleobject(v)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 else {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 n = gettuplesize(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 if (n < 1 || n > 3) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 for (i = 0; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 if (!is_intobject(gettupleitem(v, i))) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 err_setstr(TypeError, errmsg);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 if (n == 3) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 istep = getintvalue(gettupleitem(v, 2));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 --n;
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 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 istep = 1;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 ihigh = getintvalue(gettupleitem(v, --n));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 if (n > 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 ilow = getintvalue(gettupleitem(v, 0));
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 ilow = 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 if (istep == 0) {
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
537 err_setstr(ValueError, "zero step for range()");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 /* XXX ought to check overflow of subtraction */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 if (istep > 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 n = (ihigh - ilow + istep - 1) / istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 n = (ihigh - ilow + istep + 1) / istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 if (n < 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 n = 0;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 v = newlistobject(n);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 if (v == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 for (i = 0; i < n; i++) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 object *w = newintobject(ilow);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 if (w == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 DECREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 setlistitem(v, i, w);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 ilow += istep;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 return v;
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 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 builtin_raw_input(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
564 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 FILE *out = sysgetfile("stdout", stdout);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 flushline();
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
569 if (v != NULL) {
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
570 if (printobject(v, out, PRINT_RAW) != 0)
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
571 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
572 }
182
59ffb6d4b34c Use fileobject's filegetline() to implement unlimited raw_input().
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
573 return filegetline(sysget("stdin"), -1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
575
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 builtin_reload(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 return reload_module(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
583
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 builtin_type(self, v)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 object *self;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 object *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 if (v == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 err_setstr(TypeError, "type() requres an argument");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 v = (object *)v->ob_type;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 INCREF(v);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
597
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 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
599 {"abs", builtin_abs},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
600 {"apply", builtin_apply},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
601 {"chr", builtin_chr},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
602 {"dir", builtin_dir},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
603 {"divmod", builtin_divmod},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
604 {"eval", builtin_eval},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
605 {"exec", builtin_exec},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
606 {"float", builtin_float},
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
607 {"getattr", builtin_getattr},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
608 {"hex", builtin_hex},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
609 {"input", builtin_input},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
610 {"int", builtin_int},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
611 {"len", builtin_len},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
612 {"long", builtin_long},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
613 {"max", builtin_max},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
614 {"min", builtin_min},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
615 {"oct", builtin_oct},
662
aaff650d7e13 Get rid of comment about open().
Guido van Rossum <guido@python.org>
parents: 644
diff changeset
616 {"open", builtin_open},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
617 {"ord", builtin_ord},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
618 {"pow", builtin_pow},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
619 {"range", builtin_range},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
620 {"raw_input", builtin_raw_input},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
621 {"reload", builtin_reload},
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
622 {"setattr", builtin_setattr},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
623 {"type", builtin_type},
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
624 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
627 static object *builtin_dict;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
628
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
629 object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 getbuiltin(name)
332
be3052a77b7f Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents: 301
diff changeset
631 object *name;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 {
332
be3052a77b7f Change getbuiltin interface to get the name as an object;
Guido van Rossum <guido@python.org>
parents: 301
diff changeset
633 return dict2lookup(builtin_dict, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 /* Predefined exceptions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
637
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
638 object *AttributeError;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 object *EOFError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
640 object *IOError;
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
641 object *ImportError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
642 object *IndexError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
643 object *KeyError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
644 object *KeyboardInterrupt;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 object *MemoryError;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 object *NameError;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
647 object *OverflowError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
648 object *RuntimeError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
649 object *SyntaxError;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 object *SystemError;
545
9f06c6689f0d Added SystemExit.
Guido van Rossum <guido@python.org>
parents: 505
diff changeset
651 object *SystemExit;
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
652 object *TypeError;
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
653 object *ValueError;
463
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
654 object *ZeroDivisionError;
c97f4c6ba6c6 Added new exceptions.
Guido van Rossum <guido@python.org>
parents: 404
diff changeset
655
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 static object *
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
657 newstdexception(name)
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
658 char *name;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
659 {
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
660 object *v = newstringobject(name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 fatal("no mem for new standard exception");
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 static void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 initerrors()
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 {
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
669 AttributeError = newstdexception("AttributeError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
670 EOFError = newstdexception("EOFError");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
671 IOError = newstdexception("IOError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
672 ImportError = newstdexception("ImportError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
673 IndexError = newstdexception("IndexError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
674 KeyError = newstdexception("KeyError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
675 KeyboardInterrupt = newstdexception("KeyboardInterrupt");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
676 MemoryError = newstdexception("MemoryError");
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
677 NameError = newstdexception("NameError");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
678 OverflowError = newstdexception("OverflowError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
679 RuntimeError = newstdexception("RuntimeError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
680 SyntaxError = newstdexception("SyntaxError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
681 SystemError = newstdexception("SystemError");
545
9f06c6689f0d Added SystemExit.
Guido van Rossum <guido@python.org>
parents: 505
diff changeset
682 SystemExit = newstdexception("SystemExit");
505
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
683 TypeError = newstdexception("TypeError");
095d9cf4e3e6 Sorted exceptions; added ImportError.
Guido van Rossum <guido@python.org>
parents: 495
diff changeset
684 ValueError = newstdexception("ValueError");
495
0ac9d5db41c8 Regularize exceptions.
Guido van Rossum <guido@python.org>
parents: 480
diff changeset
685 ZeroDivisionError = newstdexception("ZeroDivisionError");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
687
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 initbuiltin()
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 object *m;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 m = initmodule("builtin", builtin_methods);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 builtin_dict = getmoduledict(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 INCREF(builtin_dict);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 initerrors();
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 (void) dictinsert(builtin_dict, "None", None);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 }
301
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
698
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
699 /* 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
700 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
701 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
702 (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
703 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
704 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
705 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
706 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
707
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
708 int
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
709 coerce(pv, pw)
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
710 object **pv, **pw;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
711 {
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
712 register object *v = *pv;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
713 register object *w = *pw;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
714 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
715 INCREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
716 INCREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
717 return 0;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
718 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
719 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
720 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
721 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
722 return -1;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
723 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
724 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
725 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
726 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
727 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
728 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
729 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
730 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
731 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
732 else {
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
733 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
734 return -1;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
735 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
736 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
737 XDECREF(v);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
738 XDECREF(w);
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
739 return -1;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
740 }
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
741 *pv = v;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
742 *pw = w;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
743 return 0;
c28fabe21165 Add and use coerce() routine for mixed mode arithmetic
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
744 }