annotate Python/bltinmodule.c @ 18933:09df3254b49d legacy-trunk

Merge of descr-branch back into trunk.
author Tim Peters <tim.peters@gmail.com>
date Thu, 02 Aug 2001 04:15:00 +0000
parents fb837c5ae5d3
children 2bb242dc69d8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15637
7fb7c51a2e61 Whoops! Two stray characters crept in to my last check-in
Andrew M. Kuchling <amk@amk.ca>
parents: 15635
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
c75a2f2dc220 Add unistd.h to make gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4595
diff changeset
15
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
16 /* The default encoding used by the platform file system APIs
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
17 Can remain NULL for all platforms that don't have such a concept
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
18 */
18281
e3ba3739855a experimental UCS-4 support: don't assume that MS_WIN32 implies
Fredrik Lundh <fredrik@pythonware.com>
parents: 17976
diff changeset
19 #if defined(MS_WIN32) && defined(HAVE_USABLE_WCHAR_T)
17822
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
20 const char *Py_FileSystemDefaultEncoding = "mbcs";
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
21 #else
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
22 const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
50376d470648 Fix the Py_FileSystemDefaultEncoding checkin - declare the variable in a fileobject.h, and initialize it in bltinmodule.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17817
diff changeset
23 #endif
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
24
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
25 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
26 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
27 static PyObject *filtertuple (PyObject *, PyObject *);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
28
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
29 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
30 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
31 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
32 char *name;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
33 PyObject *globals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
34 PyObject *locals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
35 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
36
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
37 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
38 &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
39 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6198
diff changeset
40 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
41 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
42
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
43 static char import_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
44 "__import__(name, globals, locals, fromlist) -> module\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
45 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
46 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
47 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
48 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
49 empty list to emulate ``import name''.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
50 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
51 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
52 fromlist is not empty.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
53
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
54
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
55 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
56 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
57 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
58 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
59
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
60 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
61 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
62 return PyNumber_Absolute(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
65 static char abs_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
66 "abs(number) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
67 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
68 Return the absolute value of the argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
69
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
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 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
72 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
73 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
74 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
75 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
76
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
77 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
78 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
79 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
80 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
81 if (!PySequence_Check(alist)) {
16017
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
82 PyErr_Format(PyExc_TypeError,
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
83 "apply() arg 2 expect sequence, found %s",
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
84 alist->ob_type->tp_name);
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
85 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
86 }
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
87 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
88 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
89 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
90 alist = t;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
91 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
92 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
93 if (kwdict != NULL && !PyDict_Check(kwdict)) {
16017
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
94 PyErr_Format(PyExc_TypeError,
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
95 "apply() arg 3 expected dictionary, found %s",
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
96 kwdict->ob_type->tp_name);
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
97 goto finally;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2824
diff changeset
98 }
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
99 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
100 finally:
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
101 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
102 return retval;
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
103 }
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
104
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
105 static char apply_doc[] =
15832
0ae389e935fc Update the docstring for apply() so that "args" is marked as optional
Fred Drake <fdrake@acm.org>
parents: 15829
diff changeset
106 "apply(object[, args[, kwargs]]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
107 \n\
11621
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
108 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
109 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
110 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
111
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
112
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
113 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
114 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
115 {
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
116 PyObject *ob;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
117 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
118 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
119
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
120 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
121 return NULL;
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
122 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
123 }
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 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
126 "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
127 \n\
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
128 Create a new buffer object which references the given 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
129 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
130 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
131 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
132
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
133
3571b4c1bc0e New builtin buffer() creates a derived read-only buffer from any
Guido van Rossum <guido@python.org>
parents: 10292
diff changeset
134 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
135 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
136 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
137 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
138
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
139 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
140 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
141 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
142 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
143
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
144 static char callable_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
145 "callable(object) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
146 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
147 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
148 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
149
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
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 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
152 builtin_filter(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
153 {
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
154 PyObject *func, *seq, *result, *it;
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
155 int len; /* guess for result list size */
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
156 register int j;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
157
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
158 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
159 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
160
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
161 /* Strings and tuples return a result of the same type. */
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
162 if (PyString_Check(seq))
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
163 return filterstring(func, seq);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
164 if (PyTuple_Check(seq))
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
165 return filtertuple(func, seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
166
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
167 /* Get iterator. */
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
168 it = PyObject_GetIter(seq);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
169 if (it == NULL)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
170 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
171
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
172 /* Guess a result list size. */
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
173 len = -1; /* unknown */
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
174 if (PySequence_Check(seq) &&
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
175 seq->ob_type->tp_as_sequence->sq_length) {
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
176 len = PySequence_Size(seq);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
177 if (len < 0)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
178 PyErr_Clear();
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
179 }
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
180 if (len < 0)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
181 len = 8; /* arbitrary */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
182
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
183 /* Get a result list. */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
184 if (PyList_Check(seq) && seq->ob_refcnt == 1) {
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
185 /* Eww - can modify the list in-place. */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
186 Py_INCREF(seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
187 result = seq;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
188 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
189 else {
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
190 result = PyList_New(len);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
191 if (result == NULL)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
192 goto Fail_it;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
193 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
194
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
195 /* Build the result list. */
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
196 j = 0;
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
197 for (;;) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
198 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
199 int ok;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
200
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
201 item = PyIter_Next(it);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
202 if (item == NULL) {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
203 if (PyErr_Occurred())
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
204 goto Fail_result_it;
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
205 break;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
206 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
207
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
208 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
209 good = item;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
210 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
211 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
212 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
213 PyObject *arg = Py_BuildValue("(O)", item);
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
214 if (arg == NULL) {
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
215 Py_DECREF(item);
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
216 goto Fail_result_it;
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
217 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
218 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
219 Py_DECREF(arg);
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
220 if (good == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
221 Py_DECREF(item);
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
222 goto Fail_result_it;
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
223 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
224 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
225 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
226 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
227 if (ok) {
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
228 if (j < len)
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
229 PyList_SET_ITEM(result, j, item);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
230 else {
9991
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
231 int status = PyList_Append(result, item);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
232 Py_DECREF(item);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
233 if (status < 0)
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
234 goto Fail_result_it;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
235 }
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
236 ++j;
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
237 }
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
238 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
239 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
240 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
241
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
242
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
243 /* Cut back result list if len is too big. */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
244 if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
245 goto Fail_result_it;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
246
17874
eb03c6fc31e5 SF bug #425836: Reference leak in filter().
Tim Peters <tim.peters@gmail.com>
parents: 17822
diff changeset
247 Py_DECREF(it);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
248 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
249
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
250 Fail_result_it:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
251 Py_DECREF(result);
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
252 Fail_it:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
253 Py_DECREF(it);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
254 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
255 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
256
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
257 static char filter_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
258 "filter(function, sequence) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
259 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
260 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
261 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
262
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
263
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
264 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
265 builtin_chr(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 long x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 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
269
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
270 if (!PyArg_ParseTuple(args, "l:chr", &x))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 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
273 PyErr_SetString(PyExc_ValueError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
274 "chr() arg not in range(256)");
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 }
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
277 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
278 return PyString_FromStringAndSize(s, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
281 static char chr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
282 "chr(i) -> character\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
283 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
284 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
285
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
286
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
287 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
288 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
289 {
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
290 long x;
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
291 Py_UNICODE s[2];
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
292
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
293 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
294 return NULL;
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
295
18836
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
296 #ifdef Py_UNICODE_WIDE
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
297 if (x < 0 || x > 0x10ffff) {
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
298 PyErr_SetString(PyExc_ValueError,
18836
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
299 "unichr() arg not in range(0x110000) "
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
300 "(wide Python build)");
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
301 return NULL;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
302 }
18836
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
303 #else
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
304 if (x < 0 || x > 0xffff) {
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
305 PyErr_SetString(PyExc_ValueError,
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
306 "unichr() arg not in range(0x10000) "
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
307 "(narrow Python build)");
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
308 return NULL;
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
309 }
03305602ba9b Undoing the UCS-4 patch addition which caused unichr() to return
Marc-André Lemburg <mal@egenix.com>
parents: 18611
diff changeset
310 #endif
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
311
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
312 if (x <= 0xffff) {
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
313 /* UCS-2 character */
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
314 s[0] = (Py_UNICODE) x;
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
315 return PyUnicode_FromUnicode(s, 1);
18297
932903c7d054 Cosmetic changes to MvL's change to unichr():
Guido van Rossum <guido@python.org>
parents: 18292
diff changeset
316 }
932903c7d054 Cosmetic changes to MvL's change to unichr():
Guido van Rossum <guido@python.org>
parents: 18292
diff changeset
317 else {
18324
3ba7a358ce02 use Py_UNICODE_WIDE instead of USE_UCS4_STORAGE and Py_UNICODE_SIZE
Fredrik Lundh <fredrik@pythonware.com>
parents: 18297
diff changeset
318 #ifndef Py_UNICODE_WIDE
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
319 /* UCS-4 character. store as two surrogate characters */
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
320 x -= 0x10000L;
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
321 s[0] = 0xD800 + (Py_UNICODE) (x >> 10);
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
322 s[1] = 0xDC00 + (Py_UNICODE) (x & 0x03FF);
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
323 return PyUnicode_FromUnicode(s, 2);
18297
932903c7d054 Cosmetic changes to MvL's change to unichr():
Guido van Rossum <guido@python.org>
parents: 18292
diff changeset
324 #else
932903c7d054 Cosmetic changes to MvL's change to unichr():
Guido van Rossum <guido@python.org>
parents: 18292
diff changeset
325 s[0] = (Py_UNICODE)x;
932903c7d054 Cosmetic changes to MvL's change to unichr():
Guido van Rossum <guido@python.org>
parents: 18292
diff changeset
326 return PyUnicode_FromUnicode(s, 1);
18292
ffbb9c918ac0 Support using UCS-4 as the Py_UNICODE type:
Martin v. Löwis <martin@v.loewis.de>
parents: 18282
diff changeset
327 #endif
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
328 }
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
329 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
330
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
331 static char unichr_doc[] =
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
332 "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
333 \n\
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
334 Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.";
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
335
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
336
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
337 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
338 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
339 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
340 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
341 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
342
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
343 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
344 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
345 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
346 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
347 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
348 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
349
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
350 static char cmp_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
351 "cmp(x, y) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
352 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
353 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
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_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
358 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
359 PyObject *v, *w;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
360 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
361
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
362 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
363 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
364 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
365 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
366 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
367 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
368 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
369 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
370 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
371
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
372 static char coerce_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
373 "coerce(x, y) -> None or (x1, y1)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
374 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
375 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
376 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
377
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
378
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
379 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
380 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
381 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
382 char *str;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
383 char *filename;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
384 char *startstr;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
385 int start;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
386 PyCompilerFlags cf;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
387
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
388 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
389 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
390 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
391 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
392 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
393 start = Py_eval_input;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
394 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
395 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
396 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
397 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
398 "compile() arg 3 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
399 return NULL;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
400 }
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
401 cf.cf_flags = 0;
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
402 if (PyEval_MergeCompilerFlags(&cf))
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17111
diff changeset
403 return Py_CompileStringFlags(str, filename, start, &cf);
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
404 else
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17111
diff changeset
405 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
406 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
407
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
408 static char compile_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
409 "compile(source, filename, mode) -> code object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
410 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
411 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
412 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
413 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
414 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
415 single (interactive) statement, or 'eval' to compile an expression.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
416
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
417
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
418 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
419 builtin_dir(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 {
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
421 static char *attrlist[] = {"__members__", "__methods__", NULL};
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
422 PyObject *v = NULL, *l = NULL, *m = NULL;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
423 PyObject *d, *x;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
424 int i;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
425 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
426
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
427 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
428 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 if (v == NULL) {
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
430 x = PyEval_GetLocals();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
431 if (x == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
432 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
433 l = PyMapping_Keys(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
434 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
435 goto error;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
438 d = PyObject_GetAttrString(v, "__dict__");
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
439 if (d == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
440 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
441 else {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
442 l = PyMapping_Keys(d);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
443 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
444 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
445 Py_DECREF(d);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
446 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
447 if (l == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
448 l = PyList_New(0);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
449 if (l == NULL)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
450 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
451 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
452 for (s = attrlist; *s != NULL; s++) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
453 m = PyObject_GetAttrString(v, *s);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
454 if (m == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
455 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
456 continue;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
457 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
458 for (i = 0; ; i++) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
459 x = PySequence_GetItem(m, i);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
460 if (x == NULL) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
461 PyErr_Clear();
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
462 break;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
463 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
464 if (PyList_Append(l, x) != 0) {
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
465 Py_DECREF(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
466 Py_DECREF(m);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
467 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
468 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
469 Py_DECREF(x);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
470 }
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
471 Py_DECREF(m);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 }
385
039aee6cbd78 Protect dir() against non-directory __dict__ attributes.
Guido van Rossum <guido@python.org>
parents: 332
diff changeset
473 }
5421
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
474 if (PyList_Sort(l) != 0)
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
475 goto error;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
476 return l;
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
477 error:
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
478 Py_XDECREF(l);
dacbfaa07c40 New dir() function --
Guido van Rossum <guido@python.org>
parents: 5388
diff changeset
479 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
482 static char dir_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
483 "dir([object]) -> list of strings\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
484 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
485 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
486 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
487 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
488 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
489 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
490
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
491
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
492 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
493 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
494 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
495 PyObject *v, *w;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
496
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
497 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
498 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
499 return PyNumber_Divmod(v, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
502 static char divmod_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
503 "divmod(x, y) -> (div, mod)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
504 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
505 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
506
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
507
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
508 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
509 builtin_eval(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
511 PyObject *cmd;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
512 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
513 char *str;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
514
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
515 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
516 &cmd,
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
517 &PyDict_Type, &globals,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
518 &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
519 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
520 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
521 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
522 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
523 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
524 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
525 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
526 locals = globals;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
527 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
528 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
529 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
530 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
531 }
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
532 if (PyCode_Check(cmd)) {
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
533 if (PyTuple_GET_SIZE(((PyCodeObject *)cmd)->co_freevars) > 0) {
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
534 PyErr_SetString(PyExc_TypeError,
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
535 "code object passed to eval() may not contain free variables");
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
536 return NULL;
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
537 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
538 return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
539 }
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
540 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
541 !PyUnicode_Check(cmd)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
542 PyErr_SetString(PyExc_TypeError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
543 "eval() arg 1 must be a string or code object");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 }
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
546 if (PyString_AsStringAndSize(cmd, &str, NULL))
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
547 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
548 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
549 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
550 return PyRun_String(str, Py_eval_input, globals, locals);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
552
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
553 static char eval_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
554 "eval(source[, globals[, locals]]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
555 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
556 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
557 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
558 or a code object as returned by compile().\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
559 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
560 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
561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
562
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
563 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
564 builtin_execfile(PyObject *self, PyObject *args)
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
565 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
566 char *filename;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
567 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
568 PyObject *res;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
569 FILE* fp;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
570 PyCompilerFlags cf;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
571
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
572 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
573 &filename,
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
574 &PyDict_Type, &globals,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
575 &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
576 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
577 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
578 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
579 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
580 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
581 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
582 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
583 locals = globals;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
584 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
585 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
586 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
587 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
588 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
589 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
590 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
591 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
592 if (fp == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
593 PyErr_SetFromErrno(PyExc_IOError);
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
594 return NULL;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
595 }
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
596 cf.cf_flags = 0;
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
597 if (PyEval_MergeCompilerFlags(&cf))
17641
9be7758a8c94 Fix buglet reported on c.l.py: map(fnc, file.xreadlines()) blows up.
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
598 res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17111
diff changeset
599 locals, 1, &cf);
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
600 else
17641
9be7758a8c94 Fix buglet reported on c.l.py: map(fnc, file.xreadlines()) blows up.
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
601 res = PyRun_FileEx(fp, filename, Py_file_input, globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17111
diff changeset
602 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
603 return res;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
604 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
605
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
606 static char execfile_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
607 "execfile(filename[, globals[, locals]])\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
608 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
609 Read and execute a Python script from a file.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
610 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
611 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
612
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
613
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
614 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
615 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
616 {
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
617 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
618 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
619
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
620 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
621 return NULL;
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
622 if (PyUnicode_Check(name)) {
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
623 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
624 if (name == NULL)
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
625 return NULL;
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
626 }
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
627
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
628 if (!PyString_Check(name)) {
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
629 PyErr_SetString(PyExc_TypeError,
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
630 "attribute name must be string");
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
631 return NULL;
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
632 }
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
633 result = PyObject_GetAttr(v, name);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
634 if (result == NULL && dflt != NULL) {
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
635 PyErr_Clear();
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
636 Py_INCREF(dflt);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
637 result = dflt;
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
638 }
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
639 return result;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
640 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
641
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
642 static char getattr_doc[] =
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
643 "getattr(object, name[, default]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
644 \n\
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
645 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
646 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
647 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
648
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
649
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
650 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
651 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
652 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
653 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
654
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
655 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
656 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
657 d = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
658 Py_INCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
659 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
660 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
661
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
662 static char globals_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
663 "globals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
664 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
665 Return the dictionary containing the current scope's global variables.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
666
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
667
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
668 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
669 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
670 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
671 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
672 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
673
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
674 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
675 return NULL;
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
676 if (PyUnicode_Check(name)) {
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
677 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
678 if (name == NULL)
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
679 return NULL;
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
680 }
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
681
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
682 if (!PyString_Check(name)) {
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
683 PyErr_SetString(PyExc_TypeError,
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
684 "attribute name must be string");
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
685 return NULL;
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
686 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
687 v = PyObject_GetAttr(v, name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
688 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
689 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
690 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
691 return Py_False;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
692 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
693 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
694 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
695 return Py_True;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
696 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
697
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
698 static char hasattr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
699 "hasattr(object, name) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
700 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
701 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
702 (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
703
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
704
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
705 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
706 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
707 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
708 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
709
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
710 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
711 return NULL;
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
712 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
713 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
714
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
715 static char id_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
716 "id(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
717 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
718 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
719 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
720
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
721
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
722 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
723 builtin_map(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
724 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
725 typedef struct {
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
726 PyObject *it; /* the iterator object */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
727 int saw_StopIteration; /* bool: did the iterator end? */
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
728 } sequence;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
729
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
730 PyObject *func, *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
731 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
732 int n, len;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
733 register int i, j;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
734
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
735 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
736 if (n < 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
737 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
738 "map() requires at least two args");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
739 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
740 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
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 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
743 n--;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
744
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
745 if (func == Py_None && n == 1) {
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
746 /* 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
747 return PySequence_List(PyTuple_GetItem(args, 1));
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
748 }
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
749
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
750 /* Get space for sequence descriptors. Must NULL out the iterator
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
751 * pointers so that jumping to Fail_2 later doesn't see trash.
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
752 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
753 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
754 PyErr_NoMemory();
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
755 return NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
756 }
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
757 for (i = 0; i < n; ++i) {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
758 seqs[i].it = (PyObject*)NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
759 seqs[i].saw_StopIteration = 0;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
760 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
761
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
762 /* Do a first pass to obtain iterators for the arguments, and set len
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
763 * to the largest of their lengths.
17641
9be7758a8c94 Fix buglet reported on c.l.py: map(fnc, file.xreadlines()) blows up.
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
764 */
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
765 len = 0;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
766 for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
767 PyObject *curseq;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
768 int curlen;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
769
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
770 /* Get iterator. */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
771 curseq = PyTuple_GetItem(args, i+1);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
772 sqp->it = PyObject_GetIter(curseq);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
773 if (sqp->it == NULL) {
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
774 static char errmsg[] =
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
775 "argument %d to map() must support iteration";
5388
13a30cea8dbf Avoid sprintf buffer overflow if more than 9999 arguments.
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
776 char errbuf[sizeof(errmsg) + 25];
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
777 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
778 PyErr_SetString(PyExc_TypeError, errbuf);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
779 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
780 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
781
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
782 /* Update len. */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
783 curlen = -1; /* unknown */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
784 if (PySequence_Check(curseq) &&
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
785 curseq->ob_type->tp_as_sequence->sq_length) {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
786 curlen = PySequence_Size(curseq);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
787 if (curlen < 0)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
788 PyErr_Clear();
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
789 }
17641
9be7758a8c94 Fix buglet reported on c.l.py: map(fnc, file.xreadlines()) blows up.
Tim Peters <tim.peters@gmail.com>
parents: 17597
diff changeset
790 if (curlen < 0)
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
791 curlen = 8; /* arbitrary */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
792 if (curlen > len)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
793 len = curlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
794 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
795
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
796 /* Get space for the result list. */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
797 if ((result = (PyObject *) PyList_New(len)) == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
798 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
799
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
800 /* Iterate over the sequences until all have stopped. */
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
801 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
802 PyObject *alist, *item=NULL, *value;
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
803 int numactive = 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
804
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
805 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
806 alist = NULL;
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
807 else if ((alist = PyTuple_New(n)) == NULL)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
808 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
809
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
810 for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
811 if (sqp->saw_StopIteration) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
812 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
813 item = Py_None;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
814 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
815 else {
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
816 item = PyIter_Next(sqp->it);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
817 if (item)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
818 ++numactive;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
819 else {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
820 if (PyErr_Occurred()) {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
821 Py_XDECREF(alist);
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
822 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
823 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
824 Py_INCREF(Py_None);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
825 item = Py_None;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
826 sqp->saw_StopIteration = 1;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
827 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
828 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
829 if (alist)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
830 PyTuple_SET_ITEM(alist, j, item);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
831 else
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
832 break;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
833 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
834
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
835 if (!alist)
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
836 alist = item;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
837
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
838 if (numactive == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
839 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
840 break;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
841 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
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 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
844 value = alist;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
845 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
846 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
847 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
848 if (value == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
849 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
850 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
851 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
852 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
853 Py_DECREF(value);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
854 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
855 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
856 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
857 else if (PyList_SetItem(result, i, value) < 0)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
858 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
859 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
860
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
861 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
862 goto Fail_1;
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
863
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
864 goto Succeed;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
865
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
866 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
867 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
868 Fail_2:
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
869 result = NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
870 Succeed:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
871 assert(seqs);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
872 for (i = 0; i < n; ++i)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
873 Py_XDECREF(seqs[i].it);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
874 PyMem_DEL(seqs);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
875 return result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
876 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
877
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
878 static char map_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
879 "map(function, sequence[, sequence, ...]) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
880 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
881 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
882 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
883 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
884 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
885 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
886 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
887
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
888
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
889 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
890 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
891 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
892 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
893 PyObject *name;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
894 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
895
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
896 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
897 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
898 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
899 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
900 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
901 return Py_None;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
902 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
903
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
904 static char setattr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
905 "setattr(object, name, value)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
906 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
907 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
908 ``x.y = v''.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
909
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
910
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
911 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
912 builtin_delattr(PyObject *self, PyObject *args)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
913 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
914 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
915 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
916
14662
2d6188c2ea56 Deferred the attribute name object type checking to the underlying
Marc-André Lemburg <mal@egenix.com>
parents: 14375
diff changeset
917 if (!PyArg_ParseTuple(args, "OO:delattr", &v, &name))
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
918 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
919 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
920 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
921 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
922 return Py_None;
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
923 }
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
924
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
925 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
926 "delattr(object, name)\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
927 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
928 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
929 ``del x.y''.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
930
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
931
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
932 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
933 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
934 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
935 PyObject *v;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
936 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
937
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
938 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
939 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
940 x = PyObject_Hash(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
941 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
942 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
943 return PyInt_FromLong(x);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
944 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
945
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
946 static char hash_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
947 "hash(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
948 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
949 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
950 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
951
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
952
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
953 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
954 builtin_hex(PyObject *self, PyObject *args)
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
955 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
956 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
957 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
958
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
959 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
960 return NULL;
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
961
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
962 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
963 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
964 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
965 "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
966 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
967 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
968 return (*nb->nb_hex)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
969 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
970
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
971 static char hex_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
972 "hex(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
973 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
974 Return the hexadecimal representation of an integer or long integer.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
975
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
976
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
977 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
978
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
979 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
980 builtin_input(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
982 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
983 char *str;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
984 PyObject *res;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
985 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
986
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
987 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
988 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
989 return line;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
990 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
991 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
992 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
993 str++;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
994 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
995 locals = PyEval_GetLocals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
996 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
997 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
998 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
999 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
1000 }
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5421
diff changeset
1001 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
1002 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
1003 return res;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1006 static char input_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1007 "input([prompt]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1008 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1009 Equivalent to eval(raw_input(prompt)).";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1010
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1011
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1012 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1013 builtin_intern(PyObject *self, PyObject *args)
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1014 {
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1015 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
1016 if (!PyArg_ParseTuple(args, "S:intern", &s))
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1017 return NULL;
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1018 Py_INCREF(s);
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1019 PyString_InternInPlace(&s);
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1020 return s;
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1021 }
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1022
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1023 static char intern_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1024 "intern(string) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1025 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1026 ``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
1027 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
1028 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
1029 same value.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1030
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1031
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1032 static PyObject *
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1033 builtin_iter(PyObject *self, PyObject *args)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1034 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1035 PyObject *v, *w = NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1036
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1037 if (!PyArg_ParseTuple(args, "O|O:iter", &v, &w))
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1038 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1039 if (w == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1040 return PyObject_GetIter(v);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1041 if (!PyCallable_Check(v)) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1042 PyErr_SetString(PyExc_TypeError,
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1043 "iter(v, w): v must be callable");
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1044 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1045 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1046 return PyCallIter_New(v, w);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1047 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1048
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1049 static char iter_doc[] =
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1050 "iter(collection) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1051 iter(callable, sentinel) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1052 \n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1053 Get an iterator from an object. In the first form, the argument must\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1054 supply its own iterator, or be a sequence.\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1055 In the second form, the callable is called until it returns the sentinel.";
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1056
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1057
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1058 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1059 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
1060 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1061 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
1062 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
1063
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1064 if (!PyArg_ParseTuple(args, "O:len", &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 return NULL;
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13447
diff changeset
1066 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
1067 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
1068 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
1069 return PyInt_FromLong(res);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1072 static char len_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1073 "len(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1074 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1075 Return the number of items of a sequence or mapping.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1076
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1077
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1078 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1079 builtin_slice(PyObject *self, PyObject *args)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1080 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1081 PyObject *start, *stop, *step;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1082
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1083 start = stop = step = NULL;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1084
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1085 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
1086 return NULL;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1087
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1088 /* 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
1089 range(). */
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1090 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
1091 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
1092 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
1093 }
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1094 return PySlice_New(start, stop, step);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1095 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
1096
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1097 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
1098 "slice([start,] stop[, step]) -> slice object\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1099 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1100 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
1101
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1102
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1103 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1104 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
1105 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1106 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1107
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
1108 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
1109 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1110 d = PyEval_GetLocals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1111 Py_INCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1112 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1113 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1114
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1115 static char locals_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1116 "locals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1117 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1118 Return the dictionary containing the current scope's local variables.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1119
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1120
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1121 static PyObject *
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1122 min_max(PyObject *args, int op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 {
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1124 PyObject *v, *w, *x, *it;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1125
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1126 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
1127 v = args;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1128 else if (!PyArg_ParseTuple(args, "O:min/max", &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 return NULL;
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1130
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1131 it = PyObject_GetIter(v);
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1132 if (it == NULL)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 return NULL;
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1134
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1135 w = NULL; /* the result */
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1136 for (;;) {
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1137 x = PyIter_Next(it);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1138 if (x == NULL) {
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1139 if (PyErr_Occurred()) {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1140 Py_XDECREF(w);
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1141 Py_DECREF(it);
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1142 return NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1143 }
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1144 break;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1145 }
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1146
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1147 if (w == NULL)
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1148 w = x;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1149 else {
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1150 int cmp = PyObject_RichCompareBool(x, w, op);
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1151 if (cmp > 0) {
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1152 Py_DECREF(w);
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1153 w = x;
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1154 }
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1155 else if (cmp < 0) {
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1156 Py_DECREF(x);
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1157 Py_DECREF(w);
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1158 Py_DECREF(it);
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1159 return NULL;
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1160 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1161 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1162 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
1163 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1165 if (w == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1166 PyErr_SetString(PyExc_ValueError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1167 "min() or max() arg is an empty sequence");
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1168 Py_DECREF(it);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 return w;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1172 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1173 builtin_min(PyObject *self, PyObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 {
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1175 return min_max(v, Py_LT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1178 static char min_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1179 "min(sequence) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1180 min(a, b, c, ...) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1181 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1182 With a single sequence argument, return its smallest item.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1183 With two or more arguments, return the smallest argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1184
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1185
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1186 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1187 builtin_max(PyObject *self, PyObject *v)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1188 {
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1189 return min_max(v, Py_GT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1192 static char max_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1193 "max(sequence) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1194 max(a, b, c, ...) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1195 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1196 With a single sequence argument, return its largest item.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1197 With two or more arguments, return the largest argument.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1198
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1199
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1200 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1201 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
1202 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1203 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1204 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
1205
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1206 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
1207 return NULL;
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1208 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
1209 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
1210 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
1211 "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
1212 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1213 }
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1214 return (*nb->nb_oct)(v);
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1215 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1216
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1217 static char oct_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1218 "oct(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1219 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1220 Return the octal representation of an integer or long integer.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1221
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1222
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1223 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1224 builtin_open(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 {
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
1226 char *name = NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1227 char *mode = "r";
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1228 int bufsize = -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1229 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
1230
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
1231 if (!PyArg_ParseTuple(args, "et|si:open", Py_FileSystemDefaultEncoding,
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
1232 &name, &mode, &bufsize))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1234 f = PyFile_FromString(name, mode);
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents: 17694
diff changeset
1235 PyMem_Free(name); /* free the encoded string */
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1236 if (f != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1237 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
1238 return f;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1241 static char open_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1242 "open(filename[, mode[, buffering]]) -> file object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1243 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1244 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
1245 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
1246 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
1247 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
1248 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
1249 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
1250 buffered, and larger numbers specify the buffer size.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1251
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1252
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1253 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1254 builtin_ord(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 {
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1256 PyObject *obj;
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1257 long ord;
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1258 int size;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1259
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1260 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
1261 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
1262
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1263 if (PyString_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1264 size = PyString_GET_SIZE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1265 if (size == 1) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1266 ord = (long)((unsigned char)*PyString_AS_STRING(obj));
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1267 return PyInt_FromLong(ord);
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1268 }
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1269 } else if (PyUnicode_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1270 size = PyUnicode_GET_SIZE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1271 if (size == 1) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1272 ord = (long)*PyUnicode_AS_UNICODE(obj);
15635
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1273 return PyInt_FromLong(ord);
838551869790 Patch #102955, fixing one of the warnings in bug #121479:
Andrew M. Kuchling <amk@amk.ca>
parents: 15478
diff changeset
1274 }
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1275 } else {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1276 PyErr_Format(PyExc_TypeError,
15654
6405f30aa08c CHange error messages for ord(), using "string" instead of "string or Unicode"
Andrew M. Kuchling <amk@amk.ca>
parents: 15637
diff changeset
1277 "ord() expected string of length 1, but " \
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1278 "%.200s found", obj->ob_type->tp_name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279 return NULL;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1280 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1281
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1282 PyErr_Format(PyExc_TypeError,
15654
6405f30aa08c CHange error messages for ord(), using "string" instead of "string or Unicode"
Andrew M. Kuchling <amk@amk.ca>
parents: 15637
diff changeset
1283 "ord() expected a character, "
6405f30aa08c CHange error messages for ord(), using "string" instead of "string or Unicode"
Andrew M. Kuchling <amk@amk.ca>
parents: 15637
diff changeset
1284 "but string of length %d found",
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1285 size);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1286 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1287 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1289 static char ord_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1290 "ord(c) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1291 \n\
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1292 Return the integer ordinal of a one-character string.";
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1293
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1294
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1295 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1296 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
1297 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1298 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
1299
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1300 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
1301 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
1302 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
1303 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1304
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1305 static char pow_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1306 "pow(x, y[, z]) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1307 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1308 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
1309 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
1310
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1311
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1312 /* 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
1313 * 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
1314 * 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
1315 */
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1316 static long
13679
61f65de19e1e Another missed ansification.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1317 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
1318 {
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1319 /* -------------------------------------------------------------
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1320 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
1321 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
1322 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
1323 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
1324 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
1325 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
1326 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
1327 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
1328 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
1329 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
1330 ---------------------------------------------------------------*/
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1331 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
1332 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
1333 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
1334 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
1335 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
1336 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
1337 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1338 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
1339 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1340
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1341 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1342 builtin_range(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1344 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
1345 long bign;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 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
1347
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1348 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
1349
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1350 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
1351 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
1352 "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
1353 &ihigh))
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1354 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1357 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
1358 "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
1359 &ilow, &ihigh, &istep))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 if (istep == 0) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1363 PyErr_SetString(PyExc_ValueError, "range() arg 3 must not be zero");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1364 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1365 }
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1366 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
1367 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
1368 else
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1369 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
1370 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
1371 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
1372 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1373 "range() result 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
1374 return NULL;
5e9ef9d8575b Avoid overflow if possible in calculations for range(); report
Guido van Rossum <guido@python.org>
parents: 9567
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 v = PyList_New(n);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1377 if (v == NULL)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1378 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1379 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
1380 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
1381 if (w == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1382 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1383 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1384 }
8138
9d0310143600 Fred's right -- we need PyList_SET_ITEM().
Guido van Rossum <guido@python.org>
parents: 8132
diff changeset
1385 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
1386 ilow += istep;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1387 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1388 return v;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1389 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1390
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1391 static char range_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1392 "range([start,] stop[, step]) -> list of integers\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1393 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1394 Return a list containing an arithmetic progression of integers.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1395 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
1396 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
1397 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
1398 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
1399
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
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 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1402 builtin_xrange(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1404 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
1405 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
1406
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1407 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
1408 if (!PyArg_ParseTuple(args,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1409 "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
1410 &ihigh))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1411 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1412 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1413 else {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1414 if (!PyArg_ParseTuple(args,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1415 "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
1416 &ilow, &ihigh, &istep))
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1417 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1418 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1419 if (istep == 0) {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1420 PyErr_SetString(PyExc_ValueError, "xrange() arg 3 must not be zero");
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
1421 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1422 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1423 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
1424 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
1425 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
1426 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
1427 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
1428 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1429 "xrange() result has too many items");
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1430 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
1431 }
18436
9653272f036f Re-add 'advanced' xrange features, adding DeprecationWarnings as discussed
Thomas Wouters <thomas@python.org>
parents: 18392
diff changeset
1432 return PyRange_New(ilow, n, istep, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1433 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1434
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1435 static char xrange_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1436 "xrange([start,] stop[, step]) -> xrange object\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1437 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1438 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
1439 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
1440 than range() but more memory efficient.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1441
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1442
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1443 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1444 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
1445 {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1446 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1447 PyObject *f;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1448
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1449 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
1450 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1451 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
1452 PyFile_AsFile(PySys_GetObject("stdout")) == stdout &&
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1453 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
1454 PyObject *po;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1455 char *prompt;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1456 char *s;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1457 PyObject *result;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1458 if (v != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1459 po = PyObject_Str(v);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1460 if (po == NULL)
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1461 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1462 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
1463 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
1464 return NULL;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1465 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1466 else {
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1467 po = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1468 prompt = "";
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1469 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1470 s = PyOS_Readline(prompt);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1471 Py_XDECREF(po);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1472 if (s == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1473 PyErr_SetNone(PyExc_KeyboardInterrupt);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1474 return NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1475 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1476 if (*s == '\0') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1477 PyErr_SetNone(PyExc_EOFError);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1478 result = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1479 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1480 else { /* strip trailing '\n' */
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1481 size_t len = strlen(s);
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1482 if (len > INT_MAX) {
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1483 PyErr_SetString(PyExc_OverflowError, "input too long");
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1484 result = NULL;
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1485 }
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1486 else {
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1487 result = PyString_FromStringAndSize(s, (int)(len-1));
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
1488 }
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1489 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12572
diff changeset
1490 PyMem_FREE(s);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1491 return result;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1492 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
1493 if (v != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1494 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
1495 if (f == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1496 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
1497 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1498 }
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1499 if (Py_FlushLine() != 0 ||
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1500 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
1501 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
1502 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1503 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
1504 if (f == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1505 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
1506 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1507 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1508 return PyFile_GetLine(f, -1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1509 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1511 static char raw_input_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1512 "raw_input([prompt]) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1513 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1514 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
1515 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
1516 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
1517 is printed without a trailing newline before reading.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1518
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1519
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1520 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1521 builtin_reduce(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1522 {
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1523 PyObject *seq, *func, *result = NULL, *it;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1524
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1525 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
1526 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1527 if (result != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1528 Py_INCREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1529
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1530 it = PyObject_GetIter(seq);
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1531 if (it == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1532 PyErr_SetString(PyExc_TypeError,
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1533 "reduce() arg 2 must support iteration");
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1534 Py_XDECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1535 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1536 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1537
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1538 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
1539 goto Fail;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1540
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1541 for (;;) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1542 PyObject *op2;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1543
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1544 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
1545 Py_DECREF(args);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1546 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
1547 goto Fail;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1548 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1549
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1550 op2 = PyIter_Next(it);
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1551 if (op2 == NULL) {
17683
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1552 if (PyErr_Occurred())
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1553 goto Fail;
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
1554 break;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1555 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1556
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1557 if (result == NULL)
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1558 result = op2;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1559 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1560 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
1561 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
1562 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
1563 goto Fail;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1564 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1565 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1566
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1567 Py_DECREF(args);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1568
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1569 if (result == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1570 PyErr_SetString(PyExc_TypeError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1571 "reduce() of empty sequence with no initial value");
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1572
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1573 Py_DECREF(it);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1574 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1575
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1576 Fail:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1577 Py_XDECREF(args);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1578 Py_XDECREF(result);
17677
bf7f166d3c97 Generalize reduce() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17675
diff changeset
1579 Py_DECREF(it);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1580 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1581 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1582
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1583 static char reduce_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1584 "reduce(function, sequence[, initial]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1585 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1586 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
1587 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
1588 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
1589 ((((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
1590 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
1591 sequence is empty.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1592
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1593
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1594 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1595 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
1596 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1597 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
1598
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1599 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
1600 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1601 return PyImport_ReloadModule(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1602 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1603
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1604 static char reload_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1605 "reload(module) -> module\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1606 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1607 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
1608
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1609
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1610 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1611 builtin_repr(PyObject *self, PyObject *args)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1612 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1613 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
1614
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1615 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
1616 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1617 return PyObject_Repr(v);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1618 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
1619
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1620 static char repr_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1621 "repr(object) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1622 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1623 Return the canonical string representation of the object.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1624 For most object types, eval(repr(object)) == object.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1625
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1626
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1627 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1628 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
1629 {
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1630 double x;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1631 double f;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1632 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
1633 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
1634
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1635 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
1636 return NULL;
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1637 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
1638 i = abs(ndigits);
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1639 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
1640 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
1641 if (ndigits < 0)
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1642 x /= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1643 else
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1644 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
1645 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
1646 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
1647 else
8287
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1648 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
1649 if (ndigits < 0)
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1650 x *= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1651 else
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1652 x /= f;
d3bf3c2ad75a Implement round() slightly different, so that for negative ndigits no
Guido van Rossum <guido@python.org>
parents: 8138
diff changeset
1653 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
1654 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
1655
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1656 static char round_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1657 "round(number[, ndigits]) -> floating point number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1658 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1659 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
1660 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
1661
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1662
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1663 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1664 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
1665 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1666 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1667 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
1668
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1669 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
1670 return NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1671 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1672 d = PyEval_GetLocals();
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1673 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1674 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1675 PyErr_SetString(PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1676 "no locals!?");
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1677 }
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
1678 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1679 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
1680 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1681 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1682 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
1683 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1684 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
1685 "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
1686 return NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1687 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1688 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1689 return d;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1690 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1691
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1692 static char vars_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1693 "vars([object]) -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1694 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1695 Without arguments, equivalent to locals().\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1696 With an argument, equivalent to object.__dict__.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1697
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1698 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1699 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
1700 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1701 PyObject *inst;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1702 PyObject *cls;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1703 int retval;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1704
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
1705 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
1706 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
1707
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1708 retval = PyObject_IsInstance(inst, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1709 if (retval < 0)
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1710 return NULL;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1711 return PyInt_FromLong(retval);
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1712 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1713
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1714 static char isinstance_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1715 "isinstance(object, class-or-type) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1716 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1717 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
1718 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
1719
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1720
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1721 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1722 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
1723 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1724 PyObject *derived;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1725 PyObject *cls;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1726 int retval;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1727
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11800
diff changeset
1728 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
1729 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
1730
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1731 retval = PyObject_IsSubclass(derived, cls);
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1732 if (retval < 0)
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
1733 return NULL;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1734 return PyInt_FromLong(retval);
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1735 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1736
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1737 static char issubclass_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1738 "issubclass(C, B) -> Boolean\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1739 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1740 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
1741
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
1742
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1743 static PyObject*
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1744 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
1745 {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1746 PyObject *ret;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1747 int itemsize = PySequence_Length(args);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1748 int i;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1749 PyObject *itlist; /* tuple of iterators */
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1750
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1751 if (itemsize < 1) {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1752 PyErr_SetString(PyExc_TypeError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1753 "zip() requires at least one sequence");
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1754 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1755 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1756 /* 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
1757 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
1758
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1759 /* allocate result list */
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1760 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
1761 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1762
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1763 /* obtain iterators */
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1764 itlist = PyTuple_New(itemsize);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1765 if (itlist == NULL)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1766 goto Fail_ret;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1767 for (i = 0; i < itemsize; ++i) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1768 PyObject *item = PyTuple_GET_ITEM(args, i);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1769 PyObject *it = PyObject_GetIter(item);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1770 if (it == NULL) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1771 if (PyErr_ExceptionMatches(PyExc_TypeError))
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1772 PyErr_Format(PyExc_TypeError,
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1773 "zip argument #%d must support iteration",
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1774 i+1);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1775 goto Fail_ret_itlist;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1776 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1777 PyTuple_SET_ITEM(itlist, i, it);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1778 }
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1779
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1780 /* build result into ret list */
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1781 for (;;) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1782 int status;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1783 PyObject *next = PyTuple_New(itemsize);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1784 if (!next)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1785 goto Fail_ret_itlist;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1786
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1787 for (i = 0; i < itemsize; i++) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1788 PyObject *it = PyTuple_GET_ITEM(itlist, i);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1789 PyObject *item = PyIter_Next(it);
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1790 if (!item) {
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1791 if (PyErr_Occurred()) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1792 Py_DECREF(ret);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1793 ret = NULL;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1794 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1795 Py_DECREF(next);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1796 Py_DECREF(itlist);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1797 return ret;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1798 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1799 PyTuple_SET_ITEM(next, i, item);
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1800 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1801
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1802 status = PyList_Append(ret, next);
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1803 Py_DECREF(next);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1804 if (status < 0)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1805 goto Fail_ret_itlist;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1806 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1807
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1808 Fail_ret_itlist:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1809 Py_DECREF(itlist);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1810 Fail_ret:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1811 Py_DECREF(ret);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
1812 return NULL;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1813 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1814
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1815
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1816 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
1817 "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
1818 \n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1819 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
1820 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
1821 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
1822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
1823
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1824 static PyMethodDef builtin_methods[] = {
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1825 {"__import__", builtin___import__, 1, import_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1826 {"abs", builtin_abs, 1, abs_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1827 {"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
1828 {"buffer", builtin_buffer, 1, buffer_doc},
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1829 {"callable", builtin_callable, 1, callable_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1830 {"chr", builtin_chr, 1, chr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1831 {"cmp", builtin_cmp, 1, cmp_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1832 {"coerce", builtin_coerce, 1, coerce_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1833 {"compile", builtin_compile, 1, compile_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1834 {"delattr", builtin_delattr, 1, delattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1835 {"dir", builtin_dir, 1, dir_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1836 {"divmod", builtin_divmod, 1, divmod_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1837 {"eval", builtin_eval, 1, eval_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1838 {"execfile", builtin_execfile, 1, execfile_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1839 {"filter", builtin_filter, 1, filter_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1840 {"getattr", builtin_getattr, 1, getattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1841 {"globals", builtin_globals, 1, globals_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1842 {"hasattr", builtin_hasattr, 1, hasattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1843 {"hash", builtin_hash, 1, hash_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1844 {"hex", builtin_hex, 1, hex_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1845 {"id", builtin_id, 1, id_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1846 {"input", builtin_input, 1, input_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1847 {"intern", builtin_intern, 1, intern_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1848 {"isinstance", builtin_isinstance, 1, isinstance_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1849 {"issubclass", builtin_issubclass, 1, issubclass_doc},
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1850 {"iter", builtin_iter, 1, iter_doc},
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1851 {"len", builtin_len, 1, len_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1852 {"locals", builtin_locals, 1, locals_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1853 {"map", builtin_map, 1, map_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1854 {"max", builtin_max, 1, max_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1855 {"min", builtin_min, 1, min_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1856 {"oct", builtin_oct, 1, oct_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1857 {"open", builtin_open, 1, open_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1858 {"ord", builtin_ord, 1, ord_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1859 {"pow", builtin_pow, 1, pow_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1860 {"range", builtin_range, 1, range_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1861 {"raw_input", builtin_raw_input, 1, raw_input_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1862 {"reduce", builtin_reduce, 1, reduce_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1863 {"reload", builtin_reload, 1, reload_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1864 {"repr", builtin_repr, 1, repr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1865 {"round", builtin_round, 1, round_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1866 {"setattr", builtin_setattr, 1, setattr_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1867 {"slice", builtin_slice, 1, slice_doc},
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1868 {"unichr", builtin_unichr, 1, unichr_doc},
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1869 {"vars", builtin_vars, 1, vars_doc},
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1870 {"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
1871 {"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
1872 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1873 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1874
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1875 static char builtin_doc[] =
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1876 "Built-in functions, exceptions, and other objects.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1877 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1878 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.";
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1879
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1880 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1881 _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
1882 {
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1883 PyObject *mod, *dict, *debug;
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1884 mod = Py_InitModule4("__builtin__", builtin_methods,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1885 builtin_doc, (PyObject *)NULL,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1886 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
1887 if (mod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1888 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1889 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
1890 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
1891 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1892 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
1893 return NULL;
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1894 if (PyDict_SetItemString(dict, "NotImplemented",
15727
641ad6c833be Add NotImplemented to the builtin module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15654
diff changeset
1895 Py_NotImplemented) < 0)
641ad6c833be Add NotImplemented to the builtin module.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15654
diff changeset
1896 return NULL;
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1897 if (PyDict_SetItemString(dict, "classmethod",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1898 (PyObject *) &PyClassMethod_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1899 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1900 #ifndef WITHOUT_COMPLEX
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1901 if (PyDict_SetItemString(dict, "complex",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1902 (PyObject *) &PyComplex_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1903 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1904 #endif
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1905 if (PyDict_SetItemString(dict, "dictionary",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1906 (PyObject *) &PyDict_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1907 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1908 if (PyDict_SetItemString(dict, "float",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1909 (PyObject *) &PyFloat_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1910 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1911 if (PyDict_SetItemString(dict, "int", (PyObject *) &PyInt_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1912 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1913 if (PyDict_SetItemString(dict, "list", (PyObject *) &PyList_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1914 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1915 if (PyDict_SetItemString(dict, "long", (PyObject *) &PyLong_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1916 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1917 if (PyDict_SetItemString(dict, "object",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1918 (PyObject *) &PyBaseObject_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1919 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1920 if (PyDict_SetItemString(dict, "staticmethod",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1921 (PyObject *) &PyStaticMethod_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1922 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1923 if (PyDict_SetItemString(dict, "str", (PyObject *) &PyString_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1924 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1925 if (PyDict_SetItemString(dict, "tuple",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1926 (PyObject *) &PyTuple_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1927 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1928 if (PyDict_SetItemString(dict, "type", (PyObject *) &PyType_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1929 return NULL;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1930 if (PyDict_SetItemString(dict, "unicode",
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1931 (PyObject *) &PyUnicode_Type) < 0)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
1932 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1933 debug = PyInt_FromLong(Py_OptimizeFlag == 0);
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1934 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1935 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
1936 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1937 }
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
1938 Py_XDECREF(debug);
6198
c43ca8dd0081 Removed obsolete exception PyExc_AccessError.
Barry Warsaw <barry@python.org>
parents: 6145
diff changeset
1939
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
1940 return mod;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1941 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1942
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
1943 /* 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
1944
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1945 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1946 filtertuple(PyObject *func, PyObject *tuple)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1947 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1948 PyObject *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1949 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
1950 int len = PyTuple_Size(tuple);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1951
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
1952 if (len == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1953 Py_INCREF(tuple);
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
1954 return tuple;
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
1955 }
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
1956
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1957 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
1958 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1959
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1960 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
1961 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
1962 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1963
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1964 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
1965 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1966 if (func == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1967 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
1968 good = item;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1969 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1970 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1971 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
1972 if (arg == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1973 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1974 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
1975 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
1976 if (good == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1977 goto Fail_1;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1978 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1979 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1980 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
1981 if (ok) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1982 Py_INCREF(item);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1983 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
1984 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1985 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1986 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1987
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17874
diff changeset
1988 if (_PyTuple_Resize(&result, j) < 0)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1989 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1990
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1991 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1992
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1993 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1994 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1995 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1996 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1997
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1998
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
1999 /* 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
2000
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2001 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2002 filterstring(PyObject *func, PyObject *strobj)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2003 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2004 PyObject *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2005 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
2006 int len = PyString_Size(strobj);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2007
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2008 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
2009 /* 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
2010 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
2011 return strobj;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2012 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2013 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
2014 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2015
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2016 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
2017 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
2018 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2019
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2020 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
2021 if (item == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2022 goto Fail_1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2023 arg = Py_BuildValue("(O)", item);
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2024 if (arg == NULL) {
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2025 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
2026 goto Fail_1;
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2027 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2028 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
2029 Py_DECREF(arg);
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2030 if (good == NULL) {
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2031 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
2032 goto Fail_1;
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2033 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2034 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2035 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
2036 if (ok)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2037 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
2038 PyString_AS_STRING((PyStringObject *)item)[0];
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2039 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2040 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2041
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2042 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
2043 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2044
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2045 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2046
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2047 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2048 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2049 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2050 }