annotate Python/bltinmodule.c @ 57673:20b7deb53d4e legacy-trunk

account for PyObject_IsInstance's new ability to fail
author Benjamin Peterson <benjamin@python.org>
date Sun, 13 Dec 2009 00:54:15 +0000
parents 75e1b8c05f43
children f550ed89398f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Built-in functions */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3 #include "Python.h"
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
4 #include "Python-ast.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"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35636
diff changeset
7 #include "code.h"
1263
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>
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
11 #include <float.h> /* for DBL_MANT_DIG and friends */
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
12
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
13 #ifdef RISCOS
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
14 #include "unixstuff.h"
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
15 #endif
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
16
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
17 /* 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
18 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
19 */
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23868
diff changeset
20 #if defined(MS_WINDOWS) && 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
21 const char *Py_FileSystemDefaultEncoding = "mbcs";
27782
904f0e769820 My previous checkin caused compile() to no longer accept buffers, as noted
Just van Rossum <just@letterror.com>
parents: 27781
diff changeset
22 #elif defined(__APPLE__)
904f0e769820 My previous checkin caused compile() to no longer accept buffers, as noted
Just van Rossum <just@letterror.com>
parents: 27781
diff changeset
23 const char *Py_FileSystemDefaultEncoding = "utf-8";
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 #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
25 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
26 #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
27
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
28 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
29 static PyObject *filterstring(PyObject *, PyObject *);
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
30 #ifdef Py_USING_UNICODE
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
31 static PyObject *filterunicode(PyObject *, PyObject *);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
32 #endif
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
33 static PyObject *filtertuple (PyObject *, PyObject *);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
34
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
35 static PyObject *
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
36 builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
37 {
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
38 static char *kwlist[] = {"name", "globals", "locals", "fromlist",
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
39 "level", 0};
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
40 char *name;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
41 PyObject *globals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
42 PyObject *locals = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
43 PyObject *fromlist = NULL;
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
44 int level = -1;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
45
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
46 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
47 kwlist, &name, &globals, &locals, &fromlist, &level))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
48 return NULL;
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
49 return PyImport_ImportModuleLevel(name, globals, locals,
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
50 fromlist, level);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
51 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
52
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
53 PyDoc_STRVAR(import_doc,
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
54 "__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
55 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
56 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
57 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
58 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
59 empty list to emulate ``import name''.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
60 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
61 returns package A when fromlist is empty, but its submodule B when\n\
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
62 fromlist is not empty. Level is used to determine whether to perform \n\
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
63 absolute or relative imports. -1 is the original strategy of attempting\n\
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
64 both absolute and relative imports, 0 is absolute, a positive number\n\
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
65 is the number of parent directories to search relative to the current module.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
66
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
67
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
68 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
69 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
70 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
71 return PyNumber_Absolute(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
74 PyDoc_STRVAR(abs_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
75 "abs(number) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
76 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
77 Return the absolute value of the argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
78
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
79 static PyObject *
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
80 builtin_all(PyObject *self, PyObject *v)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
81 {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
82 PyObject *it, *item;
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
83 PyObject *(*iternext)(PyObject *);
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
84 int cmp;
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
85
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
86 it = PyObject_GetIter(v);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
87 if (it == NULL)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
88 return NULL;
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
89 iternext = *Py_TYPE(it)->tp_iternext;
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
90
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
91 for (;;) {
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
92 item = iternext(it);
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
93 if (item == NULL)
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
94 break;
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
95 cmp = PyObject_IsTrue(item);
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
96 Py_DECREF(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
97 if (cmp < 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
98 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
99 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
100 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
101 if (cmp == 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
102 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
103 Py_RETURN_FALSE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
104 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
105 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
106 Py_DECREF(it);
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
107 if (PyErr_Occurred()) {
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
108 if (PyErr_ExceptionMatches(PyExc_StopIteration))
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
109 PyErr_Clear();
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
110 else
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
111 return NULL;
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
112 }
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
113 Py_RETURN_TRUE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
114 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
115
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
116 PyDoc_STRVAR(all_doc,
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
117 "all(iterable) -> bool\n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
118 \n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
119 Return True if bool(x) is True for all values x in the iterable.");
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
120
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
121 static PyObject *
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
122 builtin_any(PyObject *self, PyObject *v)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
123 {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
124 PyObject *it, *item;
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
125 PyObject *(*iternext)(PyObject *);
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
126 int cmp;
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
127
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
128 it = PyObject_GetIter(v);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
129 if (it == NULL)
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
130 return NULL;
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
131 iternext = *Py_TYPE(it)->tp_iternext;
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
132
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
133 for (;;) {
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
134 item = iternext(it);
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
135 if (item == NULL)
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
136 break;
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
137 cmp = PyObject_IsTrue(item);
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
138 Py_DECREF(item);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
139 if (cmp < 0) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
140 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
141 return NULL;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
142 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
143 if (cmp == 1) {
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
144 Py_DECREF(it);
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
145 Py_RETURN_TRUE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
146 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
147 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
148 Py_DECREF(it);
44367
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
149 if (PyErr_Occurred()) {
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
150 if (PyErr_ExceptionMatches(PyExc_StopIteration))
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
151 PyErr_Clear();
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
152 else
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
153 return NULL;
eaefb93b5ed4 Improve performance of built-in any()/all() by avoiding PyIter_Next() --
Guido van Rossum <guido@python.org>
parents: 44222
diff changeset
154 }
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
155 Py_RETURN_FALSE;
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
156 }
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
157
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
158 PyDoc_STRVAR(any_doc,
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
159 "any(iterable) -> bool\n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
160 \n\
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
161 Return True if bool(x) is True for any x in the iterable.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
162
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
163 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
164 builtin_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
165 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
166 PyObject *func, *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
167 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
168
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
169 if (PyErr_WarnPy3k("apply() not supported in 3.x; "
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
170 "use func(*args, **kwargs)", 1) < 0)
41832
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41168
diff changeset
171 return NULL;
a0ee3b2ba361 Add -3 option to the interpreter to warn about features that are
Neal Norwitz <nnorwitz@gmail.com>
parents: 41168
diff changeset
172
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
173 if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
174 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
175 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
176 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
177 if (!PySequence_Check(alist)) {
16017
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
178 PyErr_Format(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
179 "apply() arg 2 expected sequence, found %s",
16017
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
180 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
181 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
182 }
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
183 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
184 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
185 return NULL;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
186 alist = t;
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
187 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
188 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
189 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
190 PyErr_Format(PyExc_TypeError,
7234d7c3c29f clearer error messages for apply() and "no locals"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16011
diff changeset
191 "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
192 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
193 goto finally;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2824
diff changeset
194 }
9188
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
195 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
196 finally:
a1672e775f33 builtin_apply(): Second argument type check is relaxed to allow any sequence.
Barry Warsaw <barry@python.org>
parents: 9112
diff changeset
197 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
198 return retval;
480
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
199 }
c64c6702549d Added "apply"; added "SyntaxError"; changed table lay-out.
Guido van Rossum <guido@python.org>
parents: 463
diff changeset
200
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
201 PyDoc_STRVAR(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
202 "apply(object[, args[, kwargs]]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
203 \n\
11621
0ec803cbd872 Adjusted apply() docstring based on comments from Gerrit Holl
Fred Drake <fdrake@acm.org>
parents: 11383
diff changeset
204 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
205 and keyword arguments taken from the optional dictionary kwargs.\n\
28513
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 28396
diff changeset
206 Note that classes are callable, as are instances with a __call__() method.\n\
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 28396
diff changeset
207 \n\
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 28396
diff changeset
208 Deprecated since release 2.3. Instead, use the extended call syntax:\n\
90c77b8af1a4 SF patch #701494: more apply removals
Raymond Hettinger <python@rcn.com>
parents: 28396
diff changeset
209 function(*args, **keywords).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
210
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
211
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
212 static PyObject *
45433
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
213 builtin_bin(PyObject *self, PyObject *v)
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
214 {
47822
3ada46f2321d Revert 64424, 64438, and 64439.
Raymond Hettinger <python@rcn.com>
parents: 47779
diff changeset
215 return PyNumber_ToBase(v, 2);
45433
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
216 }
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
217
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
218 PyDoc_STRVAR(bin_doc,
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
219 "bin(number) -> string\n\
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
220 \n\
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
221 Return the binary representation of an integer or long integer.");
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
222
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
223
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
224 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
225 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
226 {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
227 if (PyErr_WarnPy3k("callable() not supported in 3.x; "
57306
349b97ff4c2b a better callable replacement
Benjamin Peterson <benjamin@python.org>
parents: 56750
diff changeset
228 "use isinstance(x, collections.Callable)", 1) < 0)
41834
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
229 return NULL;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
230 return PyBool_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
231 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
232
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
233 PyDoc_STRVAR(callable_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
234 "callable(object) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
235 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
236 Return whether the object is callable (i.e., some kind of function).\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
237 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
238
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
239
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
240 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
241 builtin_filter(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
242 {
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
243 PyObject *func, *seq, *result, *it, *arg;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
244 Py_ssize_t len; /* guess for result list size */
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
245 register Py_ssize_t j;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
246
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
247 if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
248 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
249
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
250 /* Strings and tuples return a result of the same type. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
251 if (PyString_Check(seq))
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
252 return filterstring(func, seq);
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
253 #ifdef Py_USING_UNICODE
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
254 if (PyUnicode_Check(seq))
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
255 return filterunicode(func, seq);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
256 #endif
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
257 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
258 return filtertuple(func, seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
259
35264
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
260 /* Pre-allocate argument list tuple. */
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
261 arg = PyTuple_New(1);
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
262 if (arg == NULL)
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
263 return NULL;
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
264
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
265 /* Get iterator. */
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
266 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
267 if (it == NULL)
35264
b431ed7bcdc2 Fix cleanup DECREF logic in builtin_filter function.
Georg Brandl <georg@python.org>
parents: 34736
diff changeset
268 goto Fail_arg;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
269
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
270 /* Guess a result list size. */
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 43675
diff changeset
271 len = _PyObject_LengthHint(seq, 8);
51282
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48280
diff changeset
272 if (len == -1)
ca71a44d5432 Issue 1242657: list(obj) can swallow KeyboardInterrupt
Raymond Hettinger <python@rcn.com>
parents: 48280
diff changeset
273 goto Fail_it;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
274
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
275 /* 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
276 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
277 /* 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
278 Py_INCREF(seq);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
279 result = seq;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
280 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
281 else {
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
282 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
283 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
284 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
285 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
286
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
287 /* 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
288 j = 0;
e9a4e27e8d4e Make PyIter_Next() a little smarter (wrt its knowledge of iterator
Tim Peters <tim.peters@gmail.com>
parents: 17677
diff changeset
289 for (;;) {
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
290 PyObject *item;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
291 int ok;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
292
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
293 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
294 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
295 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
296 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
297 break;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
298 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
299
30151
9f53e53f3880 Make filter(bool, ...) as fast as filter(None, ...).
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 30037
diff changeset
300 if (func == (PyObject *)&PyBool_Type || func == Py_None) {
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
301 ok = PyObject_IsTrue(item);
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
302 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
303 else {
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
304 PyObject *good;
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
305 PyTuple_SET_ITEM(arg, 0, item);
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
306 good = PyObject_Call(func, arg, NULL);
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
307 PyTuple_SET_ITEM(arg, 0, NULL);
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
308 if (good == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
309 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
310 goto Fail_result_it;
2120
84b61fe44573 fix subtle refcount big in filter() -- Tim MacKenzie
Guido van Rossum <guido@python.org>
parents: 2113
diff changeset
311 }
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
312 ok = PyObject_IsTrue(good);
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
313 Py_DECREF(good);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
314 }
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
315 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
316 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
317 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
318 else {
9991
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
319 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
320 Py_DECREF(item);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
321 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
322 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
323 }
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
324 ++j;
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
325 }
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
326 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
327 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
328 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
329
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
330
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
331 /* 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
332 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
333 goto Fail_result_it;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
334
17874
eb03c6fc31e5 SF bug #425836: Reference leak in filter().
Tim Peters <tim.peters@gmail.com>
parents: 17822
diff changeset
335 Py_DECREF(it);
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
336 Py_DECREF(arg);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
337 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
338
17654
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
339 Fail_result_it:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
340 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
341 Fail_it:
a97e18e499a9 Generalize filter(f, seq) to work with iterators. This also generalizes
Tim Peters <tim.peters@gmail.com>
parents: 17641
diff changeset
342 Py_DECREF(it);
24863
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
343 Fail_arg:
a3ebd1233c00 A nice little speed-up for filter():
Guido van Rossum <guido@python.org>
parents: 24817
diff changeset
344 Py_DECREF(arg);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
345 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
346 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
347
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
348 PyDoc_STRVAR(filter_doc,
22225
67a5e25dc88b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21712
diff changeset
349 "filter(function or None, sequence) -> list, tuple, or string\n"
67a5e25dc88b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21712
diff changeset
350 "\n"
67a5e25dc88b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21712
diff changeset
351 "Return those items of sequence for which function(item) is true. If\n"
67a5e25dc88b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 21712
diff changeset
352 "function is None, return the items that are true. If sequence is a tuple\n"
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
353 "or string, return the same type, else return a list.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
354
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
355 static PyObject *
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
356 builtin_format(PyObject *self, PyObject *args)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
357 {
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
358 PyObject *value;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
359 PyObject *format_spec = NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
360
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
361 if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
362 return NULL;
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
363
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
364 return PyObject_Format(value, format_spec);
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
365 }
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
366
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
367 PyDoc_STRVAR(format_doc,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
368 "format(value[, format_spec]) -> string\n\
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
369 \n\
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
370 Returns value.__format__(format_spec)\n\
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
371 format_spec defaults to \"\"");
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
372
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
373 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
374 builtin_chr(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 long x;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 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
378
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
379 if (!PyArg_ParseTuple(args, "l:chr", &x))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 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
382 PyErr_SetString(PyExc_ValueError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
383 "chr() arg not in range(256)");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 }
5317
c72c7e1930b3 Keep gcc -Wall and Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 5223
diff changeset
386 s[0] = (char)x;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
387 return PyString_FromStringAndSize(s, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
389
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
390 PyDoc_STRVAR(chr_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
391 "chr(i) -> character\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
392 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
393 Return a string of one character with ordinal i; 0 <= i < 256.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
394
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
395
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
396 #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
397 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
398 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
399 {
48115
4550144b1560 #3479: unichr(2**32) used to return u'\x00'.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47822
diff changeset
400 int x;
4550144b1560 #3479: unichr(2**32) used to return u'\x00'.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47822
diff changeset
401
4550144b1560 #3479: unichr(2**32) used to return u'\x00'.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47822
diff changeset
402 if (!PyArg_ParseTuple(args, "i:unichr", &x))
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
403 return NULL;
18282
f38923ce3c6b more unicode tweaks: make unichr(0xdddddddd) behave like u"\Udddddddd"
Fredrik Lundh <fredrik@pythonware.com>
parents: 18281
diff changeset
404
24737
eaa87632b1e8 Add C API PyUnicode_FromOrdinal() which exposes unichr() at C level.
Marc-André Lemburg <mal@egenix.com>
parents: 24071
diff changeset
405 return PyUnicode_FromOrdinal(x);
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
406 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
407
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
408 PyDoc_STRVAR(unichr_doc,
12386
32e9d0e78935 When refering to Unicode characters in exception messages and
Fred Drake <fdrake@acm.org>
parents: 12384
diff changeset
409 "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
410 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
411 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
412 #endif
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
413
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
414
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
415 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
416 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
417 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
418 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
419 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
420
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
421 if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
422 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
423 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
424 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
425 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
426 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
427
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
428 PyDoc_STRVAR(cmp_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
429 "cmp(x, y) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
430 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
431 Return negative if x<y, zero if x==y, positive if x>y.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
432
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
433
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
434 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
435 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
436 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
437 PyObject *v, *w;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
438 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
439
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
440 if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
41834
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
441 return NULL;
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
442
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
443 if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &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
444 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
445 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
446 return NULL;
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30430
diff changeset
447 res = PyTuple_Pack(2, v, w);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
448 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
449 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
450 return res;
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
451 }
222f04f55083 Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents: 926
diff changeset
452
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
453 PyDoc_STRVAR(coerce_doc,
33261
1cf7d7225a99 Patch #1015021: Stop claiming that coerce can return None.
Martin v. Löwis <martin@v.loewis.de>
parents: 33099
diff changeset
454 "coerce(x, y) -> (x1, y1)\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
455 \n\
33261
1cf7d7225a99 Patch #1015021: Stop claiming that coerce can return None.
Martin v. Löwis <martin@v.loewis.de>
parents: 33099
diff changeset
456 Return a tuple consisting of the two numeric arguments converted to\n\
1cf7d7225a99 Patch #1015021: Stop claiming that coerce can return None.
Martin v. Löwis <martin@v.loewis.de>
parents: 33099
diff changeset
457 a common type, using the same rules as used by arithmetic operations.\n\
1cf7d7225a99 Patch #1015021: Stop claiming that coerce can return None.
Martin v. Löwis <martin@v.loewis.de>
parents: 33099
diff changeset
458 If coercion is not possible, raise TypeError.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
459
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
460 static PyObject *
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
461 builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
462 {
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
463 char *str;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
464 char *filename;
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
465 char *startstr;
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
466 int mode = -1;
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
467 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
468 int supplied_flags = 0;
57673
20b7deb53d4e account for PyObject_IsInstance's new ability to fail
Benjamin Peterson <benjamin@python.org>
parents: 57550
diff changeset
469 int is_ast;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
470 PyCompilerFlags cf;
35960
658444baeba3 Fix memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 35766
diff changeset
471 PyObject *result = NULL, *cmd, *tmp = NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
472 Py_ssize_t length;
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
473 static char *kwlist[] = {"source", "filename", "mode", "flags",
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
474 "dont_inherit", NULL};
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
475 int start[] = {Py_file_input, Py_eval_input, Py_single_input};
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
476
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
477 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
478 kwlist, &cmd, &filename, &startstr,
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
479 &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
480 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
481
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
482 cf.cf_flags = supplied_flags;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
483
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
484 if (supplied_flags &
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
485 ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
486 {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
487 PyErr_SetString(PyExc_ValueError,
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
488 "compile(): unrecognised flags");
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
489 return NULL;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
490 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
491 /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
492
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
493 if (!dont_inherit) {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
494 PyEval_MergeCompilerFlags(&cf);
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
495 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
496
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
497 if (strcmp(startstr, "exec") == 0)
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
498 mode = 0;
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
499 else if (strcmp(startstr, "eval") == 0)
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
500 mode = 1;
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
501 else if (strcmp(startstr, "single") == 0)
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
502 mode = 2;
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
503 else {
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
504 PyErr_SetString(PyExc_ValueError,
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
505 "compile() arg 3 must be 'exec', 'eval' or 'single'");
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
506 return NULL;
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
507 }
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
508
57673
20b7deb53d4e account for PyObject_IsInstance's new ability to fail
Benjamin Peterson <benjamin@python.org>
parents: 57550
diff changeset
509 is_ast = PyAST_Check(cmd);
20b7deb53d4e account for PyObject_IsInstance's new ability to fail
Benjamin Peterson <benjamin@python.org>
parents: 57550
diff changeset
510 if (is_ast == -1)
20b7deb53d4e account for PyObject_IsInstance's new ability to fail
Benjamin Peterson <benjamin@python.org>
parents: 57550
diff changeset
511 return NULL;
20b7deb53d4e account for PyObject_IsInstance's new ability to fail
Benjamin Peterson <benjamin@python.org>
parents: 57550
diff changeset
512 if (is_ast) {
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
513 if (supplied_flags & PyCF_ONLY_AST) {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
514 Py_INCREF(cmd);
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
515 result = cmd;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
516 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
517 else {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
518 PyArena *arena;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
519 mod_ty mod;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
520
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
521 arena = PyArena_New();
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
522 mod = PyAST_obj2mod(cmd, arena, mode);
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
523 if (mod == NULL) {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
524 PyArena_Free(arena);
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
525 return NULL;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
526 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
527 result = (PyObject*)PyAST_Compile(mod, filename,
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
528 &cf, arena);
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
529 PyArena_Free(arena);
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
530 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
531 return result;
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
532 }
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
533
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
534 #ifdef Py_USING_UNICODE
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
535 if (PyUnicode_Check(cmd)) {
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
536 tmp = PyUnicode_AsUTF8String(cmd);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
537 if (tmp == NULL)
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
538 return NULL;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
539 cmd = tmp;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
540 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
541 }
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
542 #endif
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
543
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
544 if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length))
35960
658444baeba3 Fix memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 35766
diff changeset
545 goto cleanup;
46162
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
546 if ((size_t)length != strlen(str)) {
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
547 PyErr_SetString(PyExc_TypeError,
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
548 "compile() expected string without null bytes");
ca38f7a4b4c9 Patch #1810 by Thomas Lee, reviewed by myself:
Georg Brandl <georg@python.org>
parents: 46107
diff changeset
549 goto cleanup;
19355
d5acff178ad5 A fiddled version of the rest of Michael Hudson's SF patch
Tim Peters <tim.peters@gmail.com>
parents: 19343
diff changeset
550 }
46193
b0bf06d49647 Properly check for consistency with the third argument of
Georg Brandl <georg@python.org>
parents: 46162
diff changeset
551 result = Py_CompileStringFlags(str, filename, start[mode], &cf);
35960
658444baeba3 Fix memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 35766
diff changeset
552 cleanup:
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
553 Py_XDECREF(tmp);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
554 return result;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
555 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
556
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
557 PyDoc_STRVAR(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
558 "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
559 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
560 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
561 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
562 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
563 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
564 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
565 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
566 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
567 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
568 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
569 compile; if absent or zero these statements do influence the compilation,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
570 in addition to any features explicitly specified.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
571
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
572 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
573 builtin_dir(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 {
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
575 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
576
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
577 if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
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
578 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
579 return PyObject_Dir(arg);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
581
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
582 PyDoc_STRVAR(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
583 "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
584 "\n"
41125
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
585 "If called without an argument, return the names in the current scope.\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
586 "Else, return an alphabetized list of names comprising (some of) the attributes\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
587 "of the given object, and of attributes reachable from it.\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
588 "If the object supplies a method named __dir__, it will be used; otherwise\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
589 "the default dir() logic is used and returns:\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
590 " for a module object: the module's attributes.\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
591 " for a class object: its attributes, and recursively the attributes\n"
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
592 " of its bases.\n"
41140
7286d832ad27 Typo and grammar fixes.
Georg Brandl <georg@python.org>
parents: 41125
diff changeset
593 " for any other object: its attributes, its class's attributes, and\n"
41125
717bc2a4eee7 Backport from Py3k branch:
Georg Brandl <georg@python.org>
parents: 40113
diff changeset
594 " recursively the attributes of its class's base classes.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
595
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
596 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
597 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
598 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
599 PyObject *v, *w;
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
600
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
601 if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
602 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
603 return PyNumber_Divmod(v, w);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
605
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
606 PyDoc_STRVAR(divmod_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
607 "divmod(x, y) -> (div, mod)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
608 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
609 Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
610
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
611
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
612 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
613 builtin_eval(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 {
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
615 PyObject *cmd, *result, *tmp = NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
616 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
617 char *str;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
618 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
619
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
620 if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &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
621 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
622 if (locals != Py_None && !PyMapping_Check(locals)) {
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
623 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
32507
682f74088602 * Fix missing return after error message is set.
Raymond Hettinger <python@rcn.com>
parents: 32463
diff changeset
624 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
625 }
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
626 if (globals != Py_None && !PyDict_Check(globals)) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
627 PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
628 "globals must be a real dict; try eval(expr, {}, mapping)"
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
629 : "globals must be a dict");
32507
682f74088602 * Fix missing return after error message is set.
Raymond Hettinger <python@rcn.com>
parents: 32463
diff changeset
630 return NULL;
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
631 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
632 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
633 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
634 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
635 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
636 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
637 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
638 locals = globals;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
639
35567
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
640 if (globals == NULL || locals == NULL) {
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
641 PyErr_SetString(PyExc_TypeError,
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
642 "eval must be given globals and locals "
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
643 "when called without a frame");
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
644 return NULL;
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
645 }
fa0881fd49b7 bug [ 868706 ] Calling builtin function 'eval' from C causes seg fault.
Georg Brandl <georg@python.org>
parents: 35500
diff changeset
646
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
647 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
648 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
649 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
650 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
651 }
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
652
18885
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
diff changeset
653 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
654 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
655 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
656 "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
657 return NULL;
22456d0ee737 Fix for SF bug [ #443866 ] Evaluating func_code causing core dump
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18836
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 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
660 }
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
661
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
662 if (!PyString_Check(cmd) &&
14696
85038a024b19 This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents: 14662
diff changeset
663 !PyUnicode_Check(cmd)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
664 PyErr_SetString(PyExc_TypeError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
665 "eval() arg 1 must be a string or code object");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 }
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
668 cf.cf_flags = 0;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
669
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
670 #ifdef Py_USING_UNICODE
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
671 if (PyUnicode_Check(cmd)) {
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
672 tmp = PyUnicode_AsUTF8String(cmd);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
673 if (tmp == NULL)
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
674 return NULL;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
675 cmd = tmp;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
676 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
677 }
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
678 #endif
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
679 if (PyString_AsStringAndSize(cmd, &str, NULL)) {
35960
658444baeba3 Fix memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 35766
diff changeset
680 Py_XDECREF(tmp);
959
68b302ad84a1 use getargs() in more cases;
Guido van Rossum <guido@python.org>
parents: 955
diff changeset
681 return NULL;
35960
658444baeba3 Fix memory leaks
Neal Norwitz <nnorwitz@gmail.com>
parents: 35766
diff changeset
682 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
683 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
684 str++;
19357
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
685
be9b50704e4c Fix for bug [#452230] future division isn't propagated.
Tim Peters <tim.peters@gmail.com>
parents: 19355
diff changeset
686 (void)PyEval_MergeCompilerFlags(&cf);
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
687 result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
688 Py_XDECREF(tmp);
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27681
diff changeset
689 return result;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
691
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
692 PyDoc_STRVAR(eval_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
693 "eval(source[, globals[, locals]]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
694 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
695 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
696 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
697 or a code object as returned by compile().\n\
39746
97d3eab8cda2 Fix SF #1552093, eval docstring typo (3 ps in mapping)
Neal Norwitz <nnorwitz@gmail.com>
parents: 39447
diff changeset
698 The globals must be a dictionary and locals can be any mapping,\n\
32463
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
699 defaulting to the current globals and locals.\n\
d4a59230e93a SF Bug #215126: Over restricted type checking on eval() function
Raymond Hettinger <python@rcn.com>
parents: 31837
diff changeset
700 If only globals is given, locals defaults to it.\n");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
701
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
702
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
703 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
704 builtin_execfile(PyObject *self, PyObject *args)
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
705 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
706 char *filename;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
707 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
708 PyObject *res;
19726
5487d9d53a0e builtin_execfile(): initialize another local that the GCC on leroy
Guido van Rossum <guido@python.org>
parents: 19705
diff changeset
709 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
710 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
711 int exists;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
712
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
713 if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
714 1) < 0)
41834
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
715 return NULL;
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
716
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
717 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
718 &filename,
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
719 &PyDict_Type, &globals,
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
720 &locals))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
721 return NULL;
32890
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
722 if (locals != Py_None && !PyMapping_Check(locals)) {
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
723 PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
724 return NULL;
b8c2aa2be19a Completed the patch for Bug #215126.
Raymond Hettinger <python@rcn.com>
parents: 32759
diff changeset
725 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
726 if (globals == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
727 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
728 if (locals == Py_None)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
729 locals = PyEval_GetLocals();
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
730 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
731 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
732 locals = globals;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
733 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
734 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
735 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
736 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
737 }
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
738
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
739 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
740 /* Test for existence or directory. */
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
741 #if defined(PLAN9)
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
742 {
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
743 Dir *d;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
744
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
745 if ((d = dirstat(filename))!=nil) {
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
746 if(d->mode & DMDIR)
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
747 werrstr("is a directory");
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
748 else
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
749 exists = 1;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
750 free(d);
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
751 }
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
752 }
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
753 #elif defined(RISCOS)
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
754 if (object_exists(filename)) {
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
755 if (isdir(filename))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
756 errno = EISDIR;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
757 else
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
758 exists = 1;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20471
diff changeset
759 }
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
760 #else /* standard Posix */
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
761 {
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
762 struct stat s;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
763 if (stat(filename, &s) == 0) {
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
764 if (S_ISDIR(s.st_mode))
31837
81117923ae8f OS/2 VACPP build updates/fixes
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 31445
diff changeset
765 # if defined(PYOS_OS2) && defined(PYCC_VACPP)
22205
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
766 errno = EOS2ERR;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
767 # else
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
768 errno = EISDIR;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
769 # endif
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
770 else
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
771 exists = 1;
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
772 }
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
773 }
f4487350c537 Patch #494045: patches errno and stat to cope on plan9.
Martin v. Löwis <martin@v.loewis.de>
parents: 22225
diff changeset
774 #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
775
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
776 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
777 Py_BEGIN_ALLOW_THREADS
23029
8291473fde3e Mass checkin of universal newline support.
Jack Jansen <jack.jansen@cwi.nl>
parents: 22785
diff changeset
778 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
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
779 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
780
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
781 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
782 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
783 }
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
784 }
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
785
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
786 if (!exists) {
25046
434bc1dea4f8 execfile should call PyErr_SetFromErrnoWithFilename instead of
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 24863
diff changeset
787 PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
788 return NULL;
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
789 }
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18436
diff changeset
790 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
791 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
792 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
793 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
794 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
795 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
796 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
797 return res;
673
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
798 }
71d496259ad3 Added execfile().
Guido van Rossum <guido@python.org>
parents: 662
diff changeset
799
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
800 PyDoc_STRVAR(execfile_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
801 "execfile(filename[, globals[, locals]])\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
802 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
803 Read and execute a Python script from a file.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
804 The globals and locals are dictionaries, defaulting to the current\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
805 globals and locals. If only globals is given, locals defaults to it.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
806
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
807
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
808 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
809 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
810 {
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
811 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
812 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
813
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
814 if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
815 return NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
816 #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
817 if (PyUnicode_Check(name)) {
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
818 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
819 if (name == NULL)
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
820 return NULL;
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
821 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
822 #endif
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
823
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
824 if (!PyString_Check(name)) {
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
825 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
826 "getattr(): attribute name must be string");
18888
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
827 return NULL;
f12e1a766a14 Fix for SF byg [ #420304 ] getattr function w/ default
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18885
diff changeset
828 }
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
829 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
830 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
831 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
832 {
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
833 PyErr_Clear();
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
834 Py_INCREF(dflt);
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
835 result = dflt;
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
836 }
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
837 return result;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
838 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
839
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
840 PyDoc_STRVAR(getattr_doc,
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
841 "getattr(object, name[, default]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
842 \n\
8581
221667f89064 Experimental feature: add default argument to getattr().
Guido van Rossum <guido@python.org>
parents: 8561
diff changeset
843 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
844 When a default argument is given, it is returned when the attribute doesn't\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
845 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
846
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
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 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
849 builtin_globals(PyObject *self)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
850 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
851 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
852
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
853 d = PyEval_GetGlobals();
39447
71b0ac80a135 It's highly unlikely, though possible for PyEval_Get*() to return NULLs.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39385
diff changeset
854 Py_XINCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
855 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
856 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
857
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
858 PyDoc_STRVAR(globals_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
859 "globals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
860 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
861 Return the dictionary containing the current scope's global variables.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
862
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
863
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
864 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
865 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
866 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
867 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
868 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
869
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
870 if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
871 return NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
872 #ifdef Py_USING_UNICODE
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
873 if (PyUnicode_Check(name)) {
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
874 name = _PyUnicode_AsDefaultEncodedString(name, NULL);
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
875 if (name == NULL)
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
876 return NULL;
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
877 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
878 #endif
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
879
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
880 if (!PyString_Check(name)) {
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
881 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
882 "hasattr(): attribute name must be string");
18890
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
883 return NULL;
fb837c5ae5d3 Do for hasattr() what was done for getattr()
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18888
diff changeset
884 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
885 v = PyObject_GetAttr(v, name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
886 if (v == NULL) {
46925
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
887 if (!PyErr_ExceptionMatches(PyExc_Exception))
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
888 return NULL;
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
889 else {
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
890 PyErr_Clear();
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
891 Py_INCREF(Py_False);
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
892 return Py_False;
4c8375574aa9 #2196 hasattr now allows SystemExit and KeyboardInterrupt to propagate
Benjamin Peterson <benjamin@python.org>
parents: 46607
diff changeset
893 }
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
894 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
895 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
896 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
897 return Py_True;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
898 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
899
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
900 PyDoc_STRVAR(hasattr_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
901 "hasattr(object, name) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
902 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
903 Return whether the object has an attribute with the given name.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
904 (This is done by calling getattr(object, name) and catching exceptions.)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
905
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
906
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
907 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
908 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
909 {
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
910 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
911 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
912
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
913 PyDoc_STRVAR(id_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
914 "id(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
915 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
916 Return the identity of an object. This is guaranteed to be unique among\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
917 simultaneously existing objects. (Hint: it's the object's memory address.)");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
918
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
919
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
920 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
921 builtin_map(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
922 {
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
923 typedef struct {
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
924 PyObject *it; /* the iterator object */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
925 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
926 } sequence;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
927
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
928 PyObject *func, *result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
929 sequence *seqs = NULL, *sqp;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
930 Py_ssize_t n, len;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
931 register int i, j;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
932
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
933 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
934 if (n < 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
935 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
936 "map() requires at least two args");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
937 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
938 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
939
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
940 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
941 n--;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
942
45496
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
943 if (func == Py_None) {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
944 if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
945 "use list(...)", 1) < 0)
45496
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
946 return NULL;
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
947 if (n == 1) {
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
948 /* map(None, S) is the same as list(S). */
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
949 return PySequence_List(PyTuple_GetItem(args, 1));
002a5ac792b1 map(None, ...) is not supported in 3.0.
Neal Norwitz <nnorwitz@gmail.com>
parents: 45433
diff changeset
950 }
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
951 }
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
952
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
953 /* 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
954 * 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
955 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
956 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
957 PyErr_NoMemory();
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
958 return NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
959 }
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
960 for (i = 0; i < n; ++i) {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
961 seqs[i].it = (PyObject*)NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
962 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
963 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
964
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
965 /* 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
966 * 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
967 */
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
968 len = 0;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
969 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
970 PyObject *curseq;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
971 Py_ssize_t curlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
972
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
973 /* Get iterator. */
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
974 curseq = PyTuple_GetItem(args, i+1);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
975 sqp->it = PyObject_GetIter(curseq);
55871
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
976 if (sqp->it == NULL) {
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
977 static char errmsg[] =
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
978 "argument %d to map() must support iteration";
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
979 char errbuf[sizeof(errmsg) + 25];
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
980 PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
981 PyErr_SetString(PyExc_TypeError, errbuf);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
982 goto Fail_2;
55871
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
983 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
984
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
985 /* Update len. */
44193
44c090c74202 Fix Issue 1045.
Raymond Hettinger <python@rcn.com>
parents: 43675
diff changeset
986 curlen = _PyObject_LengthHint(curseq, 8);
31343
0c1d2062dee3 Apply map/zip pre-sizing optimization to a broader class of objects.
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
987 if (curlen > len)
0c1d2062dee3 Apply map/zip pre-sizing optimization to a broader class of objects.
Raymond Hettinger <python@rcn.com>
parents: 31210
diff changeset
988 len = curlen;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
989 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
990
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
991 /* 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
992 if ((result = (PyObject *) PyList_New(len)) == NULL)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
993 goto Fail_2;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
994
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
995 /* 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
996 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
997 PyObject *alist, *item=NULL, *value;
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
998 int numactive = 0;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
999
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1000 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
1001 alist = NULL;
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1002 else if ((alist = PyTuple_New(n)) == NULL)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1003 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1004
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1005 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
1006 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
1007 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1008 item = Py_None;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1009 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1010 else {
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1011 item = PyIter_Next(sqp->it);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1012 if (item)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1013 ++numactive;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1014 else {
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1015 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
1016 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
1017 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
1018 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1019 Py_INCREF(Py_None);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1020 item = Py_None;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1021 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
1022 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1023 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1024 if (alist)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1025 PyTuple_SET_ITEM(alist, j, item);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1026 else
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1027 break;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1028 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1029
2824
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1030 if (!alist)
65c7105868f8 rename arglist to alist (conflict with new grammar symbol)
Guido van Rossum <guido@python.org>
parents: 2808
diff changeset
1031 alist = item;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1032
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1033 if (numactive == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1034 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
1035 break;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1036 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1037
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1038 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
1039 value = alist;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1040 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1041 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
1042 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
1043 if (value == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
1044 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
1045 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1046 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
1047 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
1048 Py_DECREF(value);
ddbbed8cb830 builtin_map(): A better fix for the previous leak plug (remember
Barry Warsaw <barry@python.org>
parents: 9980
diff changeset
1049 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
1050 goto Fail_1;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1051 }
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1052 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
1053 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1054 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1055
8661
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
1056 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
1057 goto Fail_1;
30fbf18af29e Small changes to map() and filter():
Guido van Rossum <guido@python.org>
parents: 8589
diff changeset
1058
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1059 goto Succeed;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1060
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1061 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1062 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1063 Fail_2:
17675
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1064 result = NULL;
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1065 Succeed:
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1066 assert(seqs);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1067 for (i = 0; i < n; ++i)
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1068 Py_XDECREF(seqs[i].it);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1069 PyMem_DEL(seqs);
0be277039d93 Generalize map() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17667
diff changeset
1070 return result;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1071 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1072
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1073 PyDoc_STRVAR(map_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1074 "map(function, sequence[, sequence, ...]) -> list\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1075 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1076 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
1077 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
1078 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
1079 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
1080 sequences have the same length. If the function is None, return a list of\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1081 the items of the sequence (or a list of tuples if more than one sequence).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1082
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1083
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1084 static PyObject *
46607
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1085 builtin_next(PyObject *self, PyObject *args)
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1086 {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1087 PyObject *it, *res;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1088 PyObject *def = NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1089
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1090 if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1091 return NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1092 if (!PyIter_Check(it)) {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1093 PyErr_Format(PyExc_TypeError,
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1094 "%.200s object is not an iterator",
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1095 it->ob_type->tp_name);
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1096 return NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1097 }
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1098
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1099 res = (*it->ob_type->tp_iternext)(it);
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1100 if (res != NULL) {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1101 return res;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1102 } else if (def != NULL) {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1103 if (PyErr_Occurred()) {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1104 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1105 return NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1106 PyErr_Clear();
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1107 }
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1108 Py_INCREF(def);
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1109 return def;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1110 } else if (PyErr_Occurred()) {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1111 return NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1112 } else {
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1113 PyErr_SetNone(PyExc_StopIteration);
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1114 return NULL;
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1115 }
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1116 }
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1117
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1118 PyDoc_STRVAR(next_doc,
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1119 "next(iterator[, default])\n\
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1120 \n\
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1121 Return the next item from the iterator. If default is given and the iterator\n\
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1122 is exhausted, it is returned instead of raising StopIteration.");
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1123
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1124
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
1125 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1126 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
1127 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1128 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1129 PyObject *name;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1130 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
1131
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1132 if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1133 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1134 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
1135 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1136 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1137 return Py_None;
644
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1138 }
59f9325bb8ec Added getattr and setattr built-in functions.
Guido van Rossum <guido@python.org>
parents: 606
diff changeset
1139
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1140 PyDoc_STRVAR(setattr_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1141 "setattr(object, name, value)\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1142 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1143 Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1144 ``x.y = v''.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1145
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1146
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1147 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1148 builtin_delattr(PyObject *self, PyObject *args)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1149 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1150 PyObject *v;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1151 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
1152
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1153 if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1154 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1155 if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1156 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1157 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1158 return Py_None;
1843
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1159 }
8294bbd94535 Added delattr()
Guido van Rossum <guido@python.org>
parents: 1842
diff changeset
1160
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1161 PyDoc_STRVAR(delattr_doc,
9514
a126107c433a Fix cosmetic bug in delattr docstring discovered by JvR.
Guido van Rossum <guido@python.org>
parents: 9188
diff changeset
1162 "delattr(object, name)\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1163 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1164 Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1165 ``del x.y''.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1166
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1167
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1168 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
1169 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
1170 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1171 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
1172
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1173 x = PyObject_Hash(v);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1174 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1175 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1176 return PyInt_FromLong(x);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1177 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
1178
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1179 PyDoc_STRVAR(hash_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1180 "hash(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1181 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1182 Return a hash value for the object. Two objects with the same value have\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1183 the same hash value. The reverse is not necessarily true, but likely.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1184
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1185
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1186 static PyObject *
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
1187 builtin_hex(PyObject *self, PyObject *v)
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1188 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1189 PyNumberMethods *nb;
32759
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1190 PyObject *res;
47139
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1191
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1192 if ((nb = v->ob_type->tp_as_number) == NULL ||
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1193 nb->nb_hex == NULL) {
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1194 PyErr_SetString(PyExc_TypeError,
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1195 "hex() argument can't be converted to hex");
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1196 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1197 }
47139
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1198 res = (*nb->nb_hex)(v);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1199 if (res && !PyString_Check(res)) {
47139
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1200 PyErr_Format(PyExc_TypeError,
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1201 "__hex__ returned non-string (type %.200s)",
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1202 res->ob_type->tp_name);
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1203 Py_DECREF(res);
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1204 return NULL;
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1205 }
d953ea58cc8b revert 63425 over Guido's Febuary message about this, that I missed
Benjamin Peterson <benjamin@python.org>
parents: 47137
diff changeset
1206 return res;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1207 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1208
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1209 PyDoc_STRVAR(hex_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1210 "hex(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1211 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1212 Return the hexadecimal representation of an integer or long integer.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1213
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1214
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13446
diff changeset
1215 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
1216
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1217 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1218 builtin_input(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1220 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
1221 char *str;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1222 PyObject *res;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1223 PyObject *globals, *locals;
31445
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31345
diff changeset
1224 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
1225
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1226 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
1227 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
1228 return line;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1229 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
1230 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1231 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
1232 str++;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1233 globals = PyEval_GetGlobals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1234 locals = PyEval_GetLocals();
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1235 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
1236 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
1237 PyEval_GetBuiltins()) != 0)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
1238 return NULL;
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2087
diff changeset
1239 }
31445
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31345
diff changeset
1240 cf.cf_flags = 0;
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31345
diff changeset
1241 PyEval_MergeCompilerFlags(&cf);
1219f1162d1d Fix input() builtin function to respect compiler flags.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 31345
diff changeset
1242 res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1243 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
1244 return res;
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1247 PyDoc_STRVAR(input_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1248 "input([prompt]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1249 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1250 Equivalent to eval(raw_input(prompt)).");
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
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
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_intern(PyObject *self, PyObject *args)
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1255 {
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1256 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
1257 if (!PyArg_ParseTuple(args, "S:intern", &s))
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1258 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1259 if (!PyString_CheckExact(s)) {
33019
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32896
diff changeset
1260 PyErr_SetString(PyExc_TypeError,
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32896
diff changeset
1261 "can't intern subclass of string");
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32896
diff changeset
1262 return NULL;
5218bff56449 Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 32896
diff changeset
1263 }
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1264 Py_INCREF(s);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1265 PyString_InternInPlace(&s);
5092
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1266 return s;
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1267 }
cb7aadd9d9f2 Added intern() function.
Guido van Rossum <guido@python.org>
parents: 5010
diff changeset
1268
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1269 PyDoc_STRVAR(intern_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1270 "intern(string) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1271 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1272 ``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
1273 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
1274 Return the string itself or the previously interned string object with the\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1275 same value.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1276
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1277
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1278 static PyObject *
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1279 builtin_iter(PyObject *self, PyObject *args)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1280 {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1281 PyObject *v, *w = NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1282
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1283 if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1284 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1285 if (w == NULL)
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1286 return PyObject_GetIter(v);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1287 if (!PyCallable_Check(v)) {
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1288 PyErr_SetString(PyExc_TypeError,
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1289 "iter(v, w): v must be callable");
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1290 return NULL;
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1291 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1292 return PyCallIter_New(v, w);
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1293 }
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1294
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1295 PyDoc_STRVAR(iter_doc,
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1296 "iter(collection) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1297 iter(callable, sentinel) -> iterator\n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1298 \n\
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1299 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
1300 supply its own iterator, or be a sequence.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1301 In the second form, the callable is called until it returns the sentinel.");
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1302
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1303
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17337
diff changeset
1304 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
1305 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
1306 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1307 Py_ssize_t res;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1308
13576
61e4c29d53d8 replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 13447
diff changeset
1309 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
1310 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
1311 return NULL;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
1312 return PyInt_FromSsize_t(res);
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
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1315 PyDoc_STRVAR(len_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1316 "len(object) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1317 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1318 Return the number of items of a sequence or mapping.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1319
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1320
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1321 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
1322 builtin_locals(PyObject *self)
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1323 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1324 PyObject *d;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1325
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1326 d = PyEval_GetLocals();
39447
71b0ac80a135 It's highly unlikely, though possible for PyEval_Get*() to return NULLs.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39385
diff changeset
1327 Py_XINCREF(d);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1328 return d;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1329 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
1330
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1331 PyDoc_STRVAR(locals_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1332 "locals() -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1333 \n\
27076
753306f9b0a0 SF bug #655271: Slightly modify locals() doc
Raymond Hettinger <python@rcn.com>
parents: 26824
diff changeset
1334 Update and return a dictionary containing the current scope's local variables.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1335
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1336
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1337 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1338 min_max(PyObject *args, PyObject *kwds, int op)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1340 PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1341 const char *name = op == Py_LT ? "min" : "max";
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1342
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1343 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
1344 v = args;
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1345 else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 return NULL;
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
1347
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1348 if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1349 keyfunc = PyDict_GetItemString(kwds, "key");
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1350 if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
1351 PyErr_Format(PyExc_TypeError,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1352 "%s() got an unexpected keyword argument", name);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1353 return NULL;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1354 }
44894
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1355 Py_INCREF(keyfunc);
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
1356 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1357
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1358 it = PyObject_GetIter(v);
44894
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1359 if (it == NULL) {
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1360 Py_XDECREF(keyfunc);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 return NULL;
44894
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1362 }
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1363
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1364 maxitem = NULL; /* the result */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1365 maxval = NULL; /* the value associated with the result */
34123
4e8fb12f8378 Put parentheses around the assignment in the 'while' loop conditional
Brett Cannon <bcannon@gmail.com>
parents: 34085
diff changeset
1366 while (( item = PyIter_Next(it) )) {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1367 /* get the value from the key function */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1368 if (keyfunc != NULL) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1369 val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1370 if (val == NULL)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1371 goto Fail_it_item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1372 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1373 /* no key function; the value is the item */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1374 else {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1375 val = item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1376 Py_INCREF(val);
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1377 }
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1378
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1379 /* maximum value and item are unset; set them */
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1380 if (maxval == NULL) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1381 maxitem = item;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1382 maxval = val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1383 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1384 /* maximum value and item are set; update them as necessary */
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1385 else {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1386 int cmp = PyObject_RichCompareBool(val, maxval, op);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1387 if (cmp < 0)
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1388 goto Fail_it_item_and_val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1389 else if (cmp > 0) {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1390 Py_DECREF(maxval);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1391 Py_DECREF(maxitem);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1392 maxval = val;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1393 maxitem = item;
15949
76ad2a9a3e53 Use rich comparisons in min and max.
Guido van Rossum <guido@python.org>
parents: 15832
diff changeset
1394 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1395 else {
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1396 Py_DECREF(item);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1397 Py_DECREF(val);
5610
10e9cfa010f0 PyObject_Compare can raise an exception now.
Guido van Rossum <guido@python.org>
parents: 5471
diff changeset
1398 }
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
1399 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 }
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1401 if (PyErr_Occurred())
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1402 goto Fail_it;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1403 if (maxval == NULL) {
33099
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1404 PyErr_Format(PyExc_ValueError,
ce016d0ec701 Patch #1005468: Disambiguate "min() or max()" exception string.
Martin v. Löwis <martin@v.loewis.de>
parents: 33019
diff changeset
1405 "%s() arg is an empty sequence", name);
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1406 assert(maxitem == NULL);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1407 }
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1408 else
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1409 Py_DECREF(maxval);
17667
76ee8b62ee2a Generalize max(seq) and min(seq) to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17654
diff changeset
1410 Py_DECREF(it);
44894
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1411 Py_XDECREF(keyfunc);
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1412 return maxitem;
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1413
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1414 Fail_it_item_and_val:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1415 Py_DECREF(val);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1416 Fail_it_item:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1417 Py_DECREF(item);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1418 Fail_it:
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1419 Py_XDECREF(maxval);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1420 Py_XDECREF(maxitem);
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1421 Py_DECREF(it);
44894
4c8308ac20cd Fix two crashers.
Guido van Rossum <guido@python.org>
parents: 44747
diff changeset
1422 Py_XDECREF(keyfunc);
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1423 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1425
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1426 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1427 builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1428 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1429 return min_max(args, kwds, Py_LT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1430 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1431
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1432 PyDoc_STRVAR(min_doc,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1433 "min(iterable[, key=func]) -> value\n\
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1434 min(a, b, c, ...[, key=func]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1435 \n\
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1436 With a single iterable argument, return its smallest item.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1437 With two or more arguments, return the smallest argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1438
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1439
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1440 static PyObject *
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1441 builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 {
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1443 return min_max(args, kwds, Py_GT);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1445
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1446 PyDoc_STRVAR(max_doc,
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1447 "max(iterable[, key=func]) -> value\n\
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1448 max(a, b, c, ...[, key=func]) -> value\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1449 \n\
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
1450 With a single iterable argument, return its largest item.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1451 With two or more arguments, return the largest argument.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1452
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1453
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1454 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
1455 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
1456 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1457 PyNumberMethods *nb;
32759
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1458 PyObject *res;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1459
1049
2ed511171809 Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents: 972
diff changeset
1460 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
1461 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
1462 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
1463 "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
1464 return NULL;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1465 }
32759
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1466 res = (*nb->nb_oct)(v);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1467 if (res && !PyString_Check(res)) {
32759
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1468 PyErr_Format(PyExc_TypeError,
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1469 "__oct__ returned non-string (type %.200s)",
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1470 res->ob_type->tp_name);
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1471 Py_DECREF(res);
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1472 return NULL;
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1473 }
d1777f51923b Check the type of values returned by __int__, __float__, __long__,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 32526
diff changeset
1474 return res;
404
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1475 }
9cd38d1ac391 Made dir() more robust.
Guido van Rossum <guido@python.org>
parents: 385
diff changeset
1476
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1477 PyDoc_STRVAR(oct_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1478 "oct(number) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1479 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1480 Return the octal representation of an integer or long integer.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1481
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
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 static PyObject *
38077
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1484 builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1485 {
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1486 return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1487 }
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1488
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1489 PyDoc_STRVAR(open_doc,
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1490 "open(name[, mode[, buffering]]) -> file object\n\
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1491 \n\
44222
9a976a6ee936 Note that open() is the preferred way to open files (issue 1510).
Skip Montanaro <skip@pobox.com>
parents: 44193
diff changeset
1492 Open a file using the file() type, returns a file object. This is the\n\
54383
821f7d42bf74 further hint to where the open docs really are
Philip Jenvey <pjenvey@underboss.org>
parents: 53956
diff changeset
1493 preferred way to open a file. See file.__doc__ for further information.");
38077
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1494
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1495
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
1496 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
1497 builtin_ord(PyObject *self, PyObject* obj)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1498 {
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1499 long ord;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1500 Py_ssize_t size;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1501
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1502 if (PyString_Check(obj)) {
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1503 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
1504 if (size == 1) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
1505 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
1506 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
1507 }
47291
04bd3690fb1f First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents: 47139
diff changeset
1508 } else if (PyByteArray_Check(obj)) {
04bd3690fb1f First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents: 47139
diff changeset
1509 size = PyByteArray_GET_SIZE(obj);
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
1510 if (size == 1) {
47291
04bd3690fb1f First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents: 47139
diff changeset
1511 ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
46107
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
1512 return PyInt_FromLong(ord);
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
1513 }
5341b30b1812 Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46075
diff changeset
1514
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1515 #ifdef Py_USING_UNICODE
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1516 } else if (PyUnicode_Check(obj)) {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1517 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
1518 if (size == 1) {
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1519 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
1520 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
1521 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
1522 #endif
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1523 } else {
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1524 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
1525 "ord() expected string of length 1, but " \
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1526 "%.200s found", obj->ob_type->tp_name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 return NULL;
11973
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1528 }
8bdc25d22925 Marc-Andre Lemburg: added new builtin functions unicode() and
Guido van Rossum <guido@python.org>
parents: 11882
diff changeset
1529
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
1530 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
1531 "ord() expected a character, "
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
1532 "but string of length %zd found",
12384
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1533 size);
aa013f2cf80b ord: provide better error messages
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12368
diff changeset
1534 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1535 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1536
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1537 PyDoc_STRVAR(ord_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1538 "ord(c) -> integer\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1539 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1540 Return the integer ordinal of a one-character string.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1541
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1542
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1543 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1544 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
1545 {
8402
0cb0394c2edc A bunch of functions are now properly implemented in abstract.c, and
Guido van Rossum <guido@python.org>
parents: 8287
diff changeset
1546 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
1547
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1548 if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
2087
65146b52f16b * Python/bltinmodule.c: restructured coerce(), divmod(), pow() to
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1549 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
1550 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
1551 }
6693c5d87f3a Added long() and support for longs in int() and float();
Guido van Rossum <guido@python.org>
parents: 182
diff changeset
1552
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1553 PyDoc_STRVAR(pow_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1554 "pow(x, y[, z]) -> number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1555 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1556 With two arguments, equivalent to x**y. With three arguments,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1557 equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1558
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1559
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1560 static PyObject *
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1561 builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1562 {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1563 static char *kwlist[] = {"sep", "end", "file", 0};
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1564 static PyObject *dummy_args = NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1565 static PyObject *unicode_newline = NULL, *unicode_space = NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1566 static PyObject *str_newline = NULL, *str_space = NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1567 PyObject *newline, *space;
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1568 PyObject *sep = NULL, *end = NULL, *file = NULL;
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1569 int i, err, use_unicode = 0;
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1570
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1571 if (dummy_args == NULL) {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1572 if (!(dummy_args = PyTuple_New(0)))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1573 return NULL;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1574 }
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1575 if (str_newline == NULL) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1576 str_newline = PyString_FromString("\n");
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1577 if (str_newline == NULL)
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1578 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1579 str_space = PyString_FromString(" ");
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1580 if (str_space == NULL) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1581 Py_CLEAR(str_newline);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1582 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1583 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1584 unicode_newline = PyUnicode_FromString("\n");
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1585 if (unicode_newline == NULL) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1586 Py_CLEAR(str_newline);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1587 Py_CLEAR(str_space);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1588 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1589 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1590 unicode_space = PyUnicode_FromString(" ");
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1591 if (unicode_space == NULL) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1592 Py_CLEAR(str_newline);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1593 Py_CLEAR(str_space);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1594 Py_CLEAR(unicode_space);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1595 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1596 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1597 }
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1598 if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1599 kwlist, &sep, &end, &file))
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1600 return NULL;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1601 if (file == NULL || file == Py_None) {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1602 file = PySys_GetObject("stdout");
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1603 /* sys.stdout may be None when FILE* stdout isn't connected */
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1604 if (file == Py_None)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1605 Py_RETURN_NONE;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1606 }
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1607 if (sep == Py_None) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1608 sep = NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1609 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1610 else if (sep) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1611 if (PyUnicode_Check(sep)) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1612 use_unicode = 1;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1613 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1614 else if (!PyString_Check(sep)) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1615 PyErr_Format(PyExc_TypeError,
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1616 "sep must be None, str or unicode, not %.200s",
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1617 sep->ob_type->tp_name);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1618 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1619 }
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1620 }
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1621 if (end == Py_None)
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1622 end = NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1623 else if (end) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1624 if (PyUnicode_Check(end)) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1625 use_unicode = 1;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1626 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1627 else if (!PyString_Check(end)) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1628 PyErr_Format(PyExc_TypeError,
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1629 "end must be None, str or unicode, not %.200s",
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1630 end->ob_type->tp_name);
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1631 return NULL;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1632 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1633 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1634
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1635 if (!use_unicode) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1636 for (i = 0; i < PyTuple_Size(args); i++) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1637 if (PyUnicode_Check(PyTuple_GET_ITEM(args, i))) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1638 use_unicode = 1;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1639 break;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1640 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1641 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1642 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1643 if (use_unicode) {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1644 newline = unicode_newline;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1645 space = unicode_space;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1646 }
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1647 else {
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1648 newline = str_newline;
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1649 space = str_space;
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1650 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1651
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1652 for (i = 0; i < PyTuple_Size(args); i++) {
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1653 if (i > 0) {
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1654 if (sep == NULL)
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1655 err = PyFile_WriteObject(space, file,
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1656 Py_PRINT_RAW);
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1657 else
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1658 err = PyFile_WriteObject(sep, file,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1659 Py_PRINT_RAW);
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1660 if (err)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1661 return NULL;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1662 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1663 err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1664 Py_PRINT_RAW);
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1665 if (err)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1666 return NULL;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1667 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1668
55053
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1669 if (end == NULL)
0653c1a0cb1e when print() gets unicode arguments, sep and end should be unicode by default #4618
Benjamin Peterson <benjamin@python.org>
parents: 54383
diff changeset
1670 err = PyFile_WriteObject(newline, file, Py_PRINT_RAW);
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1671 else
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1672 err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1673 if (err)
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1674 return NULL;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1675
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1676 Py_RETURN_NONE;
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1677 }
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1678
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1679 PyDoc_STRVAR(print_doc,
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1680 "print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1681 \n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1682 Prints the values to a stream, or to sys.stdout by default.\n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1683 Optional keyword arguments:\n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1684 file: a file-like object (stream); defaults to the current sys.stdout.\n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1685 sep: string inserted between values, default a space.\n\
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1686 end: string appended after the last value, default a newline.");
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
1687
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1688
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1689 /* Return number of items in range (lo, hi, step), when arguments are
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1690 * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1691 * & only if the true value is too large to fit in a signed long.
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1692 * Arguments MUST return 1 with either PyInt_Check() or
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1693 * PyLong_Check(). Return -1 when there is an error.
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1694 */
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1695 static long
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1696 get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1697 {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1698 /* -------------------------------------------------------------
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1699 Algorithm is equal to that of get_len_of_range(), but it operates
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1700 on PyObjects (which are assumed to be PyLong or PyInt objects).
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1701 ---------------------------------------------------------------*/
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1702 long n;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1703 PyObject *diff = NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1704 PyObject *one = NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1705 PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1706 /* holds sub-expression evaluations */
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1707
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1708 /* if (lo >= hi), return length of 0. */
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1709 if (PyObject_Compare(lo, hi) >= 0)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1710 return 0;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1711
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1712 if ((one = PyLong_FromLong(1L)) == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1713 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1714
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1715 if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1716 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1717
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1718 if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1719 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1720
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1721 if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1722 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1723
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1724 if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1725 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1726
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1727 n = PyLong_AsLong(tmp3);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1728 if (PyErr_Occurred()) { /* Check for Overflow */
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1729 PyErr_Clear();
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1730 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1731 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1732
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1733 Py_DECREF(tmp3);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1734 Py_DECREF(tmp2);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1735 Py_DECREF(diff);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1736 Py_DECREF(tmp1);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1737 Py_DECREF(one);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1738 return n;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1739
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1740 Fail:
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1741 Py_XDECREF(tmp3);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1742 Py_XDECREF(tmp2);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1743 Py_XDECREF(diff);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1744 Py_XDECREF(tmp1);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1745 Py_XDECREF(one);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1746 return -1;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1747 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1748
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1749 /* An extension of builtin_range() that handles the case when PyLong
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1750 * arguments are given. */
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1751 static PyObject *
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1752 handle_range_longs(PyObject *self, PyObject *args)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1753 {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1754 PyObject *ilow;
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1755 PyObject *ihigh = NULL;
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1756 PyObject *istep = NULL;
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1757
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1758 PyObject *curnum = NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1759 PyObject *v = NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1760 long bign;
57550
75e1b8c05f43 Issue #6985: number of range() items should be constrained to lie
Mark Dickinson <dickinsm@gmail.com>
parents: 57422
diff changeset
1761 Py_ssize_t i, n;
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1762 int cmp_result;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1763
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1764 PyObject *zero = PyLong_FromLong(0);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1765
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1766 if (zero == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1767 return NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1768
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1769 if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1770 Py_DECREF(zero);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1771 return NULL;
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1772 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1773
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1774 /* Figure out which way we were called, supply defaults, and be
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1775 * sure to incref everything so that the decrefs at the end
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1776 * are correct.
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1777 */
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1778 assert(ilow != NULL);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1779 if (ihigh == NULL) {
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1780 /* only 1 arg -- it's the upper limit */
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1781 ihigh = ilow;
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1782 ilow = NULL;
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1783 }
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1784 assert(ihigh != NULL);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1785 Py_INCREF(ihigh);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1786
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1787 /* ihigh correct now; do ilow */
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1788 if (ilow == NULL)
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1789 ilow = zero;
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1790 Py_INCREF(ilow);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1791
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1792 /* ilow and ihigh correct now; do istep */
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1793 if (istep == NULL) {
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1794 istep = PyLong_FromLong(1L);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1795 if (istep == NULL)
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1796 goto Fail;
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1797 }
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1798 else {
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1799 Py_INCREF(istep);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1800 }
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1801
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1802 if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) {
28612
0792caa6371c Some errors from range() should be TypeError, not ValueError.
Guido van Rossum <guido@python.org>
parents: 28604
diff changeset
1803 PyErr_Format(PyExc_TypeError,
28738
52f3d90c17e8 complete and clarify some error messages for range()
Alex Martelli <aleaxit@gmail.com>
parents: 28718
diff changeset
1804 "range() integer start argument expected, got %s.",
28604
eee4969adbae Prompted by Tim's comment, when handle_range_longs() sees an
Guido van Rossum <guido@python.org>
parents: 28597
diff changeset
1805 ilow->ob_type->tp_name);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1806 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1807 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1808
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1809 if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) {
28612
0792caa6371c Some errors from range() should be TypeError, not ValueError.
Guido van Rossum <guido@python.org>
parents: 28604
diff changeset
1810 PyErr_Format(PyExc_TypeError,
28738
52f3d90c17e8 complete and clarify some error messages for range()
Alex Martelli <aleaxit@gmail.com>
parents: 28718
diff changeset
1811 "range() integer end argument expected, got %s.",
28604
eee4969adbae Prompted by Tim's comment, when handle_range_longs() sees an
Guido van Rossum <guido@python.org>
parents: 28597
diff changeset
1812 ihigh->ob_type->tp_name);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1813 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1814 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1815
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1816 if (!PyInt_Check(istep) && !PyLong_Check(istep)) {
28612
0792caa6371c Some errors from range() should be TypeError, not ValueError.
Guido van Rossum <guido@python.org>
parents: 28604
diff changeset
1817 PyErr_Format(PyExc_TypeError,
28738
52f3d90c17e8 complete and clarify some error messages for range()
Alex Martelli <aleaxit@gmail.com>
parents: 28718
diff changeset
1818 "range() integer step argument expected, got %s.",
28604
eee4969adbae Prompted by Tim's comment, when handle_range_longs() sees an
Guido van Rossum <guido@python.org>
parents: 28597
diff changeset
1819 istep->ob_type->tp_name);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1820 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1821 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1822
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1823 if (PyObject_Cmp(istep, zero, &cmp_result) == -1)
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1824 goto Fail;
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1825 if (cmp_result == 0) {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1826 PyErr_SetString(PyExc_ValueError,
28738
52f3d90c17e8 complete and clarify some error messages for range()
Alex Martelli <aleaxit@gmail.com>
parents: 28718
diff changeset
1827 "range() step argument must not be zero");
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1828 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1829 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1830
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1831 if (cmp_result > 0)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1832 bign = get_len_of_range_longs(ilow, ihigh, istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1833 else {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1834 PyObject *neg_istep = PyNumber_Negative(istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1835 if (neg_istep == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1836 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1837 bign = get_len_of_range_longs(ihigh, ilow, neg_istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1838 Py_DECREF(neg_istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1839 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1840
57550
75e1b8c05f43 Issue #6985: number of range() items should be constrained to lie
Mark Dickinson <dickinsm@gmail.com>
parents: 57422
diff changeset
1841 n = (Py_ssize_t)bign;
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1842 if (bign < 0 || (long)n != bign) {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1843 PyErr_SetString(PyExc_OverflowError,
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1844 "range() result has too many items");
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1845 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1846 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1847
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1848 v = PyList_New(n);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1849 if (v == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1850 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1851
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1852 curnum = ilow;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1853 Py_INCREF(curnum);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1854
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1855 for (i = 0; i < n; i++) {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1856 PyObject *w = PyNumber_Long(curnum);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1857 PyObject *tmp_num;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1858 if (w == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1859 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1860
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1861 PyList_SET_ITEM(v, i, w);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1862
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1863 tmp_num = PyNumber_Add(curnum, istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1864 if (tmp_num == NULL)
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1865 goto Fail;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1866
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1867 Py_DECREF(curnum);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1868 curnum = tmp_num;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1869 }
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1870 Py_DECREF(ilow);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1871 Py_DECREF(ihigh);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1872 Py_DECREF(istep);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1873 Py_DECREF(zero);
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1874 Py_DECREF(curnum);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1875 return v;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1876
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1877 Fail:
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1878 Py_DECREF(ilow);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1879 Py_DECREF(ihigh);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1880 Py_XDECREF(istep);
28597
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1881 Py_DECREF(zero);
d2d232e2427b handle_range_longs(): refcount handling is very delicate here, and
Tim Peters <tim.peters@gmail.com>
parents: 28583
diff changeset
1882 Py_XDECREF(curnum);
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1883 Py_XDECREF(v);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1884 return NULL;
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1885 }
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1886
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1887 /* 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
1888 * 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
1889 * 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
1890 */
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1891 static long
13679
61f65de19e1e Another missed ansification.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1892 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
1893 {
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1894 /* -------------------------------------------------------------
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1895 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
1896 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
1897 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
1898 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
1899 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
1900 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
1901 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
1902 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
1903 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
1904 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
1905 ---------------------------------------------------------------*/
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1906 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
1907 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
1908 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
1909 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
1910 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
1911 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
1912 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1913 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
1914 }
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1915
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1916 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1917 builtin_range(PyObject *self, PyObject *args)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1918 {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
1919 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
1920 long bign;
57550
75e1b8c05f43 Issue #6985: number of range() items should be constrained to lie
Mark Dickinson <dickinsm@gmail.com>
parents: 57422
diff changeset
1921 Py_ssize_t 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
1922
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1923 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
1924
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1925 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
1926 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
1927 "l;range() requires 1-3 int arguments",
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1928 &ihigh)) {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1929 PyErr_Clear();
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1930 return handle_range_longs(self, args);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1931 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1933 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1934 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
1935 "ll|l;range() requires 1-3 int arguments",
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1936 &ilow, &ihigh, &istep)) {
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1937 PyErr_Clear();
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1938 return handle_range_longs(self, args);
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1939 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1940 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
1941 if (istep == 0) {
28583
a0c002050526 Patch by Chad Netzer (with significant change):
Guido van Rossum <guido@python.org>
parents: 28513
diff changeset
1942 PyErr_SetString(PyExc_ValueError,
28738
52f3d90c17e8 complete and clarify some error messages for range()
Alex Martelli <aleaxit@gmail.com>
parents: 28718
diff changeset
1943 "range() step argument must not be zero");
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1944 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1945 }
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1946 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
1947 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
1948 else
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1949 bign = get_len_of_range(ihigh, ilow, -istep);
57550
75e1b8c05f43 Issue #6985: number of range() items should be constrained to lie
Mark Dickinson <dickinsm@gmail.com>
parents: 57422
diff changeset
1950 n = (Py_ssize_t)bign;
10279
886ea255f381 Patch by Tim Peters to improve the range checks for range() and
Guido van Rossum <guido@python.org>
parents: 10020
diff changeset
1951 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
1952 PyErr_SetString(PyExc_OverflowError,
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14869
diff changeset
1953 "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
1954 return NULL;
5e9ef9d8575b Avoid overflow if possible in calculations for range(); report
Guido van Rossum <guido@python.org>
parents: 9567
diff changeset
1955 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1956 v = PyList_New(n);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1957 if (v == NULL)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1958 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1959 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
1960 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
1961 if (w == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1962 Py_DECREF(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1963 return NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1964 }
8138
9d0310143600 Fred's right -- we need PyList_SET_ITEM().
Guido van Rossum <guido@python.org>
parents: 8132
diff changeset
1965 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
1966 ilow += istep;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1967 }
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1968 return v;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1969 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
1970
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1971 PyDoc_STRVAR(range_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1972 "range([start,] stop[, step]) -> list of integers\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1973 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1974 Return a list containing an arithmetic progression of integers.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1975 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
1976 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
1977 For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
1978 These are exactly the valid indices for a list of 4 elements.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1979
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
1980
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1981 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
1982 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
1983 {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1984 PyObject *v = NULL;
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1985 PyObject *fin = PySys_GetObject("stdin");
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1986 PyObject *fout = PySys_GetObject("stdout");
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1987
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
1988 if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1989 return NULL;
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1990
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1991 if (fin == NULL) {
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
1992 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1993 return NULL;
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1994 }
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1995 if (fout == NULL) {
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
1996 PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1997 return NULL;
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1998 }
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
1999 if (PyFile_SoftSpace(fout, 0)) {
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2000 if (PyFile_WriteString(" ", fout) != 0)
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2001 return NULL;
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2002 }
39385
5009de504f92 Bug #1535165: fixed a segfault in input() and raw_input() when
Georg Brandl <georg@python.org>
parents: 38077
diff changeset
2003 if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 25046
diff changeset
2004 && isatty(fileno(PyFile_AsFile(fin)))
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 25046
diff changeset
2005 && isatty(fileno(PyFile_AsFile(fout)))) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2006 PyObject *po;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2007 char *prompt;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2008 char *s;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2009 PyObject *result;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2010 if (v != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2011 po = PyObject_Str(v);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2012 if (po == NULL)
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2013 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2014 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
2015 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
2016 return NULL;
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2017 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2018 else {
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2019 po = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2020 prompt = "";
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2021 }
32896
a75815e31545 for some reason, the lack of adherence to Python's C whitespace rules
Michael W. Hudson <mwh@python.net>
parents: 32890
diff changeset
2022 s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 25046
diff changeset
2023 prompt);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2024 Py_XDECREF(po);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2025 if (s == NULL) {
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 32507
diff changeset
2026 if (!PyErr_Occurred())
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 32507
diff changeset
2027 PyErr_SetNone(PyExc_KeyboardInterrupt);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2028 return NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2029 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2030 if (*s == '\0') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2031 PyErr_SetNone(PyExc_EOFError);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2032 result = NULL;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2033 }
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2034 else { /* strip trailing '\n' */
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
2035 size_t len = strlen(s);
37673
606818c33e50 Replace INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents: 37411
diff changeset
2036 if (len > PY_SSIZE_T_MAX) {
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2037 PyErr_SetString(PyExc_OverflowError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
2038 "[raw_]input: input too long");
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
2039 result = NULL;
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
2040 }
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
2041 else {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2042 result = PyString_FromStringAndSize(s, len-1);
13078
1d0407960953 Trent Mick:
Guido van Rossum <guido@python.org>
parents: 12961
diff changeset
2043 }
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2044 }
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12572
diff changeset
2045 PyMem_FREE(s);
2808
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2046 return result;
9318ac51691e added locals() and globals(); [raw_]input() uses readline()
Guido van Rossum <guido@python.org>
parents: 2375
diff changeset
2047 }
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
2048 if (v != NULL) {
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2049 if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
284
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
2050 return NULL;
6ac402023cac printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 253
diff changeset
2051 }
24817
3ae8e3ff7e3f Patch #550192: Set softspace to 0 in raw_input().
Martin v. Löwis <martin@v.loewis.de>
parents: 24737
diff changeset
2052 return PyFile_GetLine(fin, -1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2053 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2054
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2055 PyDoc_STRVAR(raw_input_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2056 "raw_input([prompt]) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2057 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2058 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
2059 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
2060 On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2061 is printed without a trailing newline before reading.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2062
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2063
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2064 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2065 builtin_reduce(PyObject *self, PyObject *args)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2066 {
48280
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2067 static PyObject *functools_reduce = NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2068
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
2069 if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
2070 "use functools.reduce()", 1) < 0)
41834
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2071 return NULL;
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2072
48280
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2073 if (functools_reduce == NULL) {
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2074 PyObject *functools = PyImport_ImportModule("functools");
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2075 if (functools == NULL)
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2076 return NULL;
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2077 functools_reduce = PyObject_GetAttrString(functools, "reduce");
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2078 Py_DECREF(functools);
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2079 if (functools_reduce == NULL)
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2080 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2081 }
48280
5508862a1de5 follup to #3473: don't duplicate the reduce code
Benjamin Peterson <benjamin@python.org>
parents: 48115
diff changeset
2082 return PyObject_Call(functools_reduce, args, NULL);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2083 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2084
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2085 PyDoc_STRVAR(reduce_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2086 "reduce(function, sequence[, initial]) -> value\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2087 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2088 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
2089 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
2090 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
2091 ((((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
2092 of the sequence in the calculation, and serves as a default when the\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2093 sequence is empty.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2094
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2095
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2096 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
2097 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
2098 {
46573
aef3501d8e77 Use PyErr_WarnPy3k throughout
Benjamin Peterson <benjamin@python.org>
parents: 46193
diff changeset
2099 if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
46578
7670005da8c5 A little reformating of Py3k warnings
Benjamin Peterson <benjamin@python.org>
parents: 46573
diff changeset
2100 1) < 0)
41834
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2101 return NULL;
9a447514855f Add a bunch more deprecation warnings for builtins that are going away in 3.0
Neal Norwitz <nnorwitz@gmail.com>
parents: 41832
diff changeset
2102
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2103 return PyImport_ReloadModule(v);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2104 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2105
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2106 PyDoc_STRVAR(reload_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2107 "reload(module) -> module\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2108 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2109 Reload the module. The module must have been successfully imported before.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2110
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2111
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2112 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
2113 builtin_repr(PyObject *self, PyObject *v)
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
2114 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2115 return PyObject_Repr(v);
1094
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
2116 }
d2af6bdcb6d0 * config.c: Added audioop to lists.
Guido van Rossum <guido@python.org>
parents: 1079
diff changeset
2117
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2118 PyDoc_STRVAR(repr_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2119 "repr(object) -> string\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2120 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2121 Return the canonical string representation of the object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2122 For most object types, eval(repr(object)) == object.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2123
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2124
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2125 static PyObject *
37344
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 36866
diff changeset
2126 builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
2127 {
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2128 double x;
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2129 PyObject *o_ndigits = NULL;
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2130 Py_ssize_t ndigits;
37344
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 36866
diff changeset
2131 static char *kwlist[] = {"number", "ndigits", 0};
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44367
diff changeset
2132
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2133 if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|O:round",
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2134 kwlist, &x, &o_ndigits))
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44432
diff changeset
2135 return NULL;
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2136
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2137 if (o_ndigits == NULL) {
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2138 /* second argument defaults to 0 */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2139 ndigits = 0;
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2140 }
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2141 else {
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2142 /* interpret 2nd argument as a Py_ssize_t; clip on overflow */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2143 ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2144 if (ndigits == -1 && PyErr_Occurred())
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2145 return NULL;
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2146 }
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2147
57422
b9b683eedee1 round(0, "ermintrude") succeeded instead of producing a TypeError. Fix this.
Mark Dickinson <dickinsm@gmail.com>
parents: 57324
diff changeset
2148 /* nans, infinities and zeros round to themselves */
b9b683eedee1 round(0, "ermintrude") succeeded instead of producing a TypeError. Fix this.
Mark Dickinson <dickinsm@gmail.com>
parents: 57324
diff changeset
2149 if (!Py_IS_FINITE(x) || x == 0.0)
b9b683eedee1 round(0, "ermintrude") succeeded instead of producing a TypeError. Fix this.
Mark Dickinson <dickinsm@gmail.com>
parents: 57324
diff changeset
2150 return PyFloat_FromDouble(x);
b9b683eedee1 round(0, "ermintrude") succeeded instead of producing a TypeError. Fix this.
Mark Dickinson <dickinsm@gmail.com>
parents: 57324
diff changeset
2151
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2152 /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2153 always rounds to itself. For ndigits < NDIGITS_MIN, x always
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2154 rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2155 #define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103))
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2156 #define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103))
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2157 if (ndigits > NDIGITS_MAX)
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2158 /* return x */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2159 return PyFloat_FromDouble(x);
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2160 else if (ndigits < NDIGITS_MIN)
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2161 /* return 0.0, but with sign of x */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2162 return PyFloat_FromDouble(0.0*x);
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44432
diff changeset
2163 else
57324
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2164 /* finite x, and ndigits is not unreasonably large */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2165 /* _Py_double_round is defined in floatobject.c */
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2166 return _Py_double_round(x, (int)ndigits);
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2167 #undef NDIGITS_MAX
92e06cdbeab9 Issue #7117, continued: Change round implementation to use the correctly-rounded
Mark Dickinson <dickinsm@gmail.com>
parents: 57306
diff changeset
2168 #undef NDIGITS_MIN
1215
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
2169 }
9d6cf529bb71 bltinmodule.c: added round(x, [n]); coerce() of two class instances
Guido van Rossum <guido@python.org>
parents: 1096
diff changeset
2170
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2171 PyDoc_STRVAR(round_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2172 "round(number[, ndigits]) -> floating point number\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2173 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2174 Round a number to a given precision in decimal digits (default 0 digits).\n\
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44432
diff changeset
2175 This always returns a floating point number. Precision may be negative.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2176
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2177 static PyObject *
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2178 builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2179 {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2180 PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2181 PyObject *callable;
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36660
diff changeset
2182 static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
36034
fdf0ac36eb74 Fix an int/long mismatch identified here:
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
2183 int reverse;
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2184
36034
fdf0ac36eb74 Fix an int/long mismatch identified here:
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
2185 /* args 1-4 should match listsort in Objects/listobject.c */
35608
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
2186 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
2187 kwlist, &seq, &compare, &keyfunc, &reverse))
a670a5dd1fc6 Removed a check "if (args != NULL)" which is always True and makes no sense.
Armin Rigo <arigo@tunes.org>
parents: 35567
diff changeset
2188 return NULL;
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2189
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2190 newlist = PySequence_List(seq);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2191 if (newlist == NULL)
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2192 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2193
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2194 callable = PyObject_GetAttrString(newlist, "sort");
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2195 if (callable == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2196 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2197 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2198 }
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
2199
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2200 newargs = PyTuple_GetSlice(args, 1, 4);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2201 if (newargs == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2202 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2203 Py_DECREF(callable);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2204 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2205 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2206
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2207 v = PyObject_Call(callable, newargs, kwds);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2208 Py_DECREF(newargs);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2209 Py_DECREF(callable);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2210 if (v == NULL) {
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2211 Py_DECREF(newlist);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2212 return NULL;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2213 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2214 Py_DECREF(v);
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2215 return newlist;
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2216 }
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2217
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2218 PyDoc_STRVAR(sorted_doc,
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2219 "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2220
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2221 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2222 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
2223 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2224 PyObject *v = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2225 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
2226
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
2227 if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2002
diff changeset
2228 return NULL;
1840
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2229 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2230 d = PyEval_GetLocals();
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
2231 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2232 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2233 PyErr_SetString(PyExc_SystemError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
2234 "vars(): no locals!?");
2857
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
2235 }
eebbdbee54f9 be more suspicious of getlocals()
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
2236 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2237 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
2238 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2239 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2240 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
2241 if (d == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2242 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
2243 "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
2244 return NULL;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2245 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2246 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2247 return d;
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2248 }
bfd11620a2d8 * Python/bltinmodule.c (builtin_vars): correct typo in error msg
Guido van Rossum <guido@python.org>
parents: 1444
diff changeset
2249
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2250 PyDoc_STRVAR(vars_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2251 "vars([object]) -> dictionary\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2252 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2253 Without arguments, equivalent to locals().\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2254 With an argument, equivalent to object.__dict__.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2255
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2256
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2257 static PyObject*
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2258 builtin_sum(PyObject *self, PyObject *args)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2259 {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2260 PyObject *seq;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2261 PyObject *result = NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2262 PyObject *temp, *item, *iter;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2263
30774
2d98710f272e Use PyArg_UnpackTuple() where possible.
Raymond Hettinger <python@rcn.com>
parents: 30683
diff changeset
2264 if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2265 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2266
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2267 iter = PyObject_GetIter(seq);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2268 if (iter == NULL)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2269 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2270
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2271 if (result == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2272 result = PyInt_FromLong(0);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2273 if (result == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2274 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2275 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2276 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2277 } else {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2278 /* reject string values for 'start' parameter */
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2279 if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2280 PyErr_SetString(PyExc_TypeError,
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
2281 "sum() can't sum strings [use ''.join(seq) instead]");
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2282 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2283 return NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2284 }
28718
16ef5b9bc789 fixed a potential refcount bug (thanks Raymond!).
Alex Martelli <aleaxit@gmail.com>
parents: 28717
diff changeset
2285 Py_INCREF(result);
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2286 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2287
43657
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2288 #ifndef SLOW_SUM
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2289 /* Fast addition by keeping temporary sums in C instead of new Python objects.
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2290 Assumes all inputs are the same type. If the assumption fails, default
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2291 to the more general routine.
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2292 */
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2293 if (PyInt_CheckExact(result)) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2294 long i_result = PyInt_AS_LONG(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2295 Py_DECREF(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2296 result = NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2297 while(result == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2298 item = PyIter_Next(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2299 if (item == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2300 Py_DECREF(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2301 if (PyErr_Occurred())
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2302 return NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2303 return PyInt_FromLong(i_result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2304 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2305 if (PyInt_CheckExact(item)) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2306 long b = PyInt_AS_LONG(item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2307 long x = i_result + b;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2308 if ((x^i_result) >= 0 || (x^b) >= 0) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2309 i_result = x;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2310 Py_DECREF(item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2311 continue;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2312 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2313 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2314 /* Either overflowed or is not an int. Restore real objects and process normally */
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2315 result = PyInt_FromLong(i_result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2316 temp = PyNumber_Add(result, item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2317 Py_DECREF(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2318 Py_DECREF(item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2319 result = temp;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2320 if (result == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2321 Py_DECREF(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2322 return NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2323 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2324 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2325 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2326
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2327 if (PyFloat_CheckExact(result)) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2328 double f_result = PyFloat_AS_DOUBLE(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2329 Py_DECREF(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2330 result = NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2331 while(result == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2332 item = PyIter_Next(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2333 if (item == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2334 Py_DECREF(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2335 if (PyErr_Occurred())
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2336 return NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2337 return PyFloat_FromDouble(f_result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2338 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2339 if (PyFloat_CheckExact(item)) {
47385
1e1df892095a Issue 2784: fix leaks in exception exit.
Raymond Hettinger <python@rcn.com>
parents: 47294
diff changeset
2340 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
43657
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2341 f_result += PyFloat_AS_DOUBLE(item);
43658
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2342 PyFPE_END_PROTECT(f_result)
43675
35da915adedc Missing DECREFs
Raymond Hettinger <python@rcn.com>
parents: 43658
diff changeset
2343 Py_DECREF(item);
43658
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2344 continue;
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2345 }
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2346 if (PyInt_CheckExact(item)) {
47385
1e1df892095a Issue 2784: fix leaks in exception exit.
Raymond Hettinger <python@rcn.com>
parents: 47294
diff changeset
2347 PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
43658
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2348 f_result += (double)PyInt_AS_LONG(item);
7408e1ae98c4 Fixup error return and add support for intermixed ints and floats/
Raymond Hettinger <python@rcn.com>
parents: 43657
diff changeset
2349 PyFPE_END_PROTECT(f_result)
43675
35da915adedc Missing DECREFs
Raymond Hettinger <python@rcn.com>
parents: 43658
diff changeset
2350 Py_DECREF(item);
43657
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2351 continue;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2352 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2353 result = PyFloat_FromDouble(f_result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2354 temp = PyNumber_Add(result, item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2355 Py_DECREF(result);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2356 Py_DECREF(item);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2357 result = temp;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2358 if (result == NULL) {
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2359 Py_DECREF(iter);
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2360 return NULL;
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2361 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2362 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2363 }
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2364 #endif
08ee256a9bd1 Optimize sum() for integer and float inputs.
Raymond Hettinger <python@rcn.com>
parents: 41834
diff changeset
2365
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2366 for(;;) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2367 item = PyIter_Next(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2368 if (item == NULL) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2369 /* error, or end-of-sequence */
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2370 if (PyErr_Occurred()) {
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2371 Py_DECREF(result);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2372 result = NULL;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2373 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2374 break;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2375 }
56750
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2376 /* It's tempting to use PyNumber_InPlaceAdd instead of
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2377 PyNumber_Add here, to avoid quadratic running time
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2378 when doing 'sum(list_of_lists, [])'. However, this
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2379 would produce a change in behaviour: a snippet like
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2380
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2381 empty = []
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2382 sum([[x] for x in range(10)], empty)
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2383
6e4d8968e246 Warn against replacing PyNumber_Add with PyNumber_InPlaceAdd in sum
Mark Dickinson <dickinsm@gmail.com>
parents: 55871
diff changeset
2384 would change the value of empty. */
30784
37285a0346c1 regressing the performance bugfix -- Guido wants the performance bug left
Alex Martelli <aleaxit@gmail.com>
parents: 30776
diff changeset
2385 temp = PyNumber_Add(result, item);
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2386 Py_DECREF(result);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2387 Py_DECREF(item);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2388 result = temp;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2389 if (result == NULL)
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2390 break;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2391 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2392 Py_DECREF(iter);
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2393 return result;
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2394 }
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2395
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2396 PyDoc_STRVAR(sum_doc,
40113
635438b29673 Bug #1283491: follow docstring convention wrt. keyword-able args in sum().
Georg Brandl <georg@python.org>
parents: 39746
diff changeset
2397 "sum(sequence[, start]) -> value\n\
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2398 \n\
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2399 Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
40113
635438b29673 Bug #1283491: follow docstring convention wrt. keyword-able args in sum().
Georg Brandl <georg@python.org>
parents: 39746
diff changeset
2400 of parameter 'start' (which defaults to 0). When the sequence is\n\
635438b29673 Bug #1283491: follow docstring convention wrt. keyword-able args in sum().
Georg Brandl <georg@python.org>
parents: 39746
diff changeset
2401 empty, returns start.");
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2402
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2403
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2404 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2405 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
2406 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2407 PyObject *inst;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2408 PyObject *cls;
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
2409 int retval;
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2410
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
2411 if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2412 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
2413
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
2414 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
2415 if (retval < 0)
15829
9308dc8c85e0 (Modified) patch by Ping - SF Patch #102681.
Guido van Rossum <guido@python.org>
parents: 15727
diff changeset
2416 return NULL;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2417 return PyBool_FromLong(retval);
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2418 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2419
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2420 PyDoc_STRVAR(isinstance_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2421 "isinstance(object, class-or-type-or-tuple) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2422 \n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2423 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
2424 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
2425 The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2426 isinstance(x, A) or isinstance(x, B) or ... (etc.).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2427
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2428
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2429 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2430 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
2431 {
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2432 PyObject *derived;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2433 PyObject *cls;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2434 int retval;
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2435
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 26612
diff changeset
2436 if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2437 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
2438
17111
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
2439 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
2440 if (retval < 0)
bf678a11c21a Move the code implementing isinstance() and issubclass() to new C
Guido van Rossum <guido@python.org>
parents: 16048
diff changeset
2441 return NULL;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2442 return PyBool_FromLong(retval);
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2443 }
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2444
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2445 PyDoc_STRVAR(issubclass_doc,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2446 "issubclass(C, B) -> bool\n\
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2447 \n\
26612
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
2448 Return whether class C is a subclass (i.e., a derived class) of class B.\n\
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
2449 When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
97f83542f8ee Enhance issubclass() and PyObject_IsSubclass() so that a tuple is
Walter Dörwald <walter@livinglogic.de>
parents: 25924
diff changeset
2450 is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2451
6145
341fcb7bad90 Two new built-in functions: issubclass() and isinstance(). Both take
Barry Warsaw <barry@python.org>
parents: 5926
diff changeset
2452
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2453 static PyObject*
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2454 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
2455 {
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2456 PyObject *ret;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2457 const Py_ssize_t itemsize = PySequence_Length(args);
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2458 Py_ssize_t i;
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2459 PyObject *itlist; /* tuple of iterators */
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2460 Py_ssize_t len; /* guess at result length */
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2461
30037
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 28740
diff changeset
2462 if (itemsize == 0)
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 28740
diff changeset
2463 return PyList_New(0);
0504ba8efbb6 As discussed on python-dev, changed builtin.zip() to handle zero arguments
Raymond Hettinger <python@rcn.com>
parents: 28740
diff changeset
2464
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2465 /* 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
2466 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
2467
23421
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2468 /* Guess at result length: the shortest of the input lengths.
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2469 If some argument refuses to say, we refuse to guess too, lest
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2470 an argument like xrange(sys.maxint) lead us astray.*/
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2471 len = -1; /* unknown */
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2472 for (i = 0; i < itemsize; ++i) {
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2473 PyObject *item = PyTuple_GET_ITEM(args, i);
53956
72a50394c2dd don't ignore exceptions from _PyObject_LengthHint
Benjamin Peterson <benjamin@python.org>
parents: 53232
diff changeset
2474 Py_ssize_t thislen = _PyObject_LengthHint(item, -2);
23421
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2475 if (thislen < 0) {
53956
72a50394c2dd don't ignore exceptions from _PyObject_LengthHint
Benjamin Peterson <benjamin@python.org>
parents: 53232
diff changeset
2476 if (thislen == -1)
72a50394c2dd don't ignore exceptions from _PyObject_LengthHint
Benjamin Peterson <benjamin@python.org>
parents: 53232
diff changeset
2477 return NULL;
23421
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2478 len = -1;
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2479 break;
642a453e7a2e SF bug 555042: zip() may trigger MemoryError.
Tim Peters <tim.peters@gmail.com>
parents: 23305
diff changeset
2480 }
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2481 else if (len < 0 || thislen < len)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2482 len = thislen;
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2483 }
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2484
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2485 /* allocate result list */
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2486 if (len < 0)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2487 len = 10; /* arbitrary */
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2488 if ((ret = PyList_New(len)) == NULL)
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2489 return NULL;
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2490
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2491 /* obtain iterators */
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2492 itlist = PyTuple_New(itemsize);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2493 if (itlist == NULL)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2494 goto Fail_ret;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2495 for (i = 0; i < itemsize; ++i) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2496 PyObject *item = PyTuple_GET_ITEM(args, i);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2497 PyObject *it = PyObject_GetIter(item);
55871
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2498 if (it == NULL) {
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2499 if (PyErr_ExceptionMatches(PyExc_TypeError))
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2500 PyErr_Format(PyExc_TypeError,
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2501 "zip argument #%zd must support iteration",
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2502 i+1);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2503 goto Fail_ret_itlist;
55871
37f5d1785bb3 revert r74699 since it loses useful error information
Benjamin Peterson <benjamin@python.org>
parents: 55869
diff changeset
2504 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2505 PyTuple_SET_ITEM(itlist, i, it);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2506 }
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2507
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2508 /* build result into ret list */
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2509 for (i = 0; ; ++i) {
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2510 int j;
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2511 PyObject *next = PyTuple_New(itemsize);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2512 if (!next)
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2513 goto Fail_ret_itlist;
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2514
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2515 for (j = 0; j < itemsize; j++) {
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2516 PyObject *it = PyTuple_GET_ITEM(itlist, j);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2517 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
2518 if (!item) {
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2519 if (PyErr_Occurred()) {
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2520 Py_DECREF(ret);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2521 ret = NULL;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2522 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2523 Py_DECREF(next);
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2524 Py_DECREF(itlist);
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2525 goto Done;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2526 }
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2527 PyTuple_SET_ITEM(next, j, item);
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2528 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2529
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2530 if (i < len)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2531 PyList_SET_ITEM(ret, i, next);
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2532 else {
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2533 int status = PyList_Append(ret, next);
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2534 Py_DECREF(next);
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2535 ++len;
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2536 if (status < 0)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2537 goto Fail_ret_itlist;
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2538 }
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2539 }
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2540
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2541 Done:
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2542 if (ret != NULL && i < len) {
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2543 /* The list is too big. */
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2544 if (PyList_SetSlice(ret, i, len, NULL) < 0)
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2545 return NULL;
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2546 }
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2547 return ret;
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2548
17694
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2549 Fail_ret_itlist:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2550 Py_DECREF(itlist);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2551 Fail_ret:
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2552 Py_DECREF(ret);
aab6c1f19289 Generalize zip() to work with iterators.
Tim Peters <tim.peters@gmail.com>
parents: 17683
diff changeset
2553 return NULL;
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2554 }
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2555
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2556
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2557 PyDoc_STRVAR(zip_doc,
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2558 "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
2559 \n\
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2560 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
2561 from each of the argument sequences. The returned list is truncated\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2562 in length to the length of the shortest argument sequence.");
13822
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2563
c57b574e6766 Both PEP 201 Lockstep Iteration and SF patch #101030 have been
Barry Warsaw <barry@python.org>
parents: 13761
diff changeset
2564
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2565 static PyMethodDef builtin_methods[] = {
37411
ea9a03b8b9f6 Accept keyword arguments for __import__ and doc the addition of the level param from PEP 328.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37344
diff changeset
2566 {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2567 {"abs", builtin_abs, METH_O, abs_doc},
34736
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
2568 {"all", builtin_all, METH_O, all_doc},
81b5a13fa38a Add two new functions, any() and all().
Raymond Hettinger <python@rcn.com>
parents: 34123
diff changeset
2569 {"any", builtin_any, METH_O, any_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2570 {"apply", builtin_apply, METH_VARARGS, apply_doc},
45433
4a9522fd75a6 Added bin() builtin. I'm going to check in the tests in a seperate checkin, because the builtin doesn't need to be ported to py3k, but the tests are missing in py3k and need to be merged there.
Eric Smith <eric@trueblade.com>
parents: 45381
diff changeset
2571 {"bin", builtin_bin, METH_O, bin_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2572 {"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
2573 {"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
2574 {"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
2575 {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
41168
78686c7a23a7 Patch #1444529: the builtin compile() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 41140
diff changeset
2576 {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2577 {"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
2578 {"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
2579 {"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
2580 {"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
2581 {"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
2582 {"filter", builtin_filter, METH_VARARGS, filter_doc},
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 45079
diff changeset
2583 {"format", builtin_format, METH_VARARGS, format_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2584 {"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
2585 {"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
2586 {"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
2587 {"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
2588 {"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
2589 {"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
2590 {"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
2591 {"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
2592 {"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
2593 {"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
2594 {"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
2595 {"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
2596 {"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
2597 {"map", builtin_map, METH_VARARGS, map_doc},
34085
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
2598 {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
cc9ce6837ea9 SF patch #1077353: add key= argument to min and max
Raymond Hettinger <python@rcn.com>
parents: 33261
diff changeset
2599 {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
46607
7a6d7474ce6e #2719: backport next() from 3k.
Georg Brandl <georg@python.org>
parents: 46578
diff changeset
2600 {"next", builtin_next, METH_VARARGS, next_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2601 {"oct", builtin_oct, METH_O, oct_doc},
38077
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
2602 {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2603 {"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
2604 {"pow", builtin_pow, METH_VARARGS, pow_doc},
45891
727f743cf630 Backport of the print function, using a __future__ import.
Eric Smith <eric@trueblade.com>
parents: 45880
diff changeset
2605 {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2606 {"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
2607 {"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
2608 {"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
2609 {"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
2610 {"repr", builtin_repr, METH_O, repr_doc},
37344
c6cf11acb41f Patch #1460496: round() now accepts keyword arguments.
Georg Brandl <georg@python.org>
parents: 36866
diff changeset
2611 {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
19301
b9701538f827 Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents: 19061
diff changeset
2612 {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
31210
c06b570adf12 Guido grants a Christmas wish:
Raymond Hettinger <python@rcn.com>
parents: 31122
diff changeset
2613 {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
28717
13d0a3c4e465 Adding new built-in function sum, with docs and tests.
Alex Martelli <aleaxit@gmail.com>
parents: 28612
diff changeset
2614 {"sum", builtin_sum, METH_VARARGS, sum_doc},
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2615 #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
2616 {"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
2617 #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
2618 {"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
2619 {"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
2620 {NULL, NULL},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2621 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2622
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2623 PyDoc_STRVAR(builtin_doc,
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2624 "Built-in functions, exceptions, and other objects.\n\
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2625 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23695
diff changeset
2626 Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2627
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2628 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2629 _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
2630 {
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2631 PyObject *mod, *dict, *debug;
8561
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2632 mod = Py_InitModule4("__builtin__", builtin_methods,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2633 builtin_doc, (PyObject *)NULL,
8973e7085cf4 Added doc strings.
Guido van Rossum <guido@python.org>
parents: 8560
diff changeset
2634 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
2635 if (mod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2636 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2637 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
2638
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2639 #ifdef Py_TRACE_REFS
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2640 /* __builtin__ exposes a number of statically allocated objects
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2641 * that, before this code was added in 2.3, never showed up in
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2642 * the list of "all objects" maintained by Py_TRACE_REFS. As a
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2643 * result, programs leaking references to None and False (etc)
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2644 * couldn't be diagnosed by examining sys.getobjects(0).
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2645 */
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2646 #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0)
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2647 #else
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2648 #define ADD_TO_ALL(OBJECT) (void)0
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2649 #endif
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2650
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2651 #define SETBUILTIN(NAME, OBJECT) \
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2652 if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2653 return NULL; \
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2654 ADD_TO_ALL(OBJECT)
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2655
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2656 SETBUILTIN("None", Py_None);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2657 SETBUILTIN("Ellipsis", Py_Ellipsis);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2658 SETBUILTIN("NotImplemented", Py_NotImplemented);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2659 SETBUILTIN("False", Py_False);
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2660 SETBUILTIN("True", Py_True);
23597
ed007e47b3a1 Change name from string to basestring
Neal Norwitz <nnorwitz@gmail.com>
parents: 23542
diff changeset
2661 SETBUILTIN("basestring", &PyBaseString_Type);
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2662 SETBUILTIN("bool", &PyBool_Type);
52749
75efd757434b Issue #2396: backport the memoryview object.
Antoine Pitrou <solipsis@pitrou.net>
parents: 51282
diff changeset
2663 SETBUILTIN("memoryview", &PyMemoryView_Type);
47291
04bd3690fb1f First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents: 47139
diff changeset
2664 SETBUILTIN("bytearray", &PyByteArray_Type);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2665 SETBUILTIN("bytes", &PyString_Type);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2666 SETBUILTIN("buffer", &PyBuffer_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2667 SETBUILTIN("classmethod", &PyClassMethod_Type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
2668 #ifndef WITHOUT_COMPLEX
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2669 SETBUILTIN("complex", &PyComplex_Type);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18890
diff changeset
2670 #endif
20754
272336648d1f Rename "dictionary" (type and constructor) to "dict".
Tim Peters <tim.peters@gmail.com>
parents: 20654
diff changeset
2671 SETBUILTIN("dict", &PyDict_Type);
23305
ef6e31f0cee1 builtin_zip(): Take a good guess at how big the result list will be,
Tim Peters <tim.peters@gmail.com>
parents: 23295
diff changeset
2672 SETBUILTIN("enumerate", &PyEnum_Type);
38077
c9202f1c56c2 SF #1479181: split open() and file() from being aliases for each other.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37673
diff changeset
2673 SETBUILTIN("file", &PyFile_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2674 SETBUILTIN("float", &PyFloat_Type);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
2675 SETBUILTIN("frozenset", &PyFrozenSet_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2676 SETBUILTIN("property", &PyProperty_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2677 SETBUILTIN("int", &PyInt_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2678 SETBUILTIN("list", &PyList_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2679 SETBUILTIN("long", &PyLong_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2680 SETBUILTIN("object", &PyBaseObject_Type);
30865
41c4e143e6e6 Implement and apply PEP 322, reverse iteration
Raymond Hettinger <python@rcn.com>
parents: 30784
diff changeset
2681 SETBUILTIN("reversed", &PyReversed_Type);
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30865
diff changeset
2682 SETBUILTIN("set", &PySet_Type);
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23832
diff changeset
2683 SETBUILTIN("slice", &PySlice_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2684 SETBUILTIN("staticmethod", &PyStaticMethod_Type);
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2685 SETBUILTIN("str", &PyString_Type);
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2686 SETBUILTIN("super", &PySuper_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2687 SETBUILTIN("tuple", &PyTuple_Type);
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2688 SETBUILTIN("type", &PyType_Type);
23695
6266f1bd1ab9 Skip Montanaro's patch, SF 559833, exposing xrange type in builtins.
Raymond Hettinger <python@rcn.com>
parents: 23597
diff changeset
2689 SETBUILTIN("xrange", &PyRange_Type);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2690 #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
2691 SETBUILTIN("unicode", &PyUnicode_Type);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19301
diff changeset
2692 #endif
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22205
diff changeset
2693 debug = PyBool_FromLong(Py_OptimizeFlag == 0);
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2694 if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2695 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
2696 return NULL;
12961
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2697 }
eb911eca9ee9 Christopher Fandrich <cfandrich@8cs.com>:
Fred Drake <fdrake@acm.org>
parents: 12756
diff changeset
2698 Py_XDECREF(debug);
6198
c43ca8dd0081 Removed obsolete exception PyExc_AccessError.
Barry Warsaw <barry@python.org>
parents: 6145
diff changeset
2699
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5610
diff changeset
2700 return mod;
28396
d56282c28a1e Improved new Py_TRACE_REFS gimmicks.
Tim Peters <tim.peters@gmail.com>
parents: 28027
diff changeset
2701 #undef ADD_TO_ALL
19902
de39956b5458 _PyBuiltin_Init(): For clarity, macroize this purely repetitive code.
Tim Peters <tim.peters@gmail.com>
parents: 19894
diff changeset
2702 #undef SETBUILTIN
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2703 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents:
diff changeset
2704
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
2705 /* 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
2706
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2707 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2708 filtertuple(PyObject *func, PyObject *tuple)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2709 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2710 PyObject *result;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2711 Py_ssize_t i, j;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2712 Py_ssize_t len = PyTuple_Size(tuple);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2713
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2714 if (len == 0) {
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2715 if (PyTuple_CheckExact(tuple))
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2716 Py_INCREF(tuple);
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2717 else
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2718 tuple = PyTuple_New(0);
2890
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2719 return tuple;
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2720 }
04e1690fb4d1 avoid resize of 0-length tuple
Guido van Rossum <guido@python.org>
parents: 2857
diff changeset
2721
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2722 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
2723 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2724
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2725 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
2726 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
2727 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2728
27795
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27790
diff changeset
2729 if (tuple->ob_type->tp_as_sequence &&
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27790
diff changeset
2730 tuple->ob_type->tp_as_sequence->sq_item) {
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27790
diff changeset
2731 item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2732 if (item == NULL)
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2733 goto Fail_1;
27795
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27790
diff changeset
2734 } else {
28740
d3297e298211 some more error-message enhancements
Alex Martelli <aleaxit@gmail.com>
parents: 28738
diff changeset
2735 PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2736 goto Fail_1;
27795
2042e2051d97 Change filtertuple() to use tp_as_sequence->sq_item
Walter Dörwald <walter@livinglogic.de>
parents: 27790
diff changeset
2737 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2738 if (func == Py_None) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2739 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
2740 good = item;
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2741 }
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2742 else {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30430
diff changeset
2743 PyObject *arg = PyTuple_Pack(1, item);
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2744 if (arg == NULL) {
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2745 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
2746 goto Fail_1;
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2747 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2748 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
2749 Py_DECREF(arg);
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2750 if (good == NULL) {
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2751 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2752 goto Fail_1;
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2753 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2754 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2755 ok = PyObject_IsTrue(good);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2756 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
2757 if (ok) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2758 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
2759 goto Fail_1;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2760 }
30182
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2761 else
7bc772527e44 Fix a crash: when sq_item failed the code continued blindly and used the
Walter Dörwald <walter@livinglogic.de>
parents: 30151
diff changeset
2762 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2763 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2764
17976
d579a5d8b3fe Cruft cleanup: Removed the unused last_is_sticky argument from the internal
Tim Peters <tim.peters@gmail.com>
parents: 17874
diff changeset
2765 if (_PyTuple_Resize(&result, j) < 0)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2766 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2767
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2768 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2769
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2770 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2771 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2772 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2773 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2774
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2775
1393
7d6a2d950f13 * nismodule.c: database keys and values can contain null bytes. be more
Guido van Rossum <guido@python.org>
parents: 1387
diff changeset
2776 /* 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
2777
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2778 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13576
diff changeset
2779 filterstring(PyObject *func, PyObject *strobj)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2780 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2781 PyObject *result;
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2782 Py_ssize_t i, j;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2783 Py_ssize_t len = PyString_Size(strobj);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36484
diff changeset
2784 Py_ssize_t outlen = len;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2785
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2786 if (func == Py_None) {
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2787 /* If it's a real string we can return the original,
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2788 * as no character is ever false and __getitem__
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2789 * does return this character. If it's a subclass
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2790 * we must go through the __getitem__ loop */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2791 if (PyString_CheckExact(strobj)) {
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2792 Py_INCREF(strobj);
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2793 return strobj;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2794 }
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2795 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2796 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
2797 return NULL;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2798
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2799 for (i = j = 0; i < len; ++i) {
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2800 PyObject *item;
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2801 int ok;
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2802
1375
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2803 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
2804 if (item == NULL)
f3a325662d39 * listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
2805 goto Fail_1;
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2806 if (func==Py_None) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2807 ok = 1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2808 } else {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2809 PyObject *arg, *good;
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30430
diff changeset
2810 arg = PyTuple_Pack(1, item);
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2811 if (arg == NULL) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2812 Py_DECREF(item);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2813 goto Fail_1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2814 }
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2815 good = PyEval_CallObject(func, arg);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2816 Py_DECREF(arg);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2817 if (good == NULL) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2818 Py_DECREF(item);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2819 goto Fail_1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2820 }
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2821 ok = PyObject_IsTrue(good);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2822 Py_DECREF(good);
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2823 }
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2824 if (ok) {
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2825 Py_ssize_t reslen;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2826 if (!PyString_Check(item)) {
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2827 PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2828 " __getitem__ returned different type");
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2829 Py_DECREF(item);
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2830 goto Fail_1;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2831 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2832 reslen = PyString_GET_SIZE(item);
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2833 if (reslen == 1) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2834 PyString_AS_STRING(result)[j++] =
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2835 PyString_AS_STRING(item)[0];
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2836 } else {
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2837 /* do we need more space? */
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2838 Py_ssize_t need = j;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2839
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2840 /* calculate space requirements while checking for overflow */
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2841 if (need > PY_SSIZE_T_MAX - reslen) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2842 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2843 goto Fail_1;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2844 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2845
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2846 need += reslen;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2847
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2848 if (need > PY_SSIZE_T_MAX - len) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2849 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2850 goto Fail_1;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2851 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2852
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2853 need += len;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2854
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2855 if (need <= i) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2856 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2857 goto Fail_1;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2858 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2859
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2860 need = need - i - 1;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2861
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2862 assert(need >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2863 assert(outlen >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2864
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2865 if (need > outlen) {
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2866 /* overallocate, to avoid reallocations */
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2867 if (outlen > PY_SSIZE_T_MAX / 2) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2868 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2869 return NULL;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2870 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2871
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2872 if (need<2*outlen) {
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2873 need = 2*outlen;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2874 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2875 if (_PyString_Resize(&result, need)) {
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2876 Py_DECREF(item);
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2877 return NULL;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2878 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2879 outlen = need;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2880 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2881 memcpy(
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2882 PyString_AS_STRING(result) + j,
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2883 PyString_AS_STRING(item),
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2884 reslen
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2885 );
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2886 j += reslen;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2887 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2888 }
17337
96ea0eccdb31 SF patch #413552 - Premature decref on object
Tim Peters <tim.peters@gmail.com>
parents: 17125
diff changeset
2889 Py_DECREF(item);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2890 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2891
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2892 if (j < outlen)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47385
diff changeset
2893 _PyString_Resize(&result, j);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2894
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2895 return result;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2896
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2897 Fail_1:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2898 Py_DECREF(result);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2899 return NULL;
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1360
diff changeset
2900 }
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2901
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2902 #ifdef Py_USING_UNICODE
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2903 /* Helper for filter(): filter a Unicode object through a function */
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2904
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2905 static PyObject *
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2906 filterunicode(PyObject *func, PyObject *strobj)
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2907 {
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2908 PyObject *result;
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
2909 register Py_ssize_t i, j;
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2910 Py_ssize_t len = PyUnicode_GetSize(strobj);
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2911 Py_ssize_t outlen = len;
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2912
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2913 if (func == Py_None) {
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2914 /* If it's a real string we can return the original,
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2915 * as no character is ever false and __getitem__
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2916 * does return this character. If it's a subclass
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2917 * we must go through the __getitem__ loop */
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2918 if (PyUnicode_CheckExact(strobj)) {
27681
e5454fe1aafd Make sure filter() never returns tuple, str or unicode
Walter Dörwald <walter@livinglogic.de>
parents: 27675
diff changeset
2919 Py_INCREF(strobj);
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2920 return strobj;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2921 }
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2922 }
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2923 if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2924 return NULL;
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2925
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2926 for (i = j = 0; i < len; ++i) {
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2927 PyObject *item, *arg, *good;
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2928 int ok;
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2929
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2930 item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2931 if (item == NULL)
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2932 goto Fail_1;
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2933 if (func == Py_None) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2934 ok = 1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2935 } else {
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30430
diff changeset
2936 arg = PyTuple_Pack(1, item);
27786
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2937 if (arg == NULL) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2938 Py_DECREF(item);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2939 goto Fail_1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2940 }
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2941 good = PyEval_CallObject(func, arg);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2942 Py_DECREF(arg);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2943 if (good == NULL) {
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2944 Py_DECREF(item);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2945 goto Fail_1;
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2946 }
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2947 ok = PyObject_IsTrue(good);
f34446fa216e Change filterstring() and filterunicode(): If the
Walter Dörwald <walter@livinglogic.de>
parents: 27782
diff changeset
2948 Py_DECREF(good);
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
2949 }
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2950 if (ok) {
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2951 Py_ssize_t reslen;
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2952 if (!PyUnicode_Check(item)) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
2953 PyErr_SetString(PyExc_TypeError,
30429
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2954 "can't filter unicode to unicode:"
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2955 " __getitem__ returned different type");
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2956 Py_DECREF(item);
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2957 goto Fail_1;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2958 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2959 reslen = PyUnicode_GET_SIZE(item);
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
2960 if (reslen == 1)
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2961 PyUnicode_AS_UNICODE(result)[j++] =
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2962 PyUnicode_AS_UNICODE(item)[0];
30429
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2963 else {
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2964 /* do we need more space? */
36521
30aef3ce2b26 Use Py_ssize_t to count the
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
2965 Py_ssize_t need = j + reslen + len - i - 1;
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2966
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2967 /* check that didnt overflow */
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2968 if ((j > PY_SSIZE_T_MAX - reslen) ||
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2969 ((j + reslen) > PY_SSIZE_T_MAX - len) ||
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2970 ((j + reslen + len) < i) ||
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2971 ((j + reslen + len - i) <= 0)) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2972 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2973 return NULL;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2974 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2975
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2976 assert(need >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2977 assert(outlen >= 0);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2978
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2979 if (need > outlen) {
35500
8fa83a9ebc6e Whitespace normalization.
Georg Brandl <georg@python.org>
parents: 35364
diff changeset
2980 /* overallocate,
30429
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2981 to avoid reallocations */
47587
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2982 if (need < 2 * outlen) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2983 if (outlen > PY_SSIZE_T_MAX / 2) {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2984 Py_DECREF(item);
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2985 return NULL;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2986 } else {
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2987 need = 2 * outlen;
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2988 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2989 }
1c964ed35ef9 Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47527
diff changeset
2990
30430
a8f04cbf659c Correct check of PyUnicode_Resize() return value.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30429
diff changeset
2991 if (PyUnicode_Resize(
a8f04cbf659c Correct check of PyUnicode_Resize() return value.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30429
diff changeset
2992 &result, need) < 0) {
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2993 Py_DECREF(item);
27675
8f8448fb58d6 PyUnicode_Resize() doesn't free its argument in case of a failure,
Walter Dörwald <walter@livinglogic.de>
parents: 27674
diff changeset
2994 goto Fail_1;
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2995 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2996 outlen = need;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
2997 }
30429
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2998 memcpy(PyUnicode_AS_UNICODE(result) + j,
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
2999 PyUnicode_AS_UNICODE(item),
c9cc35249322 Reflow long lines and reformat.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30182
diff changeset
3000 reslen*sizeof(Py_UNICODE));
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
3001 j += reslen;
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
3002 }
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
3003 }
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3004 Py_DECREF(item);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3005 }
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3006
27674
ce3faab06de7 filterstring() and filterunicode() in Python/bltinmodule.c
Walter Dörwald <walter@livinglogic.de>
parents: 27652
diff changeset
3007 if (j < outlen)
27385
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3008 PyUnicode_Resize(&result, j);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3009
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3010 return result;
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3011
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3012 Fail_1:
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3013 Py_DECREF(result);
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3014 return NULL;
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3015 }
91a573243d5d Patch #636005: Filter unicode into unicode.
Martin v. Löwis <martin@v.loewis.de>
parents: 27076
diff changeset
3016 #endif