annotate Python/bltinmodule.c @ 14696:85038a024b19 legacy-trunk

This patch adds a new Python C API called PyString_AsStringAndSize() which implements the automatic conversion from Unicode to a string object using the default encoding. The new API is then put to use to have eval() and exec accept Unicode objects as code parameter. This closes bugs #110924 and #113890. As side-effect, the traditional C APIs PyString_Size() and PyString_AsString() will also accept Unicode objects as parameters.
author Marc-André Lemburg <mal@egenix.com>
date Tue, 19 Sep 2000 21:04:18 +0000
parents 2d6188c2ea56
children 25ddd3dc7a7a
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
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Built-in functions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
4 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "node.h"
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
7 #include "compile.h"
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
8 #include "eval.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
10 #include <ctype.h>
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
11
4655
c75a2f2dc220 Add unistd.h to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4595
diff changeset
12 #ifdef HAVE_UNISTD_H
c75a2f2dc220 Add unistd.h to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4595
diff changeset
13 #include <unistd.h>
c75a2f2dc220 Add unistd.h to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4595
diff changeset
14 #endif
13330
63d2887baf11 Include limits.h if we have it.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13256
diff changeset
15 #ifdef HAVE_LIMITS_H
63d2887baf11 Include limits.h if we have it.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13256
diff changeset
16 #include <limits.h>
63d2887baf11 Include limits.h if we have it.
Jack Jansen <jack.jansen@cwi.nl>
parents: 13256
diff changeset
17 #endif
4655
c75a2f2dc220 Add unistd.h to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4595
diff changeset
18
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
19 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
20 static PyObject *filterstring(PyObject *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
21 static PyObject *filtertuple (PyObject *, PyObject *);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
22
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
23 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
24 builtin___import__(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
25 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
26 char *name;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
27 PyObject *globals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
28 PyObject *locals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
29 PyObject *fromlist = NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
30
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
31 if (!PyArg_ParseTuple(args, "s|OOO:__import__",
2338
4c2f1452125d call __import__() with 4 args instead of 1
Guido van Rossum <guido@python.org>
parents: 2306
diff changeset
32 &name, &globals, &locals, &fromlist))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
33 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6198
diff changeset
34 return PyImport_ImportModuleEx(name, globals, locals, fromlist);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
35 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
36
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
37 static char import_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
38 "__import__(name, globals, locals, fromlist) -> module\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
39 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
40 Import a module. The globals are only used to determine the context;\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
41 they are not modified. The locals are currently unused. The fromlist\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
42 should be a list of names to emulate ``from name import ...'', or an\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
43 empty list to emulate ``import name''.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
44 When importing a module from a package, note that __import__('A.B', ...)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
45 returns package A when fromlist is empty, but its submodule B when\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
46 fromlist is not empty.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
47
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
48
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
49 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
50 builtin_abs(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
51 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
52 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
53
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
54 if (!PyArg_ParseTuple(args, "O:abs", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
55 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
56 return PyNumber_Absolute(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
59 static char abs_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
60 "abs(number) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
61 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
62 Return the absolute value of the argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
63
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
64
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
65 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
66 builtin_apply(PyObject *self, PyObject *args)
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
67 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
68 PyObject *func, *alist = NULL, *kwdict = NULL;
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
69 PyObject *t = NULL, *retval = NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
70
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
71 if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict))
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
72 return NULL;
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
73 if (alist != NULL) {
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
74 if (!PyTuple_Check(alist)) {
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
75 if (!PySequence_Check(alist)) {
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
76 PyErr_SetString(PyExc_TypeError,
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
77 "apply() 2nd argument must be a sequence");
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
78 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
79 }
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
80 t = PySequence_Tuple(alist);
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
81 if (t == NULL)
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
82 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
83 alist = t;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
84 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
85 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
86 if (kwdict != NULL && !PyDict_Check(kwdict)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
87 PyErr_SetString(PyExc_TypeError,
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2824
diff changeset
88 "apply() 3rd argument must be dictionary");
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
89 goto finally;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2824
diff changeset
90 }
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
91 retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
92 finally:
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
93 Py_XDECREF(t);
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
94 return retval;
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
95 }
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
96
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
97 static char apply_doc[] =
11621
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
98 "apply(object, args[, kwargs]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
99 \n\
11621
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
100 Call a callable object with positional arguments taken from the tuple args,\n\
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
101 and keyword arguments taken from the optional dictionary kwargs.\n\
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
102 Note that classes are callable, as are instances with a __call__() method.";
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
103
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
104
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
105 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
106 builtin_buffer(PyObject *self, PyObject *args)
10428
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
107 {
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
108 PyObject *ob;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
109 int offset = 0;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
110 int size = Py_END_OF_BUFFER;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
111
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
112 if ( !PyArg_ParseTuple(args, "O|ii:buffer", &ob, &offset, &size) )
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
113 return NULL;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
114 return PyBuffer_FromObject(ob, offset, size);
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
115 }
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
116
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
117 static char buffer_doc[] =
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
118 "buffer(object [, offset[, size]]) -> object\n\
10428
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
119 \n\
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
120 Creates a new buffer object which references the given object.\n\
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
121 The buffer will reference a slice of the target object from the\n\
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
122 start of the object (or at the specified offset). The slice will\n\
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
123 extend to the end of the target object (or with the specified size).";
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
124
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
125
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
126 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
127 builtin_unicode(PyObject *self, PyObject *args)
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
128 {
12368
c2d0d6744b3b Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
129 PyObject *v;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
130 char *encoding = NULL;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
131 char *errors = NULL;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
132
12368
c2d0d6744b3b Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12248
diff changeset
133 if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
134 return NULL;
13402
7fbdb581838f Fixed unicode() to use the new API PyUnicode_FromEncodedObject().
Marc-André Lemburg <mal@egenix.com>
parents: 13330
diff changeset
135 return PyUnicode_FromEncodedObject(v, encoding, errors);
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
136 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
137
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
138 static char unicode_doc[] =
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
139 "unicode(string [, encoding[, errors]]) -> object\n\
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
140 \n\
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
141 Creates a new Unicode object from the given encoded string.\n\
12647
035abb930317 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
142 encoding defaults to the current default string encoding and \n\
035abb930317 M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
143 errors, defining the error handling, to 'strict'.";
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
144
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
145
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
146 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
147 builtin_callable(PyObject *self, PyObject *args)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
148 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
149 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
150
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
151 if (!PyArg_ParseTuple(args, "O:callable", &v))
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
152 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
153 return PyInt_FromLong((long)PyCallable_Check(v));
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
154 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
155
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
156 static char callable_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
157 "callable(object) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
158 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
159 Return whether the object is callable (i.e., some kind of function).\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
160 Note that classes are callable, as are instances with a __call__() method.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
161
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
162
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
163 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
164 builtin_filter(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
165 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
166 PyObject *func, *seq, *result;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
167 PySequenceMethods *sqf;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
168 int len;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
169 register int i, j;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
170
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
171 if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq))
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
172 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
173
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
174 if (PyString_Check(seq)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
175 PyObject *r = filterstring(func, seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
176 return r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
177 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
178
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
179 if (PyTuple_Check(seq)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
180 PyObject *r = filtertuple(func, seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
181 return r;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
182 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
183
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
184 sqf = seq->ob_type->tp_as_sequence;
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
185 if (sqf == NULL || sqf->sq_length == NULL || sqf->sq_item == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
186 PyErr_SetString(PyExc_TypeError,
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
187 "argument 2 to filter() must be a sequence type");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
188 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
189 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
190
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
191 if ((len = (*sqf->sq_length)(seq)) < 0)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
192 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
193
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
194 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
195 Py_INCREF(seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
196 result = seq;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
197 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
198 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
199 if ((result = PyList_New(len)) == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
200 goto Fail_2;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
201 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
202
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
203 for (i = j = 0; ; ++i) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
204 PyObject *item, *good;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
205 int ok;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
206
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
207 if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
208 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
209 PyErr_Clear();
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
210 break;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
211 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
212 goto Fail_1;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
213 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
214
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
215 if (func == Py_None) {
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
216 good = item;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
217 Py_INCREF(good);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
218 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
219 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
220 PyObject *arg = Py_BuildValue("(O)", item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
221 if (arg == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
222 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
223 good = PyEval_CallObject(func, arg);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
224 Py_DECREF(arg);
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
225 if (good == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
226 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
227 goto Fail_1;
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
228 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
229 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
230 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
231 Py_DECREF(good);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
232 if (ok) {
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
233 if (j < len) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
234 if (PyList_SetItem(result, j++, item) < 0)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
235 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
236 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
237 else {
9991
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
238 int status = PyList_Append(result, item);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
239 j++;
9991
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
240 Py_DECREF(item);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
241 if (status < 0)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
242 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
243 }
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
244 } else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
245 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
246 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
247 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
248
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
249
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
250 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
251 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
252
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
253 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
254
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
255 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
256 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
257 Fail_2:
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
258 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
259 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
260
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
261 static char filter_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
262 "filter(function, sequence) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
263 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
264 Return a list containing those items of sequence for which function(item)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
265 is true. If function is None, return a list of items that are true.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
266
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
267
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
268 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
269 builtin_chr(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 long x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 char s[1];
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
273
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
274 if (!PyArg_ParseTuple(args, "l:chr", &x))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 if (x < 0 || x >= 256) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
277 PyErr_SetString(PyExc_ValueError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
278 "chr() arg not in range(256)");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 }
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
281 s[0] = (char)x;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
282 return PyString_FromStringAndSize(s, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
284
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
285 static char chr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
286 "chr(i) -> character\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
287 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
288 Return a string of one character with ordinal i; 0 <= i < 256.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
289
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
290
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
291 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
292 builtin_unichr(PyObject *self, PyObject *args)
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
293 {
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
294 long x;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
295 Py_UNICODE s[1];
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
296
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
297 if (!PyArg_ParseTuple(args, "l:unichr", &x))
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
298 return NULL;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
299 if (x < 0 || x >= 65536) {
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
300 PyErr_SetString(PyExc_ValueError,
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
301 "unichr() arg not in range(65536)");
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
302 return NULL;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
303 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
304 s[0] = (Py_UNICODE)x;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
305 return PyUnicode_FromUnicode(s, 1);
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
306 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
307
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
308 static char unichr_doc[] =
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
309 "unichr(i) -> Unicode character\n\
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
310 \n\
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
311 Return a Unicode string of one character with ordinal i; 0 <= i < 65536.";
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
312
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
313
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
314 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
315 builtin_cmp(PyObject *self, PyObject *args)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
316 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
317 PyObject *a, *b;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
318 int c;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
319
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
320 if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b))
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
321 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
322 if (PyObject_Cmp(a, b, &c) < 0)
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
323 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
324 return PyInt_FromLong((long)c);
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
325 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
326
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
327 static char cmp_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
328 "cmp(x, y) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
329 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
330 Return negative if x<y, zero if x==y, positive if x>y.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
331
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
332
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
333 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
334 builtin_coerce(PyObject *self, PyObject *args)
2113
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2106
diff changeset
335 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
336 PyObject *v, *w;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
337 PyObject *res;
2113
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2106
diff changeset
338
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
339 if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w))
2113
76c860a18d82 move coerce() from bltinmodule.c to object.c and implement builtin_coerce() differently
Guido van Rossum <guido@python.org>
parents: 2106
diff changeset
340 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
341 if (PyNumber_Coerce(&v, &w) < 0)
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
342 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
343 res = Py_BuildValue("(OO)", v, w);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
344 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
345 Py_DECREF(w);
944
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
346 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
347 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
348
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
349 static char coerce_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
350 "coerce(x, y) -> None or (x1, y1)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
351 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
352 When x and y can be coerced to values of the same type, return a tuple\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
353 containing the coerced values. When they can't be coerced, return None.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
354
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
355
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
356 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
357 builtin_compile(PyObject *self, PyObject *args)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
358 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
359 char *str;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
360 char *filename;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
361 char *startstr;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
362 int start;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
363
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
364 if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr))
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
365 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
366 if (strcmp(startstr, "exec") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
367 start = Py_file_input;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
368 else if (strcmp(startstr, "eval") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
369 start = Py_eval_input;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
370 else if (strcmp(startstr, "single") == 0)
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
371 start = Py_single_input;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
372 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
373 PyErr_SetString(PyExc_ValueError,
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
374 "compile() mode must be 'exec' or 'eval' or 'single'");
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
375 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
376 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
377 return Py_CompileString(str, filename, start);
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
378 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
379
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
380 static char compile_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
381 "compile(source, filename, mode) -> code object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
382 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
383 Compile the source string (a Python module, statement or expression)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
384 into a code object that can be executed by the exec statement or eval().\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
385 The filename will be used for run-time error messages.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
386 The mode must be 'exec' to compile a module, 'single' to compile a\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
387 single (interactive) statement, or 'eval' to compile an expression.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
388
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
389
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
390 #ifndef WITHOUT_COMPLEX
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
391
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
392 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
393 complex_from_string(PyObject *v)
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
394 {
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
395 extern double strtod(const char *, char **);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
396 const char *s, *start;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
397 char *end;
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
398 double x=0.0, y=0.0, z;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
399 int got_re=0, got_im=0, done=0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
400 int digit_or_dot;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
401 int sw_error=0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
402 int sign;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
403 char buffer[256]; /* For errors */
14052
275bc4f9d50d comples_from_string(): Move s_buffer[] up to the top-level function
Barry Warsaw <barry@python.org>
parents: 13943
diff changeset
404 char s_buffer[256];
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
405 int len;
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
406
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
407 if (PyString_Check(v)) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
408 s = PyString_AS_STRING(v);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
409 len = PyString_GET_SIZE(v);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
410 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
411 else if (PyUnicode_Check(v)) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
412 if (PyUnicode_GET_SIZE(v) >= sizeof(s_buffer)) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
413 PyErr_SetString(PyExc_ValueError,
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
414 "complex() literal too large to convert");
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
415 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
416 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
417 if (PyUnicode_EncodeDecimal(PyUnicode_AS_UNICODE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
418 PyUnicode_GET_SIZE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
419 s_buffer,
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
420 NULL))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
421 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
422 s = s_buffer;
13943
5780b198abc8 Clean up a couple of warnings on Win64. The downcast of the strlen size_t
Trent Mick <trentm@activestate.com>
parents: 13822
diff changeset
423 len = (int)strlen(s);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
424 }
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
425 else if (PyObject_AsCharBuffer(v, &s, &len)) {
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
426 PyErr_SetString(PyExc_TypeError,
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
427 "complex() needs a string first argument");
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
428 return NULL;
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
429 }
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
430
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
431 /* position on first nonblank */
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
432 start = s;
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
433 while (*s && isspace(Py_CHARMASK(*s)))
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
434 s++;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
435 if (s[0] == '\0') {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
436 PyErr_SetString(PyExc_ValueError,
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
437 "empty string for complex()");
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
438 return NULL;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
439 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
440
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
441 z = -1.0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
442 sign = 1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
443 do {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
444
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
445 switch (*s) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
446
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
447 case '\0':
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
448 if (s-start != len) {
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
449 PyErr_SetString(
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
450 PyExc_ValueError,
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
451 "null byte in argument for complex()");
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
452 return NULL;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
453 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
454 if(!done) sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
455 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
456
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
457 case '-':
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
458 sign = -1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
459 /* Fallthrough */
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
460 case '+':
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
461 if (done) sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
462 s++;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
463 if ( *s=='\0'||*s=='+'||*s=='-' ||
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
464 isspace(Py_CHARMASK(*s)) ) sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
465 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
467 case 'J':
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
468 case 'j':
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
469 if (got_im || done) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
470 sw_error = 1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
471 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
472 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
473 if (z<0.0) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
474 y=sign;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
475 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
476 else{
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
477 y=sign*z;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
478 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
479 got_im=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
480 s++;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
481 if (*s!='+' && *s!='-' )
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
482 done=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
483 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
484
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
485 default:
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
486 if (isspace(Py_CHARMASK(*s))) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
487 while (*s && isspace(Py_CHARMASK(*s)))
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
488 s++;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
489 if (s[0] != '\0')
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
490 sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
491 else
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
492 done = 1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
493 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
494 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
495 digit_or_dot =
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
496 (*s=='.' || isdigit(Py_CHARMASK(*s)));
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
497 if (done||!digit_or_dot) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
498 sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
499 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
500 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
501 errno = 0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
502 PyFPE_START_PROTECT("strtod", return 0)
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
503 z = strtod(s, &end) ;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
504 PyFPE_END_PROTECT(z)
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
505 if (errno != 0) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
506 sprintf(buffer,
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
507 "float() out of range: %.150s", s);
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
508 PyErr_SetString(
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
509 PyExc_ValueError,
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
510 buffer);
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
511 return NULL;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
512 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
513 s=end;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
514 if (*s=='J' || *s=='j') {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
515
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
516 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
517 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
518 if (got_re) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
519 sw_error=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
520 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
521 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
522
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
523 /* accept a real part */
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
524 x=sign*z;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
525 got_re=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
526 if (got_im) done=1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
527 z = -1.0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
528 sign = 1;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
529 break;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
530
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
531 } /* end of switch */
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
532
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
533 } while (*s!='\0' && !sw_error);
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
534
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
535 if (sw_error) {
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
536 PyErr_SetString(PyExc_ValueError,
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
537 "malformed string for complex()");
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
538 return NULL;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
539 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
540
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
541 return PyComplex_FromDoubles(x,y);
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
542 }
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
543
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
544 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
545 builtin_complex(PyObject *self, PyObject *args)
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
546 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
547 PyObject *r, *i, *tmp;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
548 PyNumberMethods *nbr, *nbi = NULL;
3766
516490c8192c Py_complex; and WITHOUT_COMPLEX added to getargs.c
Guido van Rossum <guido@python.org>
parents: 3587
diff changeset
549 Py_complex cr, ci;
5223
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
550 int own_r = 0;
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
551
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
552 i = NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
553 if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i))
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
554 return NULL;
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
555 if (PyString_Check(r) || PyUnicode_Check(r))
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
556 return complex_from_string(r);
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
557 if ((nbr = r->ob_type->tp_as_number) == NULL ||
5223
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
558 nbr->nb_float == NULL ||
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
559 (i != NULL &&
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
560 ((nbi = i->ob_type->tp_as_number) == NULL ||
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
561 nbi->nb_float == NULL))) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
562 PyErr_SetString(PyExc_TypeError,
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
563 "complex() argument can't be converted to complex");
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
564 return NULL;
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
565 }
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
566 /* XXX Hack to support classes with __complex__ method */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
567 if (PyInstance_Check(r)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
568 static PyObject *complexstr;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
569 PyObject *f;
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
570 if (complexstr == NULL) {
5010
f0cf8d275fbc Intern the string "__complex__".
Guido van Rossum <guido@python.org>
parents: 4655
diff changeset
571 complexstr = PyString_InternFromString("__complex__");
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
572 if (complexstr == NULL)
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
573 return NULL;
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
574 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
575 f = PyObject_GetAttr(r, complexstr);
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
576 if (f == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
577 PyErr_Clear();
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
578 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
579 PyObject *args = Py_BuildValue("()");
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
580 if (args == NULL)
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
581 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
582 r = PyEval_CallObject(f, args);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
583 Py_DECREF(args);
9979
489ef7f64041 builtin_complex(): Nailed memory leak. This one's in the instance
Barry Warsaw <barry@python.org>
parents: 9849
diff changeset
584 Py_DECREF(f);
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
585 if (r == NULL)
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
586 return NULL;
5223
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
587 own_r = 1;
4595
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
588 }
f8f3da3fdaed Support __complex__ method on instances, for complex() conversion.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
589 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
590 if (PyComplex_Check(r)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
591 cr = ((PyComplexObject*)r)->cval;
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7678
diff changeset
592 if (own_r) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
593 Py_DECREF(r);
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7678
diff changeset
594 }
5223
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
595 }
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
596 else {
3962
f08d7f368ebf Include mymath.h instead of declaring prototypes for math functions.
Guido van Rossum <guido@python.org>
parents: 3862
diff changeset
597 tmp = (*nbr->nb_float)(r);
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7678
diff changeset
598 if (own_r) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
599 Py_DECREF(r);
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 7678
diff changeset
600 }
3962
f08d7f368ebf Include mymath.h instead of declaring prototypes for math functions.
Guido van Rossum <guido@python.org>
parents: 3862
diff changeset
601 if (tmp == NULL)
f08d7f368ebf Include mymath.h instead of declaring prototypes for math functions.
Guido van Rossum <guido@python.org>
parents: 3862
diff changeset
602 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
603 cr.real = PyFloat_AsDouble(tmp);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
604 Py_DECREF(tmp);
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
605 cr.imag = 0.0;
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
606 }
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
607 if (i == NULL) {
10466
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
608 ci.real = 0.0;
36ba4dbff686 Patch by Nick and Stephanie Lockwood to implement complex() with a string
Guido van Rossum <guido@python.org>
parents: 10428
diff changeset
609 ci.imag = 0.0;
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
610 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
611 else if (PyComplex_Check(i))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
612 ci = ((PyComplexObject*)i)->cval;
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
613 else {
5223
bc8832b52b50 1. Add string conversions to int(), long(), float(). (Not to complex()!)
Guido van Rossum <guido@python.org>
parents: 5182
diff changeset
614 tmp = (*nbi->nb_float)(i);
3962
f08d7f368ebf Include mymath.h instead of declaring prototypes for math functions.
Guido van Rossum <guido@python.org>
parents: 3862
diff changeset
615 if (tmp == NULL)
f08d7f368ebf Include mymath.h instead of declaring prototypes for math functions.
Guido van Rossum <guido@python.org>
parents: 3862
diff changeset
616 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
617 ci.real = PyFloat_AsDouble(tmp);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
618 Py_DECREF(tmp);
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
619 ci.imag = 0.;
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
620 }
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
621 cr.real -= ci.imag;
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
622 cr.imag += ci.real;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
623 return PyComplex_FromCComplex(cr);
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
624 }
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
625
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
626 static char complex_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
627 "complex(real[, imag]) -> complex number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
628 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
629 Create a complex number from a real part and an optional imaginary part.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
630 This is equivalent to (real + imag*1j) where imag defaults to 0.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
631
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
632
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
633 #endif
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
634
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
635 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
636 builtin_dir(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 {
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
638 static char *attrlist[] = {"__members__", "__methods__", NULL};
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
639 PyObject *v = NULL, *l = NULL, *m = NULL;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
640 PyObject *d, *x;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
641 int i;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
642 char **s;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
643
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
644 if (!PyArg_ParseTuple(args, "|O:dir", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
645 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 if (v == NULL) {
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
647 x = PyEval_GetLocals();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
648 if (x == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
649 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
650 l = PyMapping_Keys(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
651 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
652 goto error;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
655 d = PyObject_GetAttrString(v, "__dict__");
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
656 if (d == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
657 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
658 else {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
659 l = PyMapping_Keys(d);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
660 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
661 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
662 Py_DECREF(d);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
663 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
664 if (l == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
665 l = PyList_New(0);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
666 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
667 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
668 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
669 for (s = attrlist; *s != NULL; s++) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
670 m = PyObject_GetAttrString(v, *s);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
671 if (m == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
672 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
673 continue;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
674 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
675 for (i = 0; ; i++) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
676 x = PySequence_GetItem(m, i);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
677 if (x == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
678 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
679 break;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
680 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
681 if (PyList_Append(l, x) != 0) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
682 Py_DECREF(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
683 Py_DECREF(m);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
684 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
685 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
686 Py_DECREF(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
687 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
688 Py_DECREF(m);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
690 }
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
691 if (PyList_Sort(l) != 0)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
692 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
693 return l;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
694 error:
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
695 Py_XDECREF(l);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
696 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
698
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
699 static char dir_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
700 "dir([object]) -> list of strings\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
701 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
702 Return an alphabetized list of names comprising (some of) the attributes\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
703 of the given object. Without an argument, the names in the current scope\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
704 are listed. With an instance argument, only the instance attributes are\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
705 returned. With a class argument, attributes of the base class are not\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
706 returned. For other types or arguments, this may list members or methods.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
707
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
708
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
709 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
710 builtin_divmod(PyObject *self, PyObject *args)
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
711 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
712 PyObject *v, *w;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
713
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
714 if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
715 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
716 return PyNumber_Divmod(v, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
718
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
719 static char divmod_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
720 "divmod(x, y) -> (div, mod)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
721 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
722 Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
723
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
724
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
725 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
726 builtin_eval(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
728 PyObject *cmd;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
729 PyObject *globals = Py_None, *locals = Py_None;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
730 char *str;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
731
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
732 if (!PyArg_ParseTuple(args, "O|O!O!:eval",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
733 &cmd,
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
734 &PyDict_Type, &globals,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
735 &PyDict_Type, &locals))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
736 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
737 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
738 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
739 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
740 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
741 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
742 else if (locals == Py_None)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
743 locals = globals;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
744 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
745 if (PyDict_SetItemString(globals, "__builtins__",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
746 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
747 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
748 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
749 if (PyCode_Check(cmd))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
750 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
751 if (!PyString_Check(cmd) &&
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
752 !PyUnicode_Check(cmd)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
753 PyErr_SetString(PyExc_TypeError,
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
754 "eval() argument 1 must be string or code object");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
756 }
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
757 if (PyString_AsStringAndSize(cmd, &str, NULL))
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
758 return NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
759 while (*str == ' ' || *str == '\t')
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
760 str++;
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
761 return PyRun_String(str, Py_eval_input, globals, locals);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
762 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
763
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
764 static char eval_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
765 "eval(source[, globals[, locals]]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
766 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
767 Evaluate the source in the context of globals and locals.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
768 The source may be a string representing a Python expression\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
769 or a code object as returned by compile().\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
770 The globals and locals are dictionaries, defaulting to the current\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
771 globals and locals. If only globals is given, locals defaults to it.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
772
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
773
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
774 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
775 builtin_execfile(PyObject *self, PyObject *args)
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
776 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
777 char *filename;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
778 PyObject *globals = Py_None, *locals = Py_None;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
779 PyObject *res;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
780 FILE* fp;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
781
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
782 if (!PyArg_ParseTuple(args, "s|O!O!:execfile",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
783 &filename,
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
784 &PyDict_Type, &globals,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
785 &PyDict_Type, &locals))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
786 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
787 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
788 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
789 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
790 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
791 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
792 else if (locals == Py_None)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
793 locals = globals;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
794 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
795 if (PyDict_SetItemString(globals, "__builtins__",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
796 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
797 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
798 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
799 Py_BEGIN_ALLOW_THREADS
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
800 fp = fopen(filename, "r");
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
801 Py_END_ALLOW_THREADS
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
802 if (fp == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
803 PyErr_SetFromErrno(PyExc_IOError);
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
804 return NULL;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
805 }
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14052
diff changeset
806 res = PyRun_FileEx(fp, filename, Py_file_input, globals, locals, 1);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
807 return res;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
808 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
809
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
810 static char execfile_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
811 "execfile(filename[, globals[, locals]])\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
812 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
813 Read and execute a Python script from a file.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
814 The globals and locals are dictionaries, defaulting to the current\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
815 globals and locals. If only globals is given, locals defaults to it.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
816
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
817
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
818 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
819 builtin_getattr(PyObject *self, PyObject *args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
820 {
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
821 PyObject *v, *result, *dflt = NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
822 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
823
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
824 if (!PyArg_ParseTuple(args, "OO|O:getattr", &v, &name, &dflt))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
825 return NULL;
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
826 result = PyObject_GetAttr(v, name);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
827 if (result == NULL && dflt != NULL) {
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
828 PyErr_Clear();
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
829 Py_INCREF(dflt);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
830 result = dflt;
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
831 }
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
832 return result;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
833 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
834
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
835 static char getattr_doc[] =
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
836 "getattr(object, name[, default]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
837 \n\
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
838 Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
839 When a default argument is given, it is returned when the attribute doesn't\n\
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
840 exist; without it, an exception is raised in that case.";
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
841
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
842
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
843 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
844 builtin_globals(PyObject *self, PyObject *args)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
845 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
846 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
847
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
848 if (!PyArg_ParseTuple(args, ":globals"))
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
849 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
850 d = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
851 Py_INCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
852 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
853 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
854
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
855 static char globals_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
856 "globals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
857 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
858 Return the dictionary containing the current scope's global variables.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
859
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
860
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
861 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
862 builtin_hasattr(PyObject *self, PyObject *args)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
863 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
864 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
865 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
866
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
867 if (!PyArg_ParseTuple(args, "OO:hasattr", &v, &name))
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
868 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
869 v = PyObject_GetAttr(v, name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
870 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
871 PyErr_Clear();
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
872 Py_INCREF(Py_False);
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
873 return Py_False;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
874 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
875 Py_DECREF(v);
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
876 Py_INCREF(Py_True);
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
877 return Py_True;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
878 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
879
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
880 static char hasattr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
881 "hasattr(object, name) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
882 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
883 Return whether the object has an attribute with the given name.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
884 (This is done by calling getattr(object, name) and catching exceptions.)";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
885
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
886
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
887 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
888 builtin_id(PyObject *self, PyObject *args)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
889 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
890 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
891
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
892 if (!PyArg_ParseTuple(args, "O:id", &v))
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
893 return NULL;
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
894 return PyLong_FromVoidPtr(v);
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
895 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
896
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
897 static char id_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
898 "id(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
899 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
900 Return the identity of an object. This is guaranteed to be unique among\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
901 simultaneously existing objects. (Hint: it's the object's memory address.)";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
902
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
903
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
904 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
905 builtin_map(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
906 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
907 typedef struct {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
908 PyObject *seq;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
909 PySequenceMethods *sqf;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
910 int len;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
911 } sequence;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
912
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
913 PyObject *func, *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
914 sequence *seqs = NULL, *sqp;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
915 int n, len;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
916 register int i, j;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
917
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
918 n = PyTuple_Size(args);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
919 if (n < 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
920 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
921 "map() requires at least two args");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
922 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
923 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
924
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
925 func = PyTuple_GetItem(args, 0);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
926 n--;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
927
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
928 if (func == Py_None && n == 1) {
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
929 /* map(None, S) is the same as list(S). */
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
930 return PySequence_List(PyTuple_GetItem(args, 1));
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
931 }
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
932
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
933 if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
934 PyErr_NoMemory();
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
935 goto Fail_2;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
936 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
937
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
938 for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
939 int curlen;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
940 PySequenceMethods *sqf;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
941
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
942 if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
943 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
944
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
945 sqp->sqf = sqf = sqp->seq->ob_type->tp_as_sequence;
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
946 if (sqf == NULL ||
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
947 sqf->sq_length == NULL ||
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
948 sqf->sq_item == NULL)
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
949 {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
950 static char errmsg[] =
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
951 "argument %d to map() must be a sequence object";
5388
13a30cea8dbf Avoid sprintf buffer overflow if more than 9999 arguments.
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
952 char errbuf[sizeof(errmsg) + 25];
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
953
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
954 sprintf(errbuf, errmsg, i+2);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
955 PyErr_SetString(PyExc_TypeError, errbuf);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
956 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
957 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
958
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
959 if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
960 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
961
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
962 if (curlen > len)
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
963 len = curlen;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
964 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
965
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
966 if ((result = (PyObject *) PyList_New(len)) == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
967 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
968
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
969 for (i = 0; ; ++i) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
970 PyObject *alist, *item=NULL, *value;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
971 int any = 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
972
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
973 if (func == Py_None && n == 1)
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
974 alist = NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
975 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
976 if ((alist = PyTuple_New(n)) == NULL)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
977 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
978 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
979
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
980 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
981 if (sqp->len < 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
982 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
983 item = Py_None;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
984 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
985 else {
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
986 item = (*sqp->sqf->sq_item)(sqp->seq, i);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
987 if (item == NULL) {
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
988 if (PyErr_ExceptionMatches(
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
989 PyExc_IndexError))
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
990 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
991 PyErr_Clear();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
992 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
993 item = Py_None;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
994 sqp->len = -1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
995 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
996 else {
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
997 goto Fail_0;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
998 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
999 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1000 else
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1001 any = 1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1002
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1003 }
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1004 if (!alist)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1005 break;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1006 if (PyTuple_SetItem(alist, j, item) < 0) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1007 Py_DECREF(item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1008 goto Fail_0;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1009 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1010 continue;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1011
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1012 Fail_0:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1013 Py_XDECREF(alist);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1014 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1015 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1016
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1017 if (!alist)
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1018 alist = item;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1019
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1020 if (!any) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1021 Py_DECREF(alist);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1022 break;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1023 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1024
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1025 if (func == Py_None)
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1026 value = alist;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1027 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1028 value = PyEval_CallObject(func, alist);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1029 Py_DECREF(alist);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1030 if (value == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1031 goto Fail_1;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1032 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1033 if (i >= len) {
9991
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
1034 int status = PyList_Append(result, value);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
1035 Py_DECREF(value);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
1036 if (status < 0)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1037 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1038 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1039 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1040 if (PyList_SetItem(result, i, value) < 0)
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1041 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1042 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1043 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1044
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
1045 if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
1046 goto Fail_1;
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
1047
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1048 PyMem_DEL(seqs);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1049 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1050
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1051 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1052 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1053 Fail_2:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1054 if (seqs) PyMem_DEL(seqs);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1055 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1056 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1057
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1058 static char map_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1059 "map(function, sequence[, sequence, ...]) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1060 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1061 Return a list of the results of applying the function to the items of\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1062 the argument sequence(s). If more than one sequence is given, the\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1063 function is called with an argument list consisting of the corresponding\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1064 item of each sequence, substituting None for missing values when not all\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1065 sequences have the same length. If the function is None, return a list of\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1066 the items of the sequence (or a list of tuples if more than one sequence).";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1067
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1068
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1069 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1070 builtin_setattr(PyObject *self, PyObject *args)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1071 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1072 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1073 PyObject *name;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1074 PyObject *value;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1075
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1076 if (!PyArg_ParseTuple(args, "OOO:setattr", &v, &name, &value))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1077 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1078 if (PyObject_SetAttr(v, name, value) != 0)
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1079 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1080 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1081 return Py_None;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1082 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1083
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1084 static char setattr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1085 "setattr(object, name, value)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1086 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1087 Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1088 ``x.y = v''.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1089
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1090
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1091 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1092 builtin_delattr(PyObject *self, PyObject *args)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1093 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1094 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1095 PyObject *name;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1096
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
1097 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1098 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1099 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1100 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1101 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1102 return Py_None;
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1103 }
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1104
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1105 static char delattr_doc[] =
9514
a126107c433a Fix cosmetic bug in delattr docstring discovered by JvR.
Guido van Rossum <guido@python.org>
parents: 9188
diff changeset
1106 "delattr(object, name)\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1107 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1108 Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1109 ``del x.y''.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1110
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1111
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1112 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1113 builtin_hash(PyObject *self, PyObject *args)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1114 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1115 PyObject *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1116 long x;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1117
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1118 if (!PyArg_ParseTuple(args, "O:hash", &v))
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1119 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1120 x = PyObject_Hash(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1121 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1122 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1123 return PyInt_FromLong(x);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1124 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1125
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1126 static char hash_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1127 "hash(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1128 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1129 Return a hash value for the object. Two objects with the same value have\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1130 the same hash value. The reverse is not necessarily true, but likely.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1131
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1132
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1133 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1134 builtin_hex(PyObject *self, PyObject *args)
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1135 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1136 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1137 PyNumberMethods *nb;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1138
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1139 if (!PyArg_ParseTuple(args, "O:hex", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1140 return NULL;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1141
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1142 if ((nb = v->ob_type->tp_as_number) == NULL ||
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1143 nb->nb_hex == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1144 PyErr_SetString(PyExc_TypeError,
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1145 "hex() argument can't be converted to hex");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1146 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1147 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1148 return (*nb->nb_hex)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1149 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1150
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1151 static char hex_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1152 "hex(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1153 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1154 Return the hexadecimal representation of an integer or long integer.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1155
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1156
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
1157 static PyObject *builtin_raw_input(PyObject *, PyObject *);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
1158
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1159 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1160 builtin_input(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1161 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1162 PyObject *line;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1163 char *str;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1164 PyObject *res;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1165 PyObject *globals, *locals;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1166
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1167 line = builtin_raw_input(self, args);
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
1168 if (line == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
1169 return line;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1170 if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1171 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1172 while (*str == ' ' || *str == '\t')
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1173 str++;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1174 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1175 locals = PyEval_GetLocals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1176 if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1177 if (PyDict_SetItemString(globals, "__builtins__",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1178 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
1179 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
1180 }
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
1181 res = PyRun_String(str, Py_eval_input, globals, locals);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1182 Py_DECREF(line);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1183 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1186 static char input_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1187 "input([prompt]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1188 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1189 Equivalent to eval(raw_input(prompt)).";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1190
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1191
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1192 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1193 builtin_intern(PyObject *self, PyObject *args)
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1194 {
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1195 PyObject *s;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
1196 if (!PyArg_ParseTuple(args, "S:intern", &s))
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1197 return NULL;
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1198 Py_INCREF(s);
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1199 PyString_InternInPlace(&s);
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1200 return s;
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1201 }
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1202
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1203 static char intern_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1204 "intern(string) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1205 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1206 ``Intern'' the given string. This enters the string in the (global)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1207 table of interned strings whose purpose is to speed up dictionary lookups.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1208 Return the string itself or the previously interned string object with the\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1209 same value.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1210
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1211
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1212 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1213 builtin_int(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1215 PyObject *v;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1216 int base = -909; /* unlikely! */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1217
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1218 if (!PyArg_ParseTuple(args, "O|i:int", &v, &base))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1219 return NULL;
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1220 if (base == -909)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1221 return PyNumber_Int(v);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1222 else if (PyString_Check(v))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1223 return PyInt_FromString(PyString_AS_STRING(v), NULL, base);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1224 else if (PyUnicode_Check(v))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1225 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1226 PyUnicode_GET_SIZE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1227 base);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1228 else {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1229 PyErr_SetString(PyExc_TypeError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1230 "can't convert non-string with explicit base");
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1231 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1232 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1235 static char int_doc[] =
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1236 "int(x[, base]) -> integer\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1237 \n\
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1238 Convert a string or number to an integer, if possible. A floating point\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1239 argument will be truncated towards zero (this does not include a string\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1240 representation of a floating point number!) When converting a string, use\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1241 the optional base. It is an error to supply a base when converting a\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1242 non-string.";
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1243
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1244
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1245 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1246 builtin_long(PyObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1247 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1248 PyObject *v;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1249 int base = -909; /* unlikely! */
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1250
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1251 if (!PyArg_ParseTuple(args, "O|i:long", &v, &base))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1252 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1253 if (base == -909)
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1254 return PyNumber_Long(v);
12248
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1255 else if (PyString_Check(v))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1256 return PyLong_FromString(PyString_AS_STRING(v), NULL, base);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1257 else if (PyUnicode_Check(v))
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1258 return PyLong_FromUnicode(PyUnicode_AS_UNICODE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1259 PyUnicode_GET_SIZE(v),
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1260 base);
70154e2e9c89 Marc-Andre's third try at this bulk patch seems to work (except that
Guido van Rossum <guido@python.org>
parents: 11973
diff changeset
1261 else {
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1262 PyErr_SetString(PyExc_TypeError,
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1263 "can't convert non-string with explicit base");
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1264 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1265 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1266 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1267
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1268 static char long_doc[] =
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1269 "long(x) -> long integer\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1270 long(x, base) -> long integer\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1271 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1272 Convert a string or number to a long integer, if possible. A floating\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1273 point argument will be truncated towards zero (this does not include a\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1274 string representation of a floating point number!) When converting a\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1275 string, use the given base. It is an error to supply a base when\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1276 converting a non-string.";
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1277
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1278
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1279 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1280 builtin_float(PyObject *self, PyObject *args)
11383
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1281 {
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1282 PyObject *v;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1283
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1284 if (!PyArg_ParseTuple(args, "O:float", &v))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1285 return NULL;
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1286 if (PyString_Check(v))
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1287 return PyFloat_FromString(v, NULL);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1288 return PyNumber_Float(v);
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1289 }
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1290
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1291 static char float_doc[] =
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1292 "float(x) -> floating point number\n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1293 \n\
aca7b5eaf5e8 Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents: 11132
diff changeset
1294 Convert a string or number to a floating point number, if possible.";
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1295
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1296
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1297 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1298 builtin_len(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1299 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1300 PyObject *v;
8589
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
1301 long res;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1302
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1303 if (!PyArg_ParseTuple(args, "O:len", &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 return NULL;
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13447
diff changeset
1305 res = PyObject_Size(v);
8589
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
1306 if (res < 0 && PyErr_Occurred())
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
1307 return NULL;
97dc6aa532e7 Fix a stupid little bug: len() of an unsized returns -1 and leaves an
Guido van Rossum <guido@python.org>
parents: 8581
diff changeset
1308 return PyInt_FromLong(res);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1309 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1311 static char len_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1312 "len(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1313 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1314 Return the number of items of a sequence or mapping.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1315
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1316
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1317 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1318 builtin_list(PyObject *self, PyObject *args)
3493
03c6c18325ea Add list() method, analogous to tuple().
Guido van Rossum <guido@python.org>
parents: 3365
diff changeset
1319 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1320 PyObject *v;
3493
03c6c18325ea Add list() method, analogous to tuple().
Guido van Rossum <guido@python.org>
parents: 3365
diff changeset
1321
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1322 if (!PyArg_ParseTuple(args, "O:list", &v))
3493
03c6c18325ea Add list() method, analogous to tuple().
Guido van Rossum <guido@python.org>
parents: 3365
diff changeset
1323 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1324 return PySequence_List(v);
3493
03c6c18325ea Add list() method, analogous to tuple().
Guido van Rossum <guido@python.org>
parents: 3365
diff changeset
1325 }
03c6c18325ea Add list() method, analogous to tuple().
Guido van Rossum <guido@python.org>
parents: 3365
diff changeset
1326
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1327 static char list_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1328 "list(sequence) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1329 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1330 Return a new list whose items are the same as those of the argument sequence.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1331
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1332
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1333 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1334 builtin_slice(PyObject *self, PyObject *args)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1335 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1336 PyObject *start, *stop, *step;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1337
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1338 start = stop = step = NULL;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1339
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1340 if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1341 return NULL;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1342
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1343 /* This swapping of stop and start is to maintain similarity with
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1344 range(). */
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1345 if (stop == NULL) {
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1346 stop = start;
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1347 start = NULL;
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1348 }
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1349 return PySlice_New(start, stop, step);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1350 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1351
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1352 static char slice_doc[] =
11132
41a46978a444 Fixed order of parameters in slice() docstring. The Library Reference
Fred Drake <fdrake@acm.org>
parents: 11030
diff changeset
1353 "slice([start,] stop[, step]) -> slice object\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1354 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1355 Create a slice object. This is used for slicing by the Numeric extensions.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1356
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1357
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1358 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1359 builtin_locals(PyObject *self, PyObject *args)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1360 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1361 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1362
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
1363 if (!PyArg_ParseTuple(args, ":locals"))
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1364 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1365 d = PyEval_GetLocals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1366 Py_INCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1367 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1368 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1369
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1370 static char locals_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1371 "locals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1372 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1373 Return the dictionary containing the current scope's local variables.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1374
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1375
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1376 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1377 min_max(PyObject *args, int sign)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 {
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1379 int i;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1380 PyObject *v, *w, *x;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1381 PySequenceMethods *sq;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1382
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1383 if (PyTuple_Size(args) > 1)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1384 v = args;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1385 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 sq = v->ob_type->tp_as_sequence;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1388 if (sq == NULL || sq->sq_item == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1389 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1390 "min() or max() of non-sequence");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1391 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1392 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1393 w = NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1394 for (i = 0; ; i++) {
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1395 x = (*sq->sq_item)(v, i); /* Implies INCREF */
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1396 if (x == NULL) {
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1397 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1398 PyErr_Clear();
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1399 break;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1400 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1401 Py_XDECREF(w);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1402 return NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1403 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1404 if (w == NULL)
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1405 w = x;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1406 else {
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1407 int c = PyObject_Compare(x, w);
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1408 if (c && PyErr_Occurred()) {
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1409 Py_DECREF(x);
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1410 Py_XDECREF(w);
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1411 return NULL;
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1412 }
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1413 if (c * sign > 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1414 Py_DECREF(w);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1415 w = x;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1416 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1417 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1418 Py_DECREF(x);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1419 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1420 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1421 if (w == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1422 PyErr_SetString(PyExc_ValueError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1423 "min() or max() of empty sequence");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 return w;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1425 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1427 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1428 builtin_min(PyObject *self, PyObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1429 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1430 return min_max(v, -1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1431 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1432
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1433 static char min_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1434 "min(sequence) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1435 min(a, b, c, ...) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1436 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1437 With a single sequence argument, return its smallest item.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1438 With two or more arguments, return the smallest argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1439
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1440
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1441 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1442 builtin_max(PyObject *self, PyObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 return min_max(v, 1);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1445 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1446
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1447 static char max_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1448 "max(sequence) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1449 max(a, b, c, ...) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1450 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1451 With a single sequence argument, return its largest item.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1452 With two or more arguments, return the largest argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1453
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1454
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1455 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1456 builtin_oct(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1457 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1458 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1459 PyNumberMethods *nb;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1460
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1461 if (!PyArg_ParseTuple(args, "O:oct", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1462 return NULL;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1463 if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1464 nb->nb_oct == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1465 PyErr_SetString(PyExc_TypeError,
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1466 "oct() argument can't be converted to oct");
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1467 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1468 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1469 return (*nb->nb_oct)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1470 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1471
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1472 static char oct_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1473 "oct(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1474 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1475 Return the octal representation of an integer or long integer.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1476
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1477
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1478 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1479 builtin_open(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1480 {
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1481 char *name;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1482 char *mode = "r";
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1483 int bufsize = -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1484 PyObject *f;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1485
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1486 if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1488 f = PyFile_FromString(name, mode);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1489 if (f != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1490 PyFile_SetBufSize(f, bufsize);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1491 return f;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1492 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1493
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1494 static char open_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1495 "open(filename[, mode[, buffering]]) -> file object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1496 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1497 Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1498 writing or appending. The file will be created if it doesn't exist\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1499 when opened for writing or appending; it will be truncated when\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1500 opened for writing. Add a 'b' to the mode for binary files.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1501 Add a '+' to the mode to allow simultaneous reading and writing.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1502 If the buffering argument is given, 0 means unbuffered, 1 means line\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1503 buffered, and larger numbers specify the buffer size.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1504
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1505
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1506 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1507 builtin_ord(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 {
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1509 PyObject *obj;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1510 long ord;
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1511 int size;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1512
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1513 if (!PyArg_ParseTuple(args, "O:ord", &obj))
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1514 return NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1515
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1516 if (PyString_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1517 size = PyString_GET_SIZE(obj);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1518 if (size == 1)
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1519 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1520 } else if (PyUnicode_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1521 size = PyUnicode_GET_SIZE(obj);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1522 if (size == 1)
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1523 ord = (long)*PyUnicode_AS_UNICODE(obj);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1524 } else {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1525 PyErr_Format(PyExc_TypeError,
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
1526 "expected string or Unicode character, " \
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1527 "%.200s found", obj->ob_type->tp_name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528 return NULL;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1529 }
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1530 if (size == 1)
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1531 return PyInt_FromLong(ord);
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1532
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1533 PyErr_Format(PyExc_TypeError,
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1534 "expected a character, length-%d string found",
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1535 size);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1536 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1537 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1539 static char ord_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1540 "ord(c) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1541 \n\
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
1542 Return the integer ordinal of a one character string.";
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1543
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1544
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1545 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1546 builtin_pow(PyObject *self, PyObject *args)
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1547 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1548 PyObject *v, *w, *z = Py_None;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1549
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1550 if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1551 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1552 return PyNumber_Power(v, w, z);
223
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1553 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1554
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1555 static char pow_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1556 "pow(x, y[, z]) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1557 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1558 With two arguments, equivalent to x**y. With three arguments,\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1559 equivalent to (x**y) % z, but may be more efficient (e.g. for longs).";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1560
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1561
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1562 /* Return number of items in range/xrange (lo, hi, step). step > 0
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1563 * required. Return a value < 0 if & only if the true value is too
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1564 * large to fit in a signed long.
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1565 */
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1566 static long
13679
61f65de19e1e Another missed ansification.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1567 get_len_of_range(long lo, long hi, long step)
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1568 {
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1569 /* -------------------------------------------------------------
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1570 If lo >= hi, the range is empty.
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1571 Else if n values are in the range, the last one is
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1572 lo + (n-1)*step, which must be <= hi-1. Rearranging,
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1573 n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1574 the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1575 the RHS is non-negative and so truncation is the same as the
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1576 floor. Letting M be the largest positive long, the worst case
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1577 for the RHS numerator is hi=M, lo=-M-1, and then
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1578 hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1579 precision to compute the RHS exactly.
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1580 ---------------------------------------------------------------*/
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1581 long n = 0;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1582 if (lo < hi) {
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1583 unsigned long uhi = (unsigned long)hi;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1584 unsigned long ulo = (unsigned long)lo;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1585 unsigned long diff = uhi - ulo - 1;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1586 n = (long)(diff / (unsigned long)step + 1);
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1587 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1588 return n;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1589 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1590
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1591 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1592 builtin_range(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1593 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1594 long ilow = 0, ihigh = 0, istep = 1;
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1595 long bign;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1596 int i, n;
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1597
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1598 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1599
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1600 if (PyTuple_Size(args) <= 1) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1601 if (!PyArg_ParseTuple(args,
2153
e54b8654f938 fix Alpha bug in (x)range; different __builtins__ initialization
Guido van Rossum <guido@python.org>
parents: 2120
diff changeset
1602 "l;range() requires 1-3 int arguments",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1603 &ihigh))
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1604 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1605 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1606 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1607 if (!PyArg_ParseTuple(args,
2153
e54b8654f938 fix Alpha bug in (x)range; different __builtins__ initialization
Guido van Rossum <guido@python.org>
parents: 2120
diff changeset
1608 "ll|l;range() requires 1-3 int arguments",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1609 &ilow, &ihigh, &istep))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1610 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1611 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1612 if (istep == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1613 PyErr_SetString(PyExc_ValueError, "zero step for range()");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1614 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1615 }
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1616 if (istep > 0)
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1617 bign = get_len_of_range(ilow, ihigh, istep);
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1618 else
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1619 bign = get_len_of_range(ihigh, ilow, -istep);
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1620 n = (int)bign;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1621 if (bign < 0 || (long)n != bign) {
9849
5e9ef9d8575b Avoid overflow if possible in calculations for range(); report
Guido van Rossum <guido@python.org>
parents: 9567
diff changeset
1622 PyErr_SetString(PyExc_OverflowError,
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1623 "range() has too many items");
9849
5e9ef9d8575b Avoid overflow if possible in calculations for range(); report
Guido van Rossum <guido@python.org>
parents: 9567
diff changeset
1624 return NULL;
5e9ef9d8575b Avoid overflow if possible in calculations for range(); report
Guido van Rossum <guido@python.org>
parents: 9567
diff changeset
1625 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1626 v = PyList_New(n);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1627 if (v == NULL)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1628 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1629 for (i = 0; i < n; i++) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1630 PyObject *w = PyInt_FromLong(ilow);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1631 if (w == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1632 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1633 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1634 }
8138
9d0310143600 Fred's right -- we need PyList_SET_ITEM().
Guido van Rossum <guido@python.org>
parents: 8132
diff changeset
1635 PyList_SET_ITEM(v, i, w);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1636 ilow += istep;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1637 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1638 return v;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1639 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1640
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1641 static char range_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1642 "range([start,] stop[, step]) -> list of integers\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1643 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1644 Return a list containing an arithmetic progression of integers.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1645 range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1646 When step is given, it specifies the increment (or decrement).\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1647 For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1648 These are exactly the valid indices for a list of 4 elements.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1649
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1650
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1651 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1652 builtin_xrange(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1653 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1654 long ilow = 0, ihigh = 0, istep = 1;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1655 long n;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1656
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1657 if (PyTuple_Size(args) <= 1) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1658 if (!PyArg_ParseTuple(args,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1659 "l;xrange() requires 1-3 int arguments",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1660 &ihigh))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1661 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1662 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1663 else {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1664 if (!PyArg_ParseTuple(args,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1665 "ll|l;xrange() requires 1-3 int arguments",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1666 &ilow, &ihigh, &istep))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1667 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1668 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1669 if (istep == 0) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1670 PyErr_SetString(PyExc_ValueError, "zero step for xrange()");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1049
diff changeset
1671 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1672 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1673 if (istep > 0)
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1674 n = get_len_of_range(ilow, ihigh, istep);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1675 else
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1676 n = get_len_of_range(ihigh, ilow, -istep);
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1677 if (n < 0) {
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1678 PyErr_SetString(PyExc_OverflowError,
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1679 "xrange() has more than sys.maxint items");
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1680 return NULL;
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1681 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1682 return PyRange_New(ilow, n, istep, 1);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1683 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1684
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1685 static char xrange_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1686 "xrange([start,] stop[, step]) -> xrange object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1687 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1688 Like range(), but instead of returning a list, returns an object that\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1689 generates the numbers in the range on demand. This is slightly slower\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1690 than range() but more memory efficient.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1691
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1692
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1693 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1694 builtin_raw_input(PyObject *self, PyObject *args)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1695 {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1696 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1697 PyObject *f;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1698
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1699 if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1700 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1701 if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin &&
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1702 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1703 isatty(fileno(stdin)) && isatty(fileno(stdout))) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1704 PyObject *po;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1705 char *prompt;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1706 char *s;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1707 PyObject *result;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1708 if (v != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1709 po = PyObject_Str(v);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1710 if (po == NULL)
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1711 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1712 prompt = PyString_AsString(po);
8560
46fd630dce63 In raw_input(prompt), make sure that str(prompt) really a string
Guido van Rossum <guido@python.org>
parents: 8451
diff changeset
1713 if (prompt == NULL)
46fd630dce63 In raw_input(prompt), make sure that str(prompt) really a string
Guido van Rossum <guido@python.org>
parents: 8451
diff changeset
1714 return NULL;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1715 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1716 else {
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1717 po = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1718 prompt = "";
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1719 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1720 s = PyOS_Readline(prompt);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1721 Py_XDECREF(po);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1722 if (s == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1723 PyErr_SetNone(PyExc_KeyboardInterrupt);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1724 return NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1725 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1726 if (*s == '\0') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1727 PyErr_SetNone(PyExc_EOFError);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1728 result = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1729 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1730 else { /* strip trailing '\n' */
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1731 size_t len = strlen(s);
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1732 if (len > INT_MAX) {
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1733 PyErr_SetString(PyExc_OverflowError, "input too long");
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1734 result = NULL;
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1735 }
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1736 else {
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1737 result = PyString_FromStringAndSize(s, (int)(len-1));
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1738 }
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1739 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12572
diff changeset
1740 PyMem_FREE(s);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1741 return result;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1742 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
1743 if (v != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1744 f = PySys_GetObject("stdout");
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1745 if (f == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1746 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1747 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1748 }
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1749 if (Py_FlushLine() != 0 ||
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1750 PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
1751 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
1752 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1753 f = PySys_GetObject("stdin");
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1754 if (f == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1755 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin");
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1756 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1757 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1758 return PyFile_GetLine(f, -1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1759 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1760
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1761 static char raw_input_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1762 "raw_input([prompt]) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1763 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1764 Read a string from standard input. The trailing newline is stripped.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1765 If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1766 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1767 is printed without a trailing newline before reading.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1768
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1769
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1770 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1771 builtin_reduce(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1772 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1773 PyObject *seq, *func, *result = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1774 PySequenceMethods *sqf;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1775 register int i;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1776
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1777 if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1778 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1779 if (result != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1780 Py_INCREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1781
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1782 sqf = seq->ob_type->tp_as_sequence;
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1783 if (sqf == NULL || sqf->sq_item == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1784 PyErr_SetString(PyExc_TypeError,
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1785 "2nd argument to reduce() must be a sequence object");
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1786 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1787 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1788
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1789 if ((args = PyTuple_New(2)) == NULL)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1790 goto Fail;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1791
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1792 for (i = 0; ; ++i) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1793 PyObject *op2;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1794
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1795 if (args->ob_refcnt > 1) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1796 Py_DECREF(args);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1797 if ((args = PyTuple_New(2)) == NULL)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1798 goto Fail;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1799 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1800
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1801 if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1802 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1803 PyErr_Clear();
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1804 break;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1805 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1806 goto Fail;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1807 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1808
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1809 if (result == NULL)
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1810 result = op2;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1811 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1812 PyTuple_SetItem(args, 0, result);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1813 PyTuple_SetItem(args, 1, op2);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1814 if ((result = PyEval_CallObject(func, args)) == NULL)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1815 goto Fail;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1816 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1817 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1818
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1819 Py_DECREF(args);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1820
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1821 if (result == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1822 PyErr_SetString(PyExc_TypeError,
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1823 "reduce of empty sequence with no initial value");
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1824
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1825 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1826
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1827 Fail:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1828 Py_XDECREF(args);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1829 Py_XDECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1830 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1831 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1832
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1833 static char reduce_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1834 "reduce(function, sequence[, initial]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1835 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1836 Apply a function of two arguments cumulatively to the items of a sequence,\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1837 from left to right, so as to reduce the sequence to a single value.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1838 For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1839 ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1840 of the sequence in the calculation, and serves as a default when the\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1841 sequence is empty.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1842
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1843
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1844 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1845 builtin_reload(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1846 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1847 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1848
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1849 if (!PyArg_ParseTuple(args, "O:reload", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1850 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1851 return PyImport_ReloadModule(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1852 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1853
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1854 static char reload_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1855 "reload(module) -> module\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1856 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1857 Reload the module. The module must have been successfully imported before.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1858
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1859
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1860 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1861 builtin_repr(PyObject *self, PyObject *args)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1862 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1863 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1864
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1865 if (!PyArg_ParseTuple(args, "O:repr", &v))
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1866 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1867 return PyObject_Repr(v);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1868 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1869
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1870 static char repr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1871 "repr(object) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1872 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1873 Return the canonical string representation of the object.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1874 For most object types, eval(repr(object)) == object.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1875
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1876
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1877 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1878 builtin_round(PyObject *self, PyObject *args)
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1879 {
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1880 double x;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1881 double f;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1882 int ndigits = 0;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1883 int i;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1884
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1885 if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits))
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1886 return NULL;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1887 f = 1.0;
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1888 i = abs(ndigits);
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1889 while (--i >= 0)
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1890 f = f*10.0;
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1891 if (ndigits < 0)
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1892 x /= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1893 else
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1894 x *= f;
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1895 if (x >= 0.0)
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1896 x = floor(x + 0.5);
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1897 else
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1898 x = ceil(x - 0.5);
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1899 if (ndigits < 0)
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1900 x *= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1901 else
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1902 x /= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1903 return PyFloat_FromDouble(x);
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1904 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1905
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1906 static char round_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1907 "round(number[, ndigits]) -> floating point number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1908 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1909 Round a number to a given precision in decimal digits (default 0 digits).\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1910 This always returns a floating point number. Precision may be negative.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1911
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1912
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1913 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1914 builtin_str(PyObject *self, PyObject *args)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1915 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1916 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1917
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1918 if (!PyArg_ParseTuple(args, "O:str", &v))
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1919 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1920 return PyObject_Str(v);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1921 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1922
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1923 static char str_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1924 "str(object) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1925 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1926 Return a nice string representation of the object.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1927 If the argument is a string, the return value is the same object.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1928
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1929
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1930 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1931 builtin_tuple(PyObject *self, PyObject *args)
1842
65cc727f1436 * Python/bltinmodule.c: added tuple() builtin
Guido van Rossum <guido@python.org>
parents: 1841
diff changeset
1932 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1933 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1934
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1935 if (!PyArg_ParseTuple(args, "O:tuple", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1936 return NULL;
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1937 return PySequence_Tuple(v);
1842
65cc727f1436 * Python/bltinmodule.c: added tuple() builtin
Guido van Rossum <guido@python.org>
parents: 1841
diff changeset
1938 }
65cc727f1436 * Python/bltinmodule.c: added tuple() builtin
Guido van Rossum <guido@python.org>
parents: 1841
diff changeset
1939
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1940 static char tuple_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1941 "tuple(sequence) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1942 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1943 Return a tuple whose items are the same as those of the argument sequence.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1944 If the argument is a tuple, the return value is the same object.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1945
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1946
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1947 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1948 builtin_type(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1949 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1950 PyObject *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1951
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1952 if (!PyArg_ParseTuple(args, "O:type", &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1953 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1954 v = (PyObject *)v->ob_type;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1955 Py_INCREF(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1956 return v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1957 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1958
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1959 static char type_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1960 "type(object) -> type object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1961 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1962 Return the type of the object.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1963
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1964
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1965 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1966 builtin_vars(PyObject *self, PyObject *args)
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1967 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1968 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1969 PyObject *d;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1970
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1971 if (!PyArg_ParseTuple(args, "|O:vars", &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1972 return NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1973 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1974 d = PyEval_GetLocals();
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1975 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1976 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1977 PyErr_SetString(PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1978 "no locals!?");
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1979 }
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1980 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1981 Py_INCREF(d);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1982 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1983 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1984 d = PyObject_GetAttrString(v, "__dict__");
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1985 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1986 PyErr_SetString(PyExc_TypeError,
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1987 "vars() argument must have __dict__ attribute");
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1988 return NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1989 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1990 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1991 return d;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1992 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1993
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1994 static char vars_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1995 "vars([object]) -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1996 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1997 Without arguments, equivalent to locals().\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1998 With an argument, equivalent to object.__dict__.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1999
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2000 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2001 abstract_issubclass(PyObject *derived, PyObject *cls, char *err, int first)
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2002 {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2003 static PyObject *__bases__ = NULL;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2004 PyObject *bases;
11010
e9442734bc49 # Darn! Local variable l declared but not used in abstract_issubclass().
Guido van Rossum <guido@python.org>
parents: 10995
diff changeset
2005 int i, n;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2006 int r = 0;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2007
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2008 if (__bases__ == NULL) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2009 __bases__ = PyString_FromString("__bases__");
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2010 if (__bases__ == NULL)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2011 return -1;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2012 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2013
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2014 if (first) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2015 bases = PyObject_GetAttr(cls, __bases__);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2016 if (bases == NULL || !PyTuple_Check(bases)) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2017 Py_XDECREF(bases);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2018 PyErr_SetString(PyExc_TypeError, err);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2019 return -1;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2020 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2021 Py_DECREF(bases);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2022 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2023
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2024 if (derived == cls)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2025 return 1;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2026
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2027 bases = PyObject_GetAttr(derived, __bases__);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2028 if (bases == NULL || !PyTuple_Check(bases)) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2029 Py_XDECREF(bases);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2030 PyErr_SetString(PyExc_TypeError, err);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2031 return -1;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2032 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2033
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2034 n = PyTuple_GET_SIZE(bases);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2035 for (i = 0; i < n; i++) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2036 r = abstract_issubclass(PyTuple_GET_ITEM(bases, i),
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2037 cls, err, 0);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2038 if (r != 0)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2039 break;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2040 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2041
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2042 Py_DECREF(bases);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2043
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2044 return r;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2045 }
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2046
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2047 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2048 builtin_isinstance(PyObject *self, PyObject *args)
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2049 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2050 PyObject *inst;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2051 PyObject *cls;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2052 PyObject *icls;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2053 static PyObject *__class__ = NULL;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2054 int retval = 0;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2055
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
2056 if (!PyArg_ParseTuple(args, "OO:isinstance", &inst, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2057 return NULL;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2058
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2059 if (PyClass_Check(cls)) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2060 if (PyInstance_Check(inst)) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2061 PyObject *inclass =
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2062 (PyObject*)((PyInstanceObject*)inst)->in_class;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2063 retval = PyClass_IsSubclass(inclass, cls);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2064 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2065 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2066 else if (PyType_Check(cls)) {
6812
d85147dce185 Need a cast when comparing type object in isinstance()
Guido van Rossum <guido@python.org>
parents: 6706
diff changeset
2067 retval = ((PyObject *)(inst->ob_type) == cls);
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2068 }
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2069 else if (!PyInstance_Check(inst)) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2070 if (__class__ == NULL) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2071 __class__ = PyString_FromString("__class__");
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2072 if (__class__ == NULL)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2073 return NULL;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2074 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2075 icls = PyObject_GetAttr(inst, __class__);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2076 if (icls != NULL) {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2077 retval = abstract_issubclass(
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2078 icls, cls,
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2079 "second argument must be a class",
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2080 1);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2081 Py_DECREF(icls);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2082 if (retval < 0)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2083 return NULL;
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2084 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2085 else {
6706
cbc5e7d6ad23 Support type objects in isinstance().
Guido van Rossum <guido@python.org>
parents: 6334
diff changeset
2086 PyErr_SetString(PyExc_TypeError,
cbc5e7d6ad23 Support type objects in isinstance().
Guido van Rossum <guido@python.org>
parents: 6334
diff changeset
2087 "second argument must be a class");
cbc5e7d6ad23 Support type objects in isinstance().
Guido van Rossum <guido@python.org>
parents: 6334
diff changeset
2088 return NULL;
cbc5e7d6ad23 Support type objects in isinstance().
Guido van Rossum <guido@python.org>
parents: 6334
diff changeset
2089 }
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2090 }
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2091 else {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2092 PyErr_SetString(PyExc_TypeError,
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2093 "second argument must be a class");
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2094 return NULL;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2095 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2096 return PyInt_FromLong(retval);
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2097 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2098
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2099 static char isinstance_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2100 "isinstance(object, class-or-type) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2101 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2102 Return whether an object is an instance of a class or of a subclass thereof.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2103 With a type as second argument, return whether that is the object's type.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2104
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2105
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2106 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2107 builtin_issubclass(PyObject *self, PyObject *args)
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2108 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2109 PyObject *derived;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2110 PyObject *cls;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2111 int retval;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2112
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
2113 if (!PyArg_ParseTuple(args, "OO:issubclass", &derived, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2114 return NULL;
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2115
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2116 if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2117 retval = abstract_issubclass(
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2118 derived, cls, "arguments must be classes", 1);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2119 if (retval < 0)
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2120 return NULL;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2121 }
10995
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2122 else {
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2123 /* shortcut */
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2124 if (!(retval = (derived == cls)))
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2125 retval = PyClass_IsSubclass(derived, cls);
0d186c10a70e Patch by Jim Fulton (code style tweaked a bit) to support
Guido van Rossum <guido@python.org>
parents: 10529
diff changeset
2126 }
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2127
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2128 return PyInt_FromLong(retval);
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2129 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2130
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2131 static char issubclass_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2132 "issubclass(C, B) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2133 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2134 Return whether class C is a subclass (i.e., a derived class) of class B.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2135
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2136
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2137 static PyObject*
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2138 builtin_zip(PyObject *self, PyObject *args)
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2139 {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2140 PyObject *ret;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2141 int itemsize = PySequence_Length(args);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2142 int i, j;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2143
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2144 if (itemsize < 1) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2145 PyErr_SetString(PyExc_TypeError,
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2146 "at least one sequence is required");
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2147 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2148 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2149 /* args must be a tuple */
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2150 assert(PyTuple_Check(args));
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2151
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2152 if ((ret = PyList_New(0)) == NULL)
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2153 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2154
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2155 for (i = 0;; i++) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2156 PyObject *next = PyTuple_New(itemsize);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2157 if (!next) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2158 Py_DECREF(ret);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2159 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2160 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2161 for (j = 0; j < itemsize; j++) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2162 PyObject *seq = PyTuple_GET_ITEM(args, j);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2163 PyObject *item = PySequence_GetItem(seq, i);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2164
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2165 if (!item) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2166 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2167 PyErr_Clear();
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2168 Py_DECREF(next);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2169 return ret;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2170 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2171 Py_DECREF(next);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2172 Py_DECREF(ret);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2173 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2174 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2175 PyTuple_SET_ITEM(next, j, item);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2176 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2177 PyList_Append(ret, next);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2178 Py_DECREF(next);
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2179 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2180 /* no return */
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2181 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2182
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2183
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2184 static char zip_doc[] =
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2185 "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2186 \n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2187 Return a list of tuples, where each tuple contains the i-th element\n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2188 from each of the argument sequences. The returned list is truncated\n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2189 in length to the length of the shortest argument sequence.";
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2190
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2191
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2192 static PyMethodDef builtin_methods[] = {
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2193 {"__import__", builtin___import__, 1, import_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2194 {"abs", builtin_abs, 1, abs_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2195 {"apply", builtin_apply, 1, apply_doc},
10428
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
2196 {"buffer", builtin_buffer, 1, buffer_doc},
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2197 {"callable", builtin_callable, 1, callable_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2198 {"chr", builtin_chr, 1, chr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2199 {"cmp", builtin_cmp, 1, cmp_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2200 {"coerce", builtin_coerce, 1, coerce_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2201 {"compile", builtin_compile, 1, compile_doc},
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
2202 #ifndef WITHOUT_COMPLEX
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2203 {"complex", builtin_complex, 1, complex_doc},
3323
6098aaa8ae7f changes for complex numbers
Guido van Rossum <guido@python.org>
parents: 2890
diff changeset
2204 #endif
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2205 {"delattr", builtin_delattr, 1, delattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2206 {"dir", builtin_dir, 1, dir_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2207 {"divmod", builtin_divmod, 1, divmod_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2208 {"eval", builtin_eval, 1, eval_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2209 {"execfile", builtin_execfile, 1, execfile_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2210 {"filter", builtin_filter, 1, filter_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2211 {"float", builtin_float, 1, float_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2212 {"getattr", builtin_getattr, 1, getattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2213 {"globals", builtin_globals, 1, globals_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2214 {"hasattr", builtin_hasattr, 1, hasattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2215 {"hash", builtin_hash, 1, hash_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2216 {"hex", builtin_hex, 1, hex_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2217 {"id", builtin_id, 1, id_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2218 {"input", builtin_input, 1, input_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2219 {"intern", builtin_intern, 1, intern_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2220 {"int", builtin_int, 1, int_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2221 {"isinstance", builtin_isinstance, 1, isinstance_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2222 {"issubclass", builtin_issubclass, 1, issubclass_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2223 {"len", builtin_len, 1, len_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2224 {"list", builtin_list, 1, list_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2225 {"locals", builtin_locals, 1, locals_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2226 {"long", builtin_long, 1, long_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2227 {"map", builtin_map, 1, map_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2228 {"max", builtin_max, 1, max_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2229 {"min", builtin_min, 1, min_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2230 {"oct", builtin_oct, 1, oct_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2231 {"open", builtin_open, 1, open_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2232 {"ord", builtin_ord, 1, ord_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2233 {"pow", builtin_pow, 1, pow_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2234 {"range", builtin_range, 1, range_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2235 {"raw_input", builtin_raw_input, 1, raw_input_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2236 {"reduce", builtin_reduce, 1, reduce_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2237 {"reload", builtin_reload, 1, reload_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2238 {"repr", builtin_repr, 1, repr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2239 {"round", builtin_round, 1, round_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2240 {"setattr", builtin_setattr, 1, setattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2241 {"slice", builtin_slice, 1, slice_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2242 {"str", builtin_str, 1, str_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2243 {"tuple", builtin_tuple, 1, tuple_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2244 {"type", builtin_type, 1, type_doc},
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
2245 {"unicode", builtin_unicode, 1, unicode_doc},
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
2246 {"unichr", builtin_unichr, 1, unichr_doc},
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2247 {"vars", builtin_vars, 1, vars_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2248 {"xrange", builtin_xrange, 1, xrange_doc},
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2249 {"zip", builtin_zip, 1, zip_doc},
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
2250 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2251 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2252
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2253 static char builtin_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2254 "Built-in functions, exceptions, and other objects.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2255 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2256 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2257
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2258 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2259 _PyBuiltin_Init(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2260 {
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2261 PyObject *mod, *dict, *debug;
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2262 mod = Py_InitModule4("__builtin__", builtin_methods,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2263 builtin_doc, (PyObject *)NULL,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2264 PYTHON_API_VERSION);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2265 if (mod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2266 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2267 dict = PyModule_GetDict(mod);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2268 if (PyDict_SetItemString(dict, "None", Py_None) < 0)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2269 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2270 if (PyDict_SetItemString(dict, "Ellipsis", Py_Ellipsis) < 0)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2271 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2272 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2273 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2274 Py_XDECREF(debug);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2275 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2276 }
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2277 Py_XDECREF(debug);
6198
c43ca8dd0081 Removed obsolete exception PyExc_AccessError.
Barry Warsaw <barry@python.org>
parents: 6145
diff changeset
2278
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2279 return mod;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2280 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2281
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
2282 /* Helper for filter(): filter a tuple through a function */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2283
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2284 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2285 filtertuple(PyObject *func, PyObject *tuple)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2286 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2287 PyObject *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2288 register int i, j;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2289 int len = PyTuple_Size(tuple);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2290
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2291 if (len == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2292 Py_INCREF(tuple);
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2293 return tuple;
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2294 }
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2295
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2296 if ((result = PyTuple_New(len)) == NULL)
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
2297 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2298
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2299 for (i = j = 0; i < len; ++i) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2300 PyObject *item, *good;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2301 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2302
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2303 if ((item = PyTuple_GetItem(tuple, i)) == NULL)
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2304 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2305 if (func == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2306 Py_INCREF(item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2307 good = item;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2308 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2309 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2310 PyObject *arg = Py_BuildValue("(O)", item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2311 if (arg == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2312 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2313 good = PyEval_CallObject(func, arg);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2314 Py_DECREF(arg);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2315 if (good == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2316 goto Fail_1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2317 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2318 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2319 Py_DECREF(good);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2320 if (ok) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2321 Py_INCREF(item);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2322 if (PyTuple_SetItem(result, j++, item) < 0)
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2323 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2324 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2325 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2326
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2327 if (_PyTuple_Resize(&result, j, 0) < 0)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2328 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2329
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2330 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2331
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2332 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2333 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2334 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2335 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2336
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2337
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
2338 /* Helper for filter(): filter a string through a function */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2339
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2340 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2341 filterstring(PyObject *func, PyObject *strobj)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2342 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2343 PyObject *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2344 register int i, j;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2345 int len = PyString_Size(strobj);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2346
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2347 if (func == Py_None) {
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
2348 /* No character is ever false -- share input string */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2349 Py_INCREF(strobj);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2350 return strobj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2351 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2352 if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
1387
f992ac2a38d5 * rangeobject.{c,h}, bltinmodule.c: removed non-essential ops from range
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
2353 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2354
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2355 for (i = j = 0; i < len; ++i) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2356 PyObject *item, *arg, *good;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2357 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2358
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2359 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2360 if (item == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2361 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2362 arg = Py_BuildValue("(O)", item);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2363 Py_DECREF(item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2364 if (arg == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2365 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2366 good = PyEval_CallObject(func, arg);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2367 Py_DECREF(arg);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2368 if (good == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2369 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2370 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2371 Py_DECREF(good);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2372 if (ok)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2373 PyString_AS_STRING((PyStringObject *)result)[j++] =
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2374 PyString_AS_STRING((PyStringObject *)item)[0];
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2375 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2376
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2377 if (j < len && _PyString_Resize(&result, j) < 0)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2378 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2379
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2380 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2381
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2382 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2383 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2384 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2385 }