annotate Python/bltinmodule.c @ 21398:de125792bfaa legacy-trunk

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