Andrew M. Kuchling | 9bcc68c | 2000-12-20 15:07:34 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2 | /* Built-in functions */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | |
| 6 | #include "node.h" |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 7 | #include "compile.h" |
| 8 | #include "eval.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 10 | #include <ctype.h> |
| 11 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 12 | #ifdef RISCOS |
| 13 | #include "unixstuff.h" |
| 14 | #endif |
| 15 | |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 16 | /* The default encoding used by the platform file system APIs |
| 17 | Can remain NULL for all platforms that don't have such a concept |
| 18 | */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 19 | #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 20 | const char *Py_FileSystemDefaultEncoding = "mbcs"; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 21 | #elif defined(__APPLE__) |
| 22 | const char *Py_FileSystemDefaultEncoding = "utf-8"; |
Mark Hammond | 26cffde4 | 2001-05-14 12:17:34 +0000 | [diff] [blame] | 23 | #else |
| 24 | const char *Py_FileSystemDefaultEncoding = NULL; /* use default */ |
| 25 | #endif |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 27 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 28 | static PyObject *filterstring(PyObject *, PyObject *); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 29 | #ifdef Py_USING_UNICODE |
| 30 | static PyObject *filterunicode(PyObject *, PyObject *); |
| 31 | #endif |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 32 | static PyObject *filtertuple (PyObject *, PyObject *); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 35 | builtin___import__(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 37 | char *name; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 38 | PyObject *globals = NULL; |
| 39 | PyObject *locals = NULL; |
| 40 | PyObject *fromlist = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 42 | if (!PyArg_ParseTuple(args, "s|OOO:__import__", |
Guido van Rossum | 24c1374 | 1995-02-14 09:42:43 +0000 | [diff] [blame] | 43 | &name, &globals, &locals, &fromlist)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | return NULL; |
Guido van Rossum | aee0bad | 1997-09-05 07:33:22 +0000 | [diff] [blame] | 45 | return PyImport_ImportModuleEx(name, globals, locals, fromlist); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 48 | PyDoc_STRVAR(import_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 49 | "__import__(name, globals, locals, fromlist) -> module\n\ |
| 50 | \n\ |
| 51 | Import a module. The globals are only used to determine the context;\n\ |
| 52 | they are not modified. The locals are currently unused. The fromlist\n\ |
| 53 | should be a list of names to emulate ``from name import ...'', or an\n\ |
| 54 | empty list to emulate ``import name''.\n\ |
| 55 | When importing a module from a package, note that __import__('A.B', ...)\n\ |
| 56 | returns package A when fromlist is empty, but its submodule B when\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 57 | fromlist is not empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 60 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 61 | builtin_abs(PyObject *self, PyObject *v) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 62 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 63 | return PyNumber_Absolute(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 66 | PyDoc_STRVAR(abs_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 67 | "abs(number) -> number\n\ |
| 68 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 69 | Return the absolute value of the argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 70 | |
| 71 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 72 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 73 | builtin_apply(PyObject *self, PyObject *args) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 74 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 75 | PyObject *func, *alist = NULL, *kwdict = NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 76 | PyObject *t = NULL, *retval = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | |
Neal Norwitz | 3e59076 | 2003-02-23 21:45:43 +0000 | [diff] [blame] | 78 | if (PyErr_Warn(PyExc_PendingDeprecationWarning, |
| 79 | "use func(*args, **kwargs) instead of " |
| 80 | "apply(func, args, kwargs)") < 0) |
| 81 | return NULL; |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 82 | if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict)) |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 83 | return NULL; |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 84 | if (alist != NULL) { |
| 85 | if (!PyTuple_Check(alist)) { |
| 86 | if (!PySequence_Check(alist)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 87 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 88 | "apply() arg 2 expected sequence, found %s", |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 89 | alist->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 90 | return NULL; |
| 91 | } |
| 92 | t = PySequence_Tuple(alist); |
| 93 | if (t == NULL) |
| 94 | return NULL; |
| 95 | alist = t; |
| 96 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 97 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 98 | if (kwdict != NULL && !PyDict_Check(kwdict)) { |
Jeremy Hylton | c862cf4 | 2001-01-19 03:25:05 +0000 | [diff] [blame] | 99 | PyErr_Format(PyExc_TypeError, |
| 100 | "apply() arg 3 expected dictionary, found %s", |
| 101 | kwdict->ob_type->tp_name); |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 102 | goto finally; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 103 | } |
Barry Warsaw | 968f8cb | 1998-10-01 15:33:12 +0000 | [diff] [blame] | 104 | retval = PyEval_CallObjectWithKeywords(func, alist, kwdict); |
| 105 | finally: |
| 106 | Py_XDECREF(t); |
| 107 | return retval; |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 110 | PyDoc_STRVAR(apply_doc, |
Fred Drake | f1fbc62 | 2001-01-12 17:05:05 +0000 | [diff] [blame] | 111 | "apply(object[, args[, kwargs]]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 112 | \n\ |
Fred Drake | 7b91212 | 1999-12-23 14:16:55 +0000 | [diff] [blame] | 113 | Call a callable object with positional arguments taken from the tuple args,\n\ |
| 114 | and keyword arguments taken from the optional dictionary kwargs.\n\ |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 115 | Note that classes are callable, as are instances with a __call__() method.\n\ |
| 116 | \n\ |
| 117 | Deprecated since release 2.3. Instead, use the extended call syntax:\n\ |
| 118 | function(*args, **keywords)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 119 | |
| 120 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 121 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 122 | builtin_callable(PyObject *self, PyObject *v) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 123 | { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 124 | return PyBool_FromLong((long)PyCallable_Check(v)); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 127 | PyDoc_STRVAR(callable_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 128 | "callable(object) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 129 | \n\ |
| 130 | Return whether the object is callable (i.e., some kind of function).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 131 | Note that classes are callable, as are instances with a __call__() method."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 132 | |
| 133 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 134 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 135 | builtin_filter(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 136 | { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 137 | PyObject *func, *seq, *result, *it, *arg; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 138 | int len; /* guess for result list size */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 139 | register int j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 140 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 141 | if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq)) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 142 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 143 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 144 | /* Strings and tuples return a result of the same type. */ |
| 145 | if (PyString_Check(seq)) |
| 146 | return filterstring(func, seq); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 147 | #ifdef Py_USING_UNICODE |
| 148 | if (PyUnicode_Check(seq)) |
| 149 | return filterunicode(func, seq); |
| 150 | #endif |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 151 | if (PyTuple_Check(seq)) |
| 152 | return filtertuple(func, seq); |
| 153 | |
| 154 | /* Get iterator. */ |
| 155 | it = PyObject_GetIter(seq); |
| 156 | if (it == NULL) |
| 157 | return NULL; |
| 158 | |
| 159 | /* Guess a result list size. */ |
| 160 | len = -1; /* unknown */ |
| 161 | if (PySequence_Check(seq) && |
| 162 | seq->ob_type->tp_as_sequence->sq_length) { |
| 163 | len = PySequence_Size(seq); |
| 164 | if (len < 0) |
| 165 | PyErr_Clear(); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 166 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 167 | if (len < 0) |
| 168 | len = 8; /* arbitrary */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 170 | /* Pre-allocate argument list tuple. */ |
| 171 | arg = PyTuple_New(1); |
| 172 | if (arg == NULL) |
| 173 | goto Fail_arg; |
| 174 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 175 | /* Get a result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 176 | if (PyList_Check(seq) && seq->ob_refcnt == 1) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 177 | /* Eww - can modify the list in-place. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 178 | Py_INCREF(seq); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 179 | result = seq; |
| 180 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 181 | else { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 182 | result = PyList_New(len); |
| 183 | if (result == NULL) |
| 184 | goto Fail_it; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 185 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 186 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 187 | /* Build the result list. */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 188 | j = 0; |
| 189 | for (;;) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 190 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 191 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 192 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 193 | item = PyIter_Next(it); |
| 194 | if (item == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 195 | if (PyErr_Occurred()) |
| 196 | goto Fail_result_it; |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 197 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 198 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 199 | |
Neil Schemenauer | 6897355 | 2003-08-14 20:37:34 +0000 | [diff] [blame] | 200 | if (func == (PyObject *)&PyBool_Type || func == Py_None) { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 201 | ok = PyObject_IsTrue(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 202 | } |
| 203 | else { |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 204 | PyObject *good; |
| 205 | PyTuple_SET_ITEM(arg, 0, item); |
| 206 | good = PyObject_Call(func, arg, NULL); |
| 207 | PyTuple_SET_ITEM(arg, 0, NULL); |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 208 | if (good == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 209 | Py_DECREF(item); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 210 | goto Fail_result_it; |
Guido van Rossum | 58b6873 | 1995-01-10 17:40:55 +0000 | [diff] [blame] | 211 | } |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 212 | ok = PyObject_IsTrue(good); |
| 213 | Py_DECREF(good); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 214 | } |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 215 | if (ok) { |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 216 | if (j < len) |
| 217 | PyList_SET_ITEM(result, j, item); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 218 | else { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 219 | int status = PyList_Append(result, item); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 220 | Py_DECREF(item); |
| 221 | if (status < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 222 | goto Fail_result_it; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 223 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 224 | ++j; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 225 | } |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 226 | else |
| 227 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 230 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 231 | /* Cut back result list if len is too big. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 232 | if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 233 | goto Fail_result_it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 234 | |
Tim Peters | 3c6b148 | 2001-05-21 08:07:05 +0000 | [diff] [blame] | 235 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 236 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 237 | return result; |
| 238 | |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 239 | Fail_result_it: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 240 | Py_DECREF(result); |
Tim Peters | 0e57abf | 2001-05-02 07:39:38 +0000 | [diff] [blame] | 241 | Fail_it: |
| 242 | Py_DECREF(it); |
Guido van Rossum | c7903a1 | 2002-08-16 07:04:56 +0000 | [diff] [blame] | 243 | Fail_arg: |
| 244 | Py_DECREF(arg); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 245 | return NULL; |
| 246 | } |
| 247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 248 | PyDoc_STRVAR(filter_doc, |
Tim Peters | d50e544 | 2002-03-09 00:06:26 +0000 | [diff] [blame] | 249 | "filter(function or None, sequence) -> list, tuple, or string\n" |
| 250 | "\n" |
| 251 | "Return those items of sequence for which function(item) is true. If\n" |
| 252 | "function is None, return the items that are true. If sequence is a tuple\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 253 | "or string, return the same type, else return a list."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 255 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 256 | builtin_chr(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 257 | { |
| 258 | long x; |
| 259 | char s[1]; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 261 | if (!PyArg_ParseTuple(args, "l:chr", &x)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 262 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 263 | if (x < 0 || x >= 256) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 264 | PyErr_SetString(PyExc_ValueError, |
| 265 | "chr() arg not in range(256)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 268 | s[0] = (char)x; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 269 | return PyString_FromStringAndSize(s, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 272 | PyDoc_STRVAR(chr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 273 | "chr(i) -> character\n\ |
| 274 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 275 | Return a string of one character with ordinal i; 0 <= i < 256."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 276 | |
| 277 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 278 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 279 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 280 | builtin_unichr(PyObject *self, PyObject *args) |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 281 | { |
| 282 | long x; |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 283 | |
| 284 | if (!PyArg_ParseTuple(args, "l:unichr", &x)) |
| 285 | return NULL; |
Fredrik Lundh | 0dcf67e | 2001-06-26 20:01:56 +0000 | [diff] [blame] | 286 | |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 287 | return PyUnicode_FromOrdinal(x); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 290 | PyDoc_STRVAR(unichr_doc, |
Fred Drake | 078b24f | 2000-04-13 02:42:50 +0000 | [diff] [blame] | 291 | "unichr(i) -> Unicode character\n\ |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 292 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 293 | Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 294 | #endif |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 295 | |
| 296 | |
| 297 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 298 | builtin_cmp(PyObject *self, PyObject *args) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 299 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 300 | PyObject *a, *b; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 301 | int c; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 302 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 303 | if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b)) |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 304 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 305 | if (PyObject_Cmp(a, b, &c) < 0) |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 306 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 307 | return PyInt_FromLong((long)c); |
Guido van Rossum | a9e7dc1 | 1992-10-18 18:53:57 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 310 | PyDoc_STRVAR(cmp_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 311 | "cmp(x, y) -> integer\n\ |
| 312 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 313 | Return negative if x<y, zero if x==y, positive if x>y."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 314 | |
| 315 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 316 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 317 | builtin_coerce(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 318 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 319 | PyObject *v, *w; |
| 320 | PyObject *res; |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 321 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 322 | if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w)) |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 323 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 324 | if (PyNumber_Coerce(&v, &w) < 0) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 325 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 326 | res = Py_BuildValue("(OO)", v, w); |
| 327 | Py_DECREF(v); |
| 328 | Py_DECREF(w); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 329 | return res; |
| 330 | } |
| 331 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 332 | PyDoc_STRVAR(coerce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 333 | "coerce(x, y) -> None or (x1, y1)\n\ |
| 334 | \n\ |
| 335 | When x and y can be coerced to values of the same type, return a tuple\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 336 | containing the coerced values. When they can't be coerced, return None."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 337 | |
| 338 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 339 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 340 | builtin_compile(PyObject *self, PyObject *args) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 341 | { |
| 342 | char *str; |
| 343 | char *filename; |
| 344 | char *startstr; |
| 345 | int start; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 346 | int dont_inherit = 0; |
| 347 | int supplied_flags = 0; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 348 | PyCompilerFlags cf; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 349 | PyObject *result, *cmd, *tmp = NULL; |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 350 | int length; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 351 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 352 | if (!PyArg_ParseTuple(args, "Oss|ii:compile", &cmd, &filename, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 353 | &startstr, &supplied_flags, &dont_inherit)) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 354 | return NULL; |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 355 | |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 356 | cf.cf_flags = supplied_flags; |
| 357 | |
| 358 | #ifdef Py_USING_UNICODE |
| 359 | if (PyUnicode_Check(cmd)) { |
| 360 | tmp = PyUnicode_AsUTF8String(cmd); |
| 361 | if (tmp == NULL) |
| 362 | return NULL; |
| 363 | cmd = tmp; |
| 364 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 365 | } |
| 366 | #endif |
Just van Rossum | b9b8e9c | 2003-02-10 09:22:01 +0000 | [diff] [blame] | 367 | if (PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) |
| 368 | return NULL; |
Tim Peters | 2c646c9 | 2003-02-10 14:48:29 +0000 | [diff] [blame] | 369 | if ((size_t)length != strlen(str)) { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 370 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 371 | "compile() expected string without null bytes"); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 375 | if (strcmp(startstr, "exec") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 376 | start = Py_file_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 377 | else if (strcmp(startstr, "eval") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 378 | start = Py_eval_input; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 379 | else if (strcmp(startstr, "single") == 0) |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 380 | start = Py_single_input; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 381 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 382 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 383 | "compile() arg 3 must be 'exec' or 'eval' or 'single'"); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 384 | return NULL; |
| 385 | } |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | 4b499dd3 | 2003-02-13 22:07:59 +0000 | [diff] [blame] | 387 | if (supplied_flags & |
| 388 | ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT)) |
| 389 | { |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 390 | PyErr_SetString(PyExc_ValueError, |
| 391 | "compile(): unrecognised flags"); |
| 392 | return NULL; |
| 393 | } |
| 394 | /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ |
| 395 | |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 396 | if (!dont_inherit) { |
| 397 | PyEval_MergeCompilerFlags(&cf); |
| 398 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 399 | result = Py_CompileStringFlags(str, filename, start, &cf); |
| 400 | Py_XDECREF(tmp); |
| 401 | return result; |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 404 | PyDoc_STRVAR(compile_doc, |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 405 | "compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 406 | \n\ |
| 407 | Compile the source string (a Python module, statement or expression)\n\ |
| 408 | into a code object that can be executed by the exec statement or eval().\n\ |
| 409 | The filename will be used for run-time error messages.\n\ |
| 410 | The mode must be 'exec' to compile a module, 'single' to compile a\n\ |
Tim Peters | 6cd6a82 | 2001-08-17 22:11:27 +0000 | [diff] [blame] | 411 | single (interactive) statement, or 'eval' to compile an expression.\n\ |
| 412 | The flags argument, if present, controls which future statements influence\n\ |
| 413 | the compilation of the code.\n\ |
| 414 | The dont_inherit argument, if non-zero, stops the compilation inheriting\n\ |
| 415 | the effects of any future statements in effect in the code calling\n\ |
| 416 | compile; if absent or zero these statements do influence the compilation,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 417 | in addition to any features explicitly specified."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 419 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 420 | builtin_dir(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 421 | { |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 422 | PyObject *arg = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 423 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 424 | if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 425 | return NULL; |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 426 | return PyObject_Dir(arg); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 429 | PyDoc_STRVAR(dir_doc, |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 430 | "dir([object]) -> list of strings\n" |
| 431 | "\n" |
| 432 | "Return an alphabetized list of names comprising (some of) the attributes\n" |
| 433 | "of the given object, and of attributes reachable from it:\n" |
| 434 | "\n" |
| 435 | "No argument: the names in the current scope.\n" |
| 436 | "Module object: the module attributes.\n" |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 437 | "Type or class object: its attributes, and recursively the attributes of\n" |
| 438 | " its bases.\n" |
Tim Peters | 5d2b77c | 2001-09-03 05:47:38 +0000 | [diff] [blame] | 439 | "Otherwise: its attributes, its class's attributes, and recursively the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 440 | " attributes of its class's base classes."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 442 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 443 | builtin_divmod(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 444 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 445 | PyObject *v, *w; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 446 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 447 | if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 448 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 449 | return PyNumber_Divmod(v, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 452 | PyDoc_STRVAR(divmod_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 453 | "divmod(x, y) -> (div, mod)\n\ |
| 454 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 455 | Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 456 | |
| 457 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 458 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 459 | builtin_eval(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 460 | { |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 461 | PyObject *cmd, *result, *tmp = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 462 | PyObject *globals = Py_None, *locals = Py_None; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 463 | char *str; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 464 | PyCompilerFlags cf; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 465 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 466 | if (!PyArg_ParseTuple(args, "O|O!O!:eval", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 467 | &cmd, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 468 | &PyDict_Type, &globals, |
| 469 | &PyDict_Type, &locals)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 470 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 471 | if (globals == Py_None) { |
| 472 | globals = PyEval_GetGlobals(); |
| 473 | if (locals == Py_None) |
| 474 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 475 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 476 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 477 | locals = globals; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 479 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 480 | if (PyDict_SetItemString(globals, "__builtins__", |
| 481 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 482 | return NULL; |
| 483 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 484 | |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 485 | if (PyCode_Check(cmd)) { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 486 | if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "code object passed to eval() may not contain free variables"); |
| 489 | return NULL; |
| 490 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 491 | return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); |
Jeremy Hylton | 15c1c4f | 2001-07-30 21:50:55 +0000 | [diff] [blame] | 492 | } |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 493 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 494 | if (!PyString_Check(cmd) && |
| 495 | !PyUnicode_Check(cmd)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 496 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 497 | "eval() arg 1 must be a string or code object"); |
Guido van Rossum | 94390a4 | 1992-08-14 15:14:30 +0000 | [diff] [blame] | 498 | return NULL; |
| 499 | } |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 500 | cf.cf_flags = 0; |
| 501 | |
| 502 | #ifdef Py_USING_UNICODE |
| 503 | if (PyUnicode_Check(cmd)) { |
| 504 | tmp = PyUnicode_AsUTF8String(cmd); |
| 505 | if (tmp == NULL) |
| 506 | return NULL; |
| 507 | cmd = tmp; |
| 508 | cf.cf_flags |= PyCF_SOURCE_IS_UTF8; |
| 509 | } |
| 510 | #endif |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 511 | if (PyString_AsStringAndSize(cmd, &str, NULL)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 512 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 513 | while (*str == ' ' || *str == '\t') |
| 514 | str++; |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 515 | |
Tim Peters | 9fa96be | 2001-08-17 23:04:59 +0000 | [diff] [blame] | 516 | (void)PyEval_MergeCompilerFlags(&cf); |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 517 | result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf); |
| 518 | Py_XDECREF(tmp); |
| 519 | return result; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 522 | PyDoc_STRVAR(eval_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 523 | "eval(source[, globals[, locals]]) -> value\n\ |
| 524 | \n\ |
| 525 | Evaluate the source in the context of globals and locals.\n\ |
| 526 | The source may be a string representing a Python expression\n\ |
| 527 | or a code object as returned by compile().\n\ |
| 528 | The globals and locals are dictionaries, defaulting to the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 529 | globals and locals. If only globals is given, locals defaults to it."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 530 | |
| 531 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 533 | builtin_execfile(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 534 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 535 | char *filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 536 | PyObject *globals = Py_None, *locals = Py_None; |
| 537 | PyObject *res; |
Guido van Rossum | b3a639e | 2001-09-05 13:37:47 +0000 | [diff] [blame] | 538 | FILE* fp = NULL; |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 539 | PyCompilerFlags cf; |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 540 | int exists; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 541 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 542 | if (!PyArg_ParseTuple(args, "s|O!O!:execfile", |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 543 | &filename, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | &PyDict_Type, &globals, |
| 545 | &PyDict_Type, &locals)) |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 546 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 547 | if (globals == Py_None) { |
| 548 | globals = PyEval_GetGlobals(); |
| 549 | if (locals == Py_None) |
| 550 | locals = PyEval_GetLocals(); |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 551 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 552 | else if (locals == Py_None) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 553 | locals = globals; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 554 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 555 | if (PyDict_SetItemString(globals, "__builtins__", |
| 556 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 559 | |
| 560 | exists = 0; |
| 561 | /* Test for existence or directory. */ |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 562 | #if defined(PLAN9) |
| 563 | { |
| 564 | Dir *d; |
| 565 | |
| 566 | if ((d = dirstat(filename))!=nil) { |
| 567 | if(d->mode & DMDIR) |
| 568 | werrstr("is a directory"); |
| 569 | else |
| 570 | exists = 1; |
| 571 | free(d); |
| 572 | } |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 573 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 574 | #elif defined(RISCOS) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 575 | if (object_exists(filename)) { |
| 576 | if (isdir(filename)) |
| 577 | errno = EISDIR; |
| 578 | else |
| 579 | exists = 1; |
| 580 | } |
Martin v. Löwis | 3484a18 | 2002-03-09 12:07:51 +0000 | [diff] [blame] | 581 | #else /* standard Posix */ |
| 582 | { |
| 583 | struct stat s; |
| 584 | if (stat(filename, &s) == 0) { |
| 585 | if (S_ISDIR(s.st_mode)) |
| 586 | # if defined(PY_OS2) && defined(PYCC_VACPP) |
| 587 | errno = EOS2ERR; |
| 588 | # else |
| 589 | errno = EISDIR; |
| 590 | # endif |
| 591 | else |
| 592 | exists = 1; |
| 593 | } |
| 594 | } |
| 595 | #endif |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 596 | |
| 597 | if (exists) { |
| 598 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 599 | fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
Martin v. Löwis | 6b3a2c4 | 2001-08-08 05:30:36 +0000 | [diff] [blame] | 600 | Py_END_ALLOW_THREADS |
| 601 | |
| 602 | if (fp == NULL) { |
| 603 | exists = 0; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (!exists) { |
Peter Schneider-Kamp | 4c01342 | 2002-08-27 16:58:00 +0000 | [diff] [blame] | 608 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename); |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 609 | return NULL; |
| 610 | } |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 611 | cf.cf_flags = 0; |
| 612 | if (PyEval_MergeCompilerFlags(&cf)) |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 613 | res = PyRun_FileExFlags(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 614 | locals, 1, &cf); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 615 | else |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 616 | res = PyRun_FileEx(fp, filename, Py_file_input, globals, |
Jeremy Hylton | bc32024 | 2001-03-22 02:47:58 +0000 | [diff] [blame] | 617 | locals, 1); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 618 | return res; |
Guido van Rossum | 0f61f8a | 1992-02-25 18:55:05 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 621 | PyDoc_STRVAR(execfile_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 622 | "execfile(filename[, globals[, locals]])\n\ |
| 623 | \n\ |
| 624 | Read and execute a Python script from a file.\n\ |
| 625 | The globals and locals are dictionaries, defaulting to the current\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 626 | globals and locals. If only globals is given, locals defaults to it."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 627 | |
| 628 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 630 | builtin_getattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 631 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 632 | PyObject *v, *result, *dflt = NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 633 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 634 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 635 | if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 636 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 637 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 638 | if (PyUnicode_Check(name)) { |
| 639 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 640 | if (name == NULL) |
| 641 | return NULL; |
| 642 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 643 | #endif |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 644 | |
| 645 | if (!PyString_Check(name)) { |
| 646 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 647 | "getattr(): attribute name must be string"); |
Jeremy Hylton | 0eb1115 | 2001-07-30 22:39:31 +0000 | [diff] [blame] | 648 | return NULL; |
| 649 | } |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 650 | result = PyObject_GetAttr(v, name); |
Guido van Rossum | d892357 | 2001-10-16 21:31:32 +0000 | [diff] [blame] | 651 | if (result == NULL && dflt != NULL && |
| 652 | PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 653 | { |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 654 | PyErr_Clear(); |
| 655 | Py_INCREF(dflt); |
| 656 | result = dflt; |
| 657 | } |
| 658 | return result; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 661 | PyDoc_STRVAR(getattr_doc, |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 662 | "getattr(object, name[, default]) -> value\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 663 | \n\ |
Guido van Rossum | 950ff29 | 1998-06-29 13:38:57 +0000 | [diff] [blame] | 664 | Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.\n\ |
| 665 | When a default argument is given, it is returned when the attribute doesn't\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 666 | exist; without it, an exception is raised in that case."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 667 | |
| 668 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 670 | builtin_globals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 671 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 672 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 673 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 674 | d = PyEval_GetGlobals(); |
| 675 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 676 | return d; |
| 677 | } |
| 678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 679 | PyDoc_STRVAR(globals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 680 | "globals() -> dictionary\n\ |
| 681 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 682 | Return the dictionary containing the current scope's global variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 683 | |
| 684 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 685 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 686 | builtin_hasattr(PyObject *self, PyObject *args) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 687 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 688 | PyObject *v; |
| 689 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 690 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 691 | if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 692 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 693 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 694 | if (PyUnicode_Check(name)) { |
| 695 | name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 696 | if (name == NULL) |
| 697 | return NULL; |
| 698 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 699 | #endif |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 700 | |
| 701 | if (!PyString_Check(name)) { |
| 702 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 703 | "hasattr(): attribute name must be string"); |
Jeremy Hylton | 302b54a | 2001-07-30 22:45:19 +0000 | [diff] [blame] | 704 | return NULL; |
| 705 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 706 | v = PyObject_GetAttr(v, name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 707 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 708 | PyErr_Clear(); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 709 | Py_INCREF(Py_False); |
| 710 | return Py_False; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 711 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 712 | Py_DECREF(v); |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 713 | Py_INCREF(Py_True); |
| 714 | return Py_True; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 717 | PyDoc_STRVAR(hasattr_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 718 | "hasattr(object, name) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 719 | \n\ |
| 720 | Return whether the object has an attribute with the given name.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 721 | (This is done by calling getattr(object, name) and catching exceptions.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 722 | |
| 723 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 724 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 725 | builtin_id(PyObject *self, PyObject *v) |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 726 | { |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 727 | return PyLong_FromVoidPtr(v); |
Guido van Rossum | 5b72218 | 1993-03-30 17:46:03 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 730 | PyDoc_STRVAR(id_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 731 | "id(object) -> integer\n\ |
| 732 | \n\ |
| 733 | Return the identity of an object. This is guaranteed to be unique among\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 734 | simultaneously existing objects. (Hint: it's the object's memory address.)"); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 735 | |
| 736 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 737 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 738 | builtin_map(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 739 | { |
| 740 | typedef struct { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 741 | PyObject *it; /* the iterator object */ |
| 742 | int saw_StopIteration; /* bool: did the iterator end? */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 743 | } sequence; |
| 744 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 745 | PyObject *func, *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 746 | sequence *seqs = NULL, *sqp; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 747 | int n, len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 748 | register int i, j; |
| 749 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 750 | n = PyTuple_Size(args); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 751 | if (n < 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 752 | PyErr_SetString(PyExc_TypeError, |
| 753 | "map() requires at least two args"); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 754 | return NULL; |
| 755 | } |
| 756 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 757 | func = PyTuple_GetItem(args, 0); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 758 | n--; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 759 | |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 760 | if (func == Py_None && n == 1) { |
| 761 | /* map(None, S) is the same as list(S). */ |
| 762 | return PySequence_List(PyTuple_GetItem(args, 1)); |
| 763 | } |
| 764 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 765 | /* Get space for sequence descriptors. Must NULL out the iterator |
| 766 | * pointers so that jumping to Fail_2 later doesn't see trash. |
| 767 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 768 | if ((seqs = PyMem_NEW(sequence, n)) == NULL) { |
| 769 | PyErr_NoMemory(); |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 770 | return NULL; |
| 771 | } |
| 772 | for (i = 0; i < n; ++i) { |
| 773 | seqs[i].it = (PyObject*)NULL; |
| 774 | seqs[i].saw_StopIteration = 0; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 775 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 776 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 777 | /* Do a first pass to obtain iterators for the arguments, and set len |
| 778 | * to the largest of their lengths. |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 779 | */ |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 780 | len = 0; |
| 781 | for (i = 0, sqp = seqs; i < n; ++i, ++sqp) { |
| 782 | PyObject *curseq; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 783 | int curlen; |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 784 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 785 | /* Get iterator. */ |
| 786 | curseq = PyTuple_GetItem(args, i+1); |
| 787 | sqp->it = PyObject_GetIter(curseq); |
| 788 | if (sqp->it == NULL) { |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 789 | static char errmsg[] = |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 790 | "argument %d to map() must support iteration"; |
Guido van Rossum | 15e33a4 | 1997-04-30 19:00:27 +0000 | [diff] [blame] | 791 | char errbuf[sizeof(errmsg) + 25]; |
Jeremy Hylton | 518ab1c | 2001-11-28 20:42:20 +0000 | [diff] [blame] | 792 | PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 793 | PyErr_SetString(PyExc_TypeError, errbuf); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 794 | goto Fail_2; |
| 795 | } |
| 796 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 797 | /* Update len. */ |
| 798 | curlen = -1; /* unknown */ |
| 799 | if (PySequence_Check(curseq) && |
| 800 | curseq->ob_type->tp_as_sequence->sq_length) { |
| 801 | curlen = PySequence_Size(curseq); |
| 802 | if (curlen < 0) |
| 803 | PyErr_Clear(); |
| 804 | } |
Tim Peters | 748b8bb | 2001-04-28 08:20:22 +0000 | [diff] [blame] | 805 | if (curlen < 0) |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 806 | curlen = 8; /* arbitrary */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 807 | if (curlen > len) |
| 808 | len = curlen; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 811 | /* Get space for the result list. */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 812 | if ((result = (PyObject *) PyList_New(len)) == NULL) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 813 | goto Fail_2; |
| 814 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 815 | /* Iterate over the sequences until all have stopped. */ |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 816 | for (i = 0; ; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 817 | PyObject *alist, *item=NULL, *value; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 818 | int numactive = 0; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 820 | if (func == Py_None && n == 1) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 821 | alist = NULL; |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 822 | else if ((alist = PyTuple_New(n)) == NULL) |
| 823 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 824 | |
| 825 | for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 826 | if (sqp->saw_StopIteration) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 827 | Py_INCREF(Py_None); |
| 828 | item = Py_None; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 829 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 830 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 831 | item = PyIter_Next(sqp->it); |
| 832 | if (item) |
| 833 | ++numactive; |
| 834 | else { |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 835 | if (PyErr_Occurred()) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 836 | Py_XDECREF(alist); |
| 837 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 838 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 839 | Py_INCREF(Py_None); |
| 840 | item = Py_None; |
| 841 | sqp->saw_StopIteration = 1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 842 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 843 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 844 | if (alist) |
| 845 | PyTuple_SET_ITEM(alist, j, item); |
| 846 | else |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 847 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 850 | if (!alist) |
| 851 | alist = item; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 852 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 853 | if (numactive == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 854 | Py_DECREF(alist); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 855 | break; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 856 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 858 | if (func == Py_None) |
Guido van Rossum | 3212031 | 1995-07-10 13:52:21 +0000 | [diff] [blame] | 859 | value = alist; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 860 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 861 | value = PyEval_CallObject(func, alist); |
| 862 | Py_DECREF(alist); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 863 | if (value == NULL) |
| 864 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 865 | } |
| 866 | if (i >= len) { |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 867 | int status = PyList_Append(result, value); |
Barry Warsaw | 2133287 | 1999-01-28 04:21:35 +0000 | [diff] [blame] | 868 | Py_DECREF(value); |
Barry Warsaw | fa77e09 | 1999-01-28 18:49:12 +0000 | [diff] [blame] | 869 | if (status < 0) |
| 870 | goto Fail_1; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 871 | } |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 872 | else if (PyList_SetItem(result, i, value) < 0) |
| 873 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Guido van Rossum | fa4ac71 | 1998-07-10 17:37:30 +0000 | [diff] [blame] | 876 | if (i < len && PyList_SetSlice(result, i, len, NULL) < 0) |
| 877 | goto Fail_1; |
| 878 | |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 879 | goto Succeed; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 880 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 881 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 882 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 883 | Fail_2: |
Tim Peters | 4e9afdc | 2001-05-03 23:54:49 +0000 | [diff] [blame] | 884 | result = NULL; |
| 885 | Succeed: |
| 886 | assert(seqs); |
| 887 | for (i = 0; i < n; ++i) |
| 888 | Py_XDECREF(seqs[i].it); |
| 889 | PyMem_DEL(seqs); |
| 890 | return result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 893 | PyDoc_STRVAR(map_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 894 | "map(function, sequence[, sequence, ...]) -> list\n\ |
| 895 | \n\ |
| 896 | Return a list of the results of applying the function to the items of\n\ |
| 897 | the argument sequence(s). If more than one sequence is given, the\n\ |
| 898 | function is called with an argument list consisting of the corresponding\n\ |
| 899 | item of each sequence, substituting None for missing values when not all\n\ |
| 900 | sequences have the same length. If the function is None, return a list of\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 901 | the items of the sequence (or a list of tuples if more than one sequence)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 902 | |
| 903 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 904 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 905 | builtin_setattr(PyObject *self, PyObject *args) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 906 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 907 | PyObject *v; |
| 908 | PyObject *name; |
| 909 | PyObject *value; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 910 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 911 | if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 912 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 913 | if (PyObject_SetAttr(v, name, value) != 0) |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 914 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 915 | Py_INCREF(Py_None); |
| 916 | return Py_None; |
Guido van Rossum | 33894be | 1992-01-27 16:53:09 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 919 | PyDoc_STRVAR(setattr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 920 | "setattr(object, name, value)\n\ |
| 921 | \n\ |
| 922 | Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 923 | ``x.y = v''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 924 | |
| 925 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 926 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 927 | builtin_delattr(PyObject *self, PyObject *args) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 928 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 929 | PyObject *v; |
| 930 | PyObject *name; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 931 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 932 | if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 933 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 934 | if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 935 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 936 | Py_INCREF(Py_None); |
| 937 | return Py_None; |
Guido van Rossum | 14144fc | 1994-08-29 12:53:40 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 940 | PyDoc_STRVAR(delattr_doc, |
Guido van Rossum | df12a59 | 1998-11-23 22:13:04 +0000 | [diff] [blame] | 941 | "delattr(object, name)\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 942 | \n\ |
| 943 | Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 944 | ``del x.y''."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 945 | |
| 946 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 947 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 948 | builtin_hash(PyObject *self, PyObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 949 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 950 | long x; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 951 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 952 | x = PyObject_Hash(v); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 953 | if (x == -1) |
| 954 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 955 | return PyInt_FromLong(x); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 958 | PyDoc_STRVAR(hash_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 959 | "hash(object) -> integer\n\ |
| 960 | \n\ |
| 961 | Return a hash value for the object. Two objects with the same value have\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 962 | the same hash value. The reverse is not necessarily true, but likely."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 963 | |
| 964 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 965 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 966 | builtin_hex(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 967 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 968 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 969 | |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 970 | if ((nb = v->ob_type->tp_as_number) == NULL || |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 971 | nb->nb_hex == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 972 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 973 | "hex() argument can't be converted to hex"); |
| 974 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 975 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 976 | return (*nb->nb_hex)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 979 | PyDoc_STRVAR(hex_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 980 | "hex(number) -> string\n\ |
| 981 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 982 | Return the hexadecimal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 983 | |
| 984 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 985 | static PyObject *builtin_raw_input(PyObject *, PyObject *); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 986 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 987 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 988 | builtin_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 989 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 990 | PyObject *line; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 991 | char *str; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 992 | PyObject *res; |
| 993 | PyObject *globals, *locals; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 994 | |
| 995 | line = builtin_raw_input(self, args); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 996 | if (line == NULL) |
| 997 | return line; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 998 | if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | while (*str == ' ' || *str == '\t') |
| 1001 | str++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1002 | globals = PyEval_GetGlobals(); |
| 1003 | locals = PyEval_GetLocals(); |
| 1004 | if (PyDict_GetItemString(globals, "__builtins__") == NULL) { |
| 1005 | if (PyDict_SetItemString(globals, "__builtins__", |
| 1006 | PyEval_GetBuiltins()) != 0) |
Guido van Rossum | 6135a87 | 1995-01-09 17:53:26 +0000 | [diff] [blame] | 1007 | return NULL; |
| 1008 | } |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 1009 | res = PyRun_String(str, Py_eval_input, globals, locals); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1010 | Py_DECREF(line); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1011 | return res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1014 | PyDoc_STRVAR(input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1015 | "input([prompt]) -> value\n\ |
| 1016 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1017 | Equivalent to eval(raw_input(prompt))."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1018 | |
| 1019 | |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1020 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1021 | builtin_intern(PyObject *self, PyObject *args) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1022 | { |
| 1023 | PyObject *s; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1024 | if (!PyArg_ParseTuple(args, "S:intern", &s)) |
Guido van Rossum | e8811f8 | 1997-02-14 15:48:05 +0000 | [diff] [blame] | 1025 | return NULL; |
| 1026 | Py_INCREF(s); |
| 1027 | PyString_InternInPlace(&s); |
| 1028 | return s; |
| 1029 | } |
| 1030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1031 | PyDoc_STRVAR(intern_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1032 | "intern(string) -> string\n\ |
| 1033 | \n\ |
| 1034 | ``Intern'' the given string. This enters the string in the (global)\n\ |
| 1035 | table of interned strings whose purpose is to speed up dictionary lookups.\n\ |
| 1036 | Return the string itself or the previously interned string object with the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1037 | same value."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1038 | |
| 1039 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1040 | static PyObject * |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1041 | builtin_iter(PyObject *self, PyObject *args) |
| 1042 | { |
| 1043 | PyObject *v, *w = NULL; |
| 1044 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1045 | if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w)) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1046 | return NULL; |
| 1047 | if (w == NULL) |
| 1048 | return PyObject_GetIter(v); |
| 1049 | if (!PyCallable_Check(v)) { |
| 1050 | PyErr_SetString(PyExc_TypeError, |
| 1051 | "iter(v, w): v must be callable"); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | return PyCallIter_New(v, w); |
| 1055 | } |
| 1056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1057 | PyDoc_STRVAR(iter_doc, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1058 | "iter(collection) -> iterator\n\ |
| 1059 | iter(callable, sentinel) -> iterator\n\ |
| 1060 | \n\ |
| 1061 | Get an iterator from an object. In the first form, the argument must\n\ |
| 1062 | supply its own iterator, or be a sequence.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1063 | In the second form, the callable is called until it returns the sentinel."); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1064 | |
| 1065 | |
| 1066 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1067 | builtin_len(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1068 | { |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 1069 | long res; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1070 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1071 | res = PyObject_Size(v); |
Guido van Rossum | 8ea9f4d | 1998-06-29 22:26:50 +0000 | [diff] [blame] | 1072 | if (res < 0 && PyErr_Occurred()) |
| 1073 | return NULL; |
| 1074 | return PyInt_FromLong(res); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1077 | PyDoc_STRVAR(len_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1078 | "len(object) -> integer\n\ |
| 1079 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1080 | Return the number of items of a sequence or mapping."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1081 | |
| 1082 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1083 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1084 | builtin_locals(PyObject *self) |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1085 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1086 | PyObject *d; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1087 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1088 | d = PyEval_GetLocals(); |
| 1089 | Py_INCREF(d); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1090 | return d; |
| 1091 | } |
| 1092 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1093 | PyDoc_STRVAR(locals_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1094 | "locals() -> dictionary\n\ |
| 1095 | \n\ |
Raymond Hettinger | 69bf8f3 | 2003-01-04 02:16:22 +0000 | [diff] [blame] | 1096 | Update and return a dictionary containing the current scope's local variables."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1097 | |
| 1098 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1099 | static PyObject * |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1100 | min_max(PyObject *args, int op) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1101 | { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1102 | PyObject *v, *w, *x, *it; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1103 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1104 | if (PyTuple_Size(args) > 1) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1105 | v = args; |
Raymond Hettinger | bbfb910 | 2002-12-29 18:31:19 +0000 | [diff] [blame] | 1106 | else if (!PyArg_UnpackTuple(args, (op==Py_LT) ? "min" : "max", 1, 1, &v)) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1107 | return NULL; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1108 | |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1109 | it = PyObject_GetIter(v); |
| 1110 | if (it == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1111 | return NULL; |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1112 | |
| 1113 | w = NULL; /* the result */ |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1114 | for (;;) { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1115 | x = PyIter_Next(it); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1116 | if (x == NULL) { |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1117 | if (PyErr_Occurred()) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1118 | Py_XDECREF(w); |
| 1119 | Py_DECREF(it); |
| 1120 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1121 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1122 | break; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1123 | } |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1124 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1125 | if (w == NULL) |
| 1126 | w = x; |
| 1127 | else { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1128 | int cmp = PyObject_RichCompareBool(x, w, op); |
| 1129 | if (cmp > 0) { |
| 1130 | Py_DECREF(w); |
| 1131 | w = x; |
| 1132 | } |
| 1133 | else if (cmp < 0) { |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1134 | Py_DECREF(x); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1135 | Py_DECREF(w); |
| 1136 | Py_DECREF(it); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1137 | return NULL; |
| 1138 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1139 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1140 | Py_DECREF(x); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1141 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1142 | } |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1143 | if (w == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1144 | PyErr_SetString(PyExc_ValueError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1145 | "min() or max() arg is an empty sequence"); |
Tim Peters | c307453 | 2001-05-03 07:00:32 +0000 | [diff] [blame] | 1146 | Py_DECREF(it); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1147 | return w; |
| 1148 | } |
| 1149 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1150 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1151 | builtin_min(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1152 | { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1153 | return min_max(v, Py_LT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1156 | PyDoc_STRVAR(min_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1157 | "min(sequence) -> value\n\ |
| 1158 | min(a, b, c, ...) -> value\n\ |
| 1159 | \n\ |
| 1160 | With a single sequence argument, return its smallest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1161 | With two or more arguments, return the smallest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1162 | |
| 1163 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1164 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1165 | builtin_max(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1166 | { |
Guido van Rossum | 53451b3 | 2001-01-17 15:47:24 +0000 | [diff] [blame] | 1167 | return min_max(v, Py_GT); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1170 | PyDoc_STRVAR(max_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1171 | "max(sequence) -> value\n\ |
| 1172 | max(a, b, c, ...) -> value\n\ |
| 1173 | \n\ |
| 1174 | With a single sequence argument, return its largest item.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1175 | With two or more arguments, return the largest argument."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1178 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1179 | builtin_oct(PyObject *self, PyObject *v) |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1180 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1181 | PyNumberMethods *nb; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1182 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1183 | if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || |
| 1184 | nb->nb_oct == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1185 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1186 | "oct() argument can't be converted to oct"); |
| 1187 | return NULL; |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1188 | } |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1189 | return (*nb->nb_oct)(v); |
Guido van Rossum | 006bcd4 | 1991-10-24 14:54:44 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1192 | PyDoc_STRVAR(oct_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1193 | "oct(number) -> string\n\ |
| 1194 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1195 | Return the octal representation of an integer or long integer."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1196 | |
| 1197 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1198 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1199 | builtin_ord(PyObject *self, PyObject* obj) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1200 | { |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1201 | long ord; |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1202 | int size; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1203 | |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1204 | if (PyString_Check(obj)) { |
| 1205 | size = PyString_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1206 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1207 | ord = (long)((unsigned char)*PyString_AS_STRING(obj)); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1208 | return PyInt_FromLong(ord); |
| 1209 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1210 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1211 | } else if (PyUnicode_Check(obj)) { |
| 1212 | size = PyUnicode_GET_SIZE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1213 | if (size == 1) { |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1214 | ord = (long)*PyUnicode_AS_UNICODE(obj); |
Andrew M. Kuchling | 34c20cf | 2000-12-20 14:36:56 +0000 | [diff] [blame] | 1215 | return PyInt_FromLong(ord); |
| 1216 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1217 | #endif |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1218 | } else { |
| 1219 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1220 | "ord() expected string of length 1, but " \ |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1221 | "%.200s found", obj->ob_type->tp_name); |
Guido van Rossum | 09095f3 | 2000-03-10 23:00:52 +0000 | [diff] [blame] | 1222 | return NULL; |
| 1223 | } |
| 1224 | |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1225 | PyErr_Format(PyExc_TypeError, |
Andrew M. Kuchling | f07aad1 | 2000-12-23 14:11:28 +0000 | [diff] [blame] | 1226 | "ord() expected a character, " |
| 1227 | "but string of length %d found", |
Jeremy Hylton | 394b54d | 2000-04-12 21:19:47 +0000 | [diff] [blame] | 1228 | size); |
| 1229 | return NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1232 | PyDoc_STRVAR(ord_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1233 | "ord(c) -> integer\n\ |
| 1234 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1235 | Return the integer ordinal of a one-character string."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1236 | |
| 1237 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1238 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1239 | builtin_pow(PyObject *self, PyObject *args) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1240 | { |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1241 | PyObject *v, *w, *z = Py_None; |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1242 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1243 | if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) |
Guido van Rossum | 6a00cd8 | 1995-01-07 12:39:01 +0000 | [diff] [blame] | 1244 | return NULL; |
Guido van Rossum | 09df08a | 1998-05-22 00:51:39 +0000 | [diff] [blame] | 1245 | return PyNumber_Power(v, w, z); |
Guido van Rossum | d490545 | 1991-05-05 20:00:36 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1248 | PyDoc_STRVAR(pow_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1249 | "pow(x, y[, z]) -> number\n\ |
| 1250 | \n\ |
| 1251 | With two arguments, equivalent to x**y. With three arguments,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1252 | equivalent to (x**y) % z, but may be more efficient (e.g. for longs)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1253 | |
| 1254 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1255 | |
| 1256 | /* Return number of items in range (lo, hi, step), when arguments are |
| 1257 | * PyInt or PyLong objects. step > 0 required. Return a value < 0 if |
| 1258 | * & only if the true value is too large to fit in a signed long. |
| 1259 | * Arguments MUST return 1 with either PyInt_Check() or |
| 1260 | * PyLong_Check(). Return -1 when there is an error. |
| 1261 | */ |
| 1262 | static long |
| 1263 | get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step) |
| 1264 | { |
| 1265 | /* ------------------------------------------------------------- |
| 1266 | Algorithm is equal to that of get_len_of_range(), but it operates |
| 1267 | on PyObjects (which are assumed to be PyLong or PyInt objects). |
| 1268 | ---------------------------------------------------------------*/ |
| 1269 | long n; |
| 1270 | PyObject *diff = NULL; |
| 1271 | PyObject *one = NULL; |
| 1272 | PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL; |
| 1273 | /* holds sub-expression evaluations */ |
| 1274 | |
| 1275 | /* if (lo >= hi), return length of 0. */ |
| 1276 | if (PyObject_Compare(lo, hi) >= 0) |
| 1277 | return 0; |
| 1278 | |
| 1279 | if ((one = PyLong_FromLong(1L)) == NULL) |
| 1280 | goto Fail; |
| 1281 | |
| 1282 | if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL) |
| 1283 | goto Fail; |
| 1284 | |
| 1285 | if ((diff = PyNumber_Subtract(tmp1, one)) == NULL) |
| 1286 | goto Fail; |
| 1287 | |
| 1288 | if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL) |
| 1289 | goto Fail; |
| 1290 | |
| 1291 | if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL) |
| 1292 | goto Fail; |
| 1293 | |
| 1294 | n = PyLong_AsLong(tmp3); |
| 1295 | if (PyErr_Occurred()) { /* Check for Overflow */ |
| 1296 | PyErr_Clear(); |
| 1297 | goto Fail; |
| 1298 | } |
| 1299 | |
| 1300 | Py_DECREF(tmp3); |
| 1301 | Py_DECREF(tmp2); |
| 1302 | Py_DECREF(diff); |
| 1303 | Py_DECREF(tmp1); |
| 1304 | Py_DECREF(one); |
| 1305 | return n; |
| 1306 | |
| 1307 | Fail: |
| 1308 | Py_XDECREF(tmp3); |
| 1309 | Py_XDECREF(tmp2); |
| 1310 | Py_XDECREF(diff); |
| 1311 | Py_XDECREF(tmp1); |
| 1312 | Py_XDECREF(one); |
| 1313 | return -1; |
| 1314 | } |
| 1315 | |
| 1316 | /* An extension of builtin_range() that handles the case when PyLong |
| 1317 | * arguments are given. */ |
| 1318 | static PyObject * |
| 1319 | handle_range_longs(PyObject *self, PyObject *args) |
| 1320 | { |
| 1321 | PyObject *ilow; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1322 | PyObject *ihigh = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1323 | PyObject *istep = NULL; |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1324 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1325 | PyObject *curnum = NULL; |
| 1326 | PyObject *v = NULL; |
| 1327 | long bign; |
| 1328 | int i, n; |
| 1329 | int cmp_result; |
| 1330 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1331 | PyObject *zero = PyLong_FromLong(0); |
| 1332 | |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1333 | if (zero == NULL) |
| 1334 | return NULL; |
| 1335 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1336 | if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) { |
| 1337 | Py_DECREF(zero); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1338 | return NULL; |
| 1339 | } |
| 1340 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1341 | /* Figure out which way we were called, supply defaults, and be |
| 1342 | * sure to incref everything so that the decrefs at the end |
| 1343 | * are correct. |
| 1344 | */ |
| 1345 | assert(ilow != NULL); |
| 1346 | if (ihigh == NULL) { |
| 1347 | /* only 1 arg -- it's the upper limit */ |
| 1348 | ihigh = ilow; |
| 1349 | ilow = NULL; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1350 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1351 | assert(ihigh != NULL); |
| 1352 | Py_INCREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1353 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1354 | /* ihigh correct now; do ilow */ |
| 1355 | if (ilow == NULL) |
| 1356 | ilow = zero; |
| 1357 | Py_INCREF(ilow); |
| 1358 | |
| 1359 | /* ilow and ihigh correct now; do istep */ |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1360 | if (istep == NULL) { |
| 1361 | istep = PyLong_FromLong(1L); |
| 1362 | if (istep == NULL) |
| 1363 | goto Fail; |
| 1364 | } |
| 1365 | else { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1366 | Py_INCREF(istep); |
| 1367 | } |
| 1368 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1369 | if (!PyInt_Check(ilow) && !PyLong_Check(ilow)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1370 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1371 | "range() integer start argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1372 | ilow->ob_type->tp_name); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1373 | goto Fail; |
| 1374 | } |
| 1375 | |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1376 | if (!PyInt_Check(ihigh) && !PyLong_Check(ihigh)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1377 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1378 | "range() integer end argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1379 | ihigh->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1380 | goto Fail; |
| 1381 | } |
| 1382 | |
| 1383 | if (!PyInt_Check(istep) && !PyLong_Check(istep)) { |
Guido van Rossum | 28e83e3 | 2003-04-15 12:43:26 +0000 | [diff] [blame] | 1384 | PyErr_Format(PyExc_TypeError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1385 | "range() integer step argument expected, got %s.", |
Guido van Rossum | 817d6c9 | 2003-04-14 18:25:04 +0000 | [diff] [blame] | 1386 | istep->ob_type->tp_name); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1387 | goto Fail; |
| 1388 | } |
| 1389 | |
| 1390 | if (PyObject_Cmp(istep, zero, &cmp_result) == -1) |
| 1391 | goto Fail; |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1392 | if (cmp_result == 0) { |
| 1393 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1394 | "range() step argument must not be zero"); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1395 | goto Fail; |
| 1396 | } |
| 1397 | |
| 1398 | if (cmp_result > 0) |
| 1399 | bign = get_len_of_range_longs(ilow, ihigh, istep); |
| 1400 | else { |
| 1401 | PyObject *neg_istep = PyNumber_Negative(istep); |
| 1402 | if (neg_istep == NULL) |
| 1403 | goto Fail; |
| 1404 | bign = get_len_of_range_longs(ihigh, ilow, neg_istep); |
| 1405 | Py_DECREF(neg_istep); |
| 1406 | } |
| 1407 | |
| 1408 | n = (int)bign; |
| 1409 | if (bign < 0 || (long)n != bign) { |
| 1410 | PyErr_SetString(PyExc_OverflowError, |
| 1411 | "range() result has too many items"); |
| 1412 | goto Fail; |
| 1413 | } |
| 1414 | |
| 1415 | v = PyList_New(n); |
| 1416 | if (v == NULL) |
| 1417 | goto Fail; |
| 1418 | |
| 1419 | curnum = ilow; |
| 1420 | Py_INCREF(curnum); |
| 1421 | |
| 1422 | for (i = 0; i < n; i++) { |
| 1423 | PyObject *w = PyNumber_Long(curnum); |
| 1424 | PyObject *tmp_num; |
| 1425 | if (w == NULL) |
| 1426 | goto Fail; |
| 1427 | |
| 1428 | PyList_SET_ITEM(v, i, w); |
| 1429 | |
| 1430 | tmp_num = PyNumber_Add(curnum, istep); |
| 1431 | if (tmp_num == NULL) |
| 1432 | goto Fail; |
| 1433 | |
| 1434 | Py_DECREF(curnum); |
| 1435 | curnum = tmp_num; |
| 1436 | } |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1437 | Py_DECREF(ilow); |
| 1438 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1439 | Py_DECREF(istep); |
| 1440 | Py_DECREF(zero); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1441 | Py_DECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1442 | return v; |
| 1443 | |
| 1444 | Fail: |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1445 | Py_DECREF(ilow); |
| 1446 | Py_DECREF(ihigh); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1447 | Py_XDECREF(istep); |
Tim Peters | 874e1f7 | 2003-04-13 22:13:08 +0000 | [diff] [blame] | 1448 | Py_DECREF(zero); |
| 1449 | Py_XDECREF(curnum); |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1450 | Py_XDECREF(v); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1454 | /* Return number of items in range/xrange (lo, hi, step). step > 0 |
| 1455 | * required. Return a value < 0 if & only if the true value is too |
| 1456 | * large to fit in a signed long. |
| 1457 | */ |
| 1458 | static long |
Thomas Wouters | e28c296 | 2000-07-23 22:21:32 +0000 | [diff] [blame] | 1459 | get_len_of_range(long lo, long hi, long step) |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1460 | { |
| 1461 | /* ------------------------------------------------------------- |
| 1462 | If lo >= hi, the range is empty. |
| 1463 | Else if n values are in the range, the last one is |
| 1464 | lo + (n-1)*step, which must be <= hi-1. Rearranging, |
| 1465 | n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives |
| 1466 | the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so |
| 1467 | the RHS is non-negative and so truncation is the same as the |
| 1468 | floor. Letting M be the largest positive long, the worst case |
| 1469 | for the RHS numerator is hi=M, lo=-M-1, and then |
| 1470 | hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough |
| 1471 | precision to compute the RHS exactly. |
| 1472 | ---------------------------------------------------------------*/ |
| 1473 | long n = 0; |
| 1474 | if (lo < hi) { |
| 1475 | unsigned long uhi = (unsigned long)hi; |
| 1476 | unsigned long ulo = (unsigned long)lo; |
| 1477 | unsigned long diff = uhi - ulo - 1; |
| 1478 | n = (long)(diff / (unsigned long)step + 1); |
| 1479 | } |
| 1480 | return n; |
| 1481 | } |
| 1482 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1483 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1484 | builtin_range(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1485 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1486 | long ilow = 0, ihigh = 0, istep = 1; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1487 | long bign; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1488 | int i, n; |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1489 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1490 | PyObject *v; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1491 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1492 | if (PyTuple_Size(args) <= 1) { |
| 1493 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1494 | "l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1495 | &ihigh)) { |
| 1496 | PyErr_Clear(); |
| 1497 | return handle_range_longs(self, args); |
| 1498 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1499 | } |
| 1500 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1501 | if (!PyArg_ParseTuple(args, |
Guido van Rossum | 0865dd9 | 1995-01-17 16:30:22 +0000 | [diff] [blame] | 1502 | "ll|l;range() requires 1-3 int arguments", |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1503 | &ilow, &ihigh, &istep)) { |
| 1504 | PyErr_Clear(); |
| 1505 | return handle_range_longs(self, args); |
| 1506 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1507 | } |
| 1508 | if (istep == 0) { |
Guido van Rossum | efbbb1c | 2003-04-11 18:43:06 +0000 | [diff] [blame] | 1509 | PyErr_SetString(PyExc_ValueError, |
Alex Martelli | f471d47 | 2003-04-23 13:00:44 +0000 | [diff] [blame] | 1510 | "range() step argument must not be zero"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1511 | return NULL; |
| 1512 | } |
Guido van Rossum | 124eff0 | 1999-02-23 16:11:01 +0000 | [diff] [blame] | 1513 | if (istep > 0) |
| 1514 | bign = get_len_of_range(ilow, ihigh, istep); |
| 1515 | else |
| 1516 | bign = get_len_of_range(ihigh, ilow, -istep); |
| 1517 | n = (int)bign; |
| 1518 | if (bign < 0 || (long)n != bign) { |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1519 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1520 | "range() result has too many items"); |
Guido van Rossum | e23cde2 | 1999-01-12 05:07:47 +0000 | [diff] [blame] | 1521 | return NULL; |
| 1522 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1523 | v = PyList_New(n); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1524 | if (v == NULL) |
| 1525 | return NULL; |
| 1526 | for (i = 0; i < n; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1527 | PyObject *w = PyInt_FromLong(ilow); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1528 | if (w == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1529 | Py_DECREF(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1530 | return NULL; |
| 1531 | } |
Guido van Rossum | a937d14 | 1998-04-24 18:22:02 +0000 | [diff] [blame] | 1532 | PyList_SET_ITEM(v, i, w); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1533 | ilow += istep; |
| 1534 | } |
| 1535 | return v; |
| 1536 | } |
| 1537 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1538 | PyDoc_STRVAR(range_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1539 | "range([start,] stop[, step]) -> list of integers\n\ |
| 1540 | \n\ |
| 1541 | Return a list containing an arithmetic progression of integers.\n\ |
| 1542 | range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\ |
| 1543 | When step is given, it specifies the increment (or decrement).\n\ |
| 1544 | For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1545 | These are exactly the valid indices for a list of 4 elements."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1546 | |
| 1547 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1548 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1549 | builtin_raw_input(PyObject *self, PyObject *args) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1550 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1551 | PyObject *v = NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1552 | PyObject *fin = PySys_GetObject("stdin"); |
| 1553 | PyObject *fout = PySys_GetObject("stdout"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1554 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1555 | if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v)) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1556 | return NULL; |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1557 | |
| 1558 | if (fin == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1559 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1560 | return NULL; |
| 1561 | } |
| 1562 | if (fout == NULL) { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1563 | PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout"); |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1564 | return NULL; |
| 1565 | } |
| 1566 | if (PyFile_SoftSpace(fout, 0)) { |
| 1567 | if (PyFile_WriteString(" ", fout) != 0) |
| 1568 | return NULL; |
| 1569 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1570 | if (PyFile_Check (fin) && PyFile_Check (fout) |
| 1571 | && isatty(fileno(PyFile_AsFile(fin))) |
| 1572 | && isatty(fileno(PyFile_AsFile(fout)))) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1573 | PyObject *po; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1574 | char *prompt; |
| 1575 | char *s; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1576 | PyObject *result; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1577 | if (v != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1578 | po = PyObject_Str(v); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1579 | if (po == NULL) |
| 1580 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1581 | prompt = PyString_AsString(po); |
Guido van Rossum | d9b5208 | 1998-06-26 18:25:38 +0000 | [diff] [blame] | 1582 | if (prompt == NULL) |
| 1583 | return NULL; |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1584 | } |
| 1585 | else { |
| 1586 | po = NULL; |
| 1587 | prompt = ""; |
| 1588 | } |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 1589 | s = PyOS_Readline(PyFile_AsFile (fin), PyFile_AsFile (fout), |
| 1590 | prompt); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1591 | Py_XDECREF(po); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1592 | if (s == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1593 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1594 | return NULL; |
| 1595 | } |
| 1596 | if (*s == '\0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1597 | PyErr_SetNone(PyExc_EOFError); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1598 | result = NULL; |
| 1599 | } |
| 1600 | else { /* strip trailing '\n' */ |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1601 | size_t len = strlen(s); |
| 1602 | if (len > INT_MAX) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1603 | PyErr_SetString(PyExc_OverflowError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1604 | "[raw_]input: input too long"); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1605 | result = NULL; |
| 1606 | } |
| 1607 | else { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1608 | result = PyString_FromStringAndSize(s, |
| 1609 | (int)(len-1)); |
Guido van Rossum | 106f2da | 2000-06-28 21:12:25 +0000 | [diff] [blame] | 1610 | } |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1611 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1612 | PyMem_FREE(s); |
Guido van Rossum | 872537c | 1995-07-07 22:43:42 +0000 | [diff] [blame] | 1613 | return result; |
| 1614 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1615 | if (v != NULL) { |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1616 | if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 1617 | return NULL; |
| 1618 | } |
Martin v. Löwis | 31d2df5 | 2002-08-14 15:46:02 +0000 | [diff] [blame] | 1619 | return PyFile_GetLine(fin, -1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1622 | PyDoc_STRVAR(raw_input_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1623 | "raw_input([prompt]) -> string\n\ |
| 1624 | \n\ |
| 1625 | Read a string from standard input. The trailing newline is stripped.\n\ |
| 1626 | If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\ |
| 1627 | On Unix, GNU readline is used if enabled. The prompt string, if given,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1628 | is printed without a trailing newline before reading."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1629 | |
| 1630 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1631 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1632 | builtin_reduce(PyObject *self, PyObject *args) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1633 | { |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1634 | PyObject *seq, *func, *result = NULL, *it; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1635 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1636 | if (!PyArg_UnpackTuple(args, "reduce", 2, 3, &func, &seq, &result)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1637 | return NULL; |
| 1638 | if (result != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1639 | Py_INCREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1640 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1641 | it = PyObject_GetIter(seq); |
| 1642 | if (it == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1643 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1644 | "reduce() arg 2 must support iteration"); |
| 1645 | Py_XDECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1646 | return NULL; |
| 1647 | } |
| 1648 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1649 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1650 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1651 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1652 | for (;;) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1653 | PyObject *op2; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1654 | |
| 1655 | if (args->ob_refcnt > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1656 | Py_DECREF(args); |
| 1657 | if ((args = PyTuple_New(2)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1658 | goto Fail; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1661 | op2 = PyIter_Next(it); |
| 1662 | if (op2 == NULL) { |
Tim Peters | f4848da | 2001-05-05 00:14:56 +0000 | [diff] [blame] | 1663 | if (PyErr_Occurred()) |
| 1664 | goto Fail; |
| 1665 | break; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1666 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1667 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1668 | if (result == NULL) |
| 1669 | result = op2; |
| 1670 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1671 | PyTuple_SetItem(args, 0, result); |
| 1672 | PyTuple_SetItem(args, 1, op2); |
| 1673 | if ((result = PyEval_CallObject(func, args)) == NULL) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1674 | goto Fail; |
| 1675 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1678 | Py_DECREF(args); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1679 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1680 | if (result == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1681 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1682 | "reduce() of empty sequence with no initial value"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1683 | |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1684 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1685 | return result; |
| 1686 | |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1687 | Fail: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1688 | Py_XDECREF(args); |
| 1689 | Py_XDECREF(result); |
Tim Peters | 15d81ef | 2001-05-04 04:39:21 +0000 | [diff] [blame] | 1690 | Py_DECREF(it); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 1691 | return NULL; |
| 1692 | } |
| 1693 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1694 | PyDoc_STRVAR(reduce_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1695 | "reduce(function, sequence[, initial]) -> value\n\ |
| 1696 | \n\ |
| 1697 | Apply a function of two arguments cumulatively to the items of a sequence,\n\ |
| 1698 | from left to right, so as to reduce the sequence to a single value.\n\ |
| 1699 | For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\ |
| 1700 | ((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\ |
| 1701 | of the sequence in the calculation, and serves as a default when the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1702 | sequence is empty."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1703 | |
| 1704 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1705 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1706 | builtin_reload(PyObject *self, PyObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1707 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1708 | return PyImport_ReloadModule(v); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1711 | PyDoc_STRVAR(reload_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1712 | "reload(module) -> module\n\ |
| 1713 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1714 | Reload the module. The module must have been successfully imported before."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1715 | |
| 1716 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1717 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1718 | builtin_repr(PyObject *self, PyObject *v) |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1719 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1720 | return PyObject_Repr(v); |
Guido van Rossum | c89705d | 1992-11-26 08:54:07 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1723 | PyDoc_STRVAR(repr_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1724 | "repr(object) -> string\n\ |
| 1725 | \n\ |
| 1726 | Return the canonical string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1727 | For most object types, eval(repr(object)) == object."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1728 | |
| 1729 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1730 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1731 | builtin_round(PyObject *self, PyObject *args) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1732 | { |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1733 | double x; |
| 1734 | double f; |
| 1735 | int ndigits = 0; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1736 | int i; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1737 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1738 | if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits)) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1739 | return NULL; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1740 | f = 1.0; |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1741 | i = abs(ndigits); |
| 1742 | while (--i >= 0) |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1743 | f = f*10.0; |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1744 | if (ndigits < 0) |
| 1745 | x /= f; |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1746 | else |
Guido van Rossum | 1e162d3 | 1998-05-09 14:42:25 +0000 | [diff] [blame] | 1747 | x *= f; |
| 1748 | if (x >= 0.0) |
| 1749 | x = floor(x + 0.5); |
| 1750 | else |
| 1751 | x = ceil(x - 0.5); |
| 1752 | if (ndigits < 0) |
| 1753 | x *= f; |
| 1754 | else |
| 1755 | x /= f; |
| 1756 | return PyFloat_FromDouble(x); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1759 | PyDoc_STRVAR(round_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1760 | "round(number[, ndigits]) -> floating point number\n\ |
| 1761 | \n\ |
| 1762 | Round a number to a given precision in decimal digits (default 0 digits).\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1763 | This always returns a floating point number. Precision may be negative."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1764 | |
| 1765 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1766 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1767 | builtin_vars(PyObject *self, PyObject *args) |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1768 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1769 | PyObject *v = NULL; |
| 1770 | PyObject *d; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1771 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1772 | if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1773 | return NULL; |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1774 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1775 | d = PyEval_GetLocals(); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1776 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1777 | if (!PyErr_Occurred()) |
| 1778 | PyErr_SetString(PyExc_SystemError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1779 | "vars(): no locals!?"); |
Guido van Rossum | 53bb7ff | 1995-07-26 16:26:31 +0000 | [diff] [blame] | 1780 | } |
| 1781 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1782 | Py_INCREF(d); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1783 | } |
| 1784 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1785 | d = PyObject_GetAttrString(v, "__dict__"); |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1786 | if (d == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1787 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2d95185 | 1994-08-29 12:52:16 +0000 | [diff] [blame] | 1788 | "vars() argument must have __dict__ attribute"); |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | } |
| 1792 | return d; |
| 1793 | } |
| 1794 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1795 | PyDoc_STRVAR(vars_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1796 | "vars([object]) -> dictionary\n\ |
| 1797 | \n\ |
| 1798 | Without arguments, equivalent to locals().\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1799 | With an argument, equivalent to object.__dict__."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1800 | |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1801 | |
| 1802 | static PyObject* |
| 1803 | builtin_sum(PyObject *self, PyObject *args) |
| 1804 | { |
| 1805 | PyObject *seq; |
| 1806 | PyObject *result = NULL; |
| 1807 | PyObject *temp, *item, *iter; |
| 1808 | |
| 1809 | if (!PyArg_ParseTuple(args, "O|O:sum", &seq, &result)) |
| 1810 | return NULL; |
| 1811 | |
| 1812 | iter = PyObject_GetIter(seq); |
| 1813 | if (iter == NULL) |
| 1814 | return NULL; |
| 1815 | |
| 1816 | if (result == NULL) { |
| 1817 | result = PyInt_FromLong(0); |
| 1818 | if (result == NULL) { |
| 1819 | Py_DECREF(iter); |
| 1820 | return NULL; |
| 1821 | } |
| 1822 | } else { |
| 1823 | /* reject string values for 'start' parameter */ |
| 1824 | if (PyObject_TypeCheck(result, &PyBaseString_Type)) { |
| 1825 | PyErr_SetString(PyExc_TypeError, |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 1826 | "sum() can't sum strings [use ''.join(seq) instead]"); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1827 | Py_DECREF(iter); |
| 1828 | return NULL; |
| 1829 | } |
Alex Martelli | 41c9f88 | 2003-04-22 09:24:48 +0000 | [diff] [blame] | 1830 | Py_INCREF(result); |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | for(;;) { |
| 1834 | item = PyIter_Next(iter); |
| 1835 | if (item == NULL) { |
| 1836 | /* error, or end-of-sequence */ |
| 1837 | if (PyErr_Occurred()) { |
| 1838 | Py_DECREF(result); |
| 1839 | result = NULL; |
| 1840 | } |
| 1841 | break; |
| 1842 | } |
| 1843 | temp = PyNumber_Add(result, item); |
| 1844 | Py_DECREF(result); |
| 1845 | Py_DECREF(item); |
| 1846 | result = temp; |
| 1847 | if (result == NULL) |
| 1848 | break; |
| 1849 | } |
| 1850 | Py_DECREF(iter); |
| 1851 | return result; |
| 1852 | } |
| 1853 | |
| 1854 | PyDoc_STRVAR(sum_doc, |
| 1855 | "sum(sequence, start=0) -> value\n\ |
| 1856 | \n\ |
| 1857 | Returns the sum of a sequence of numbers (NOT strings) plus the value\n\ |
| 1858 | of parameter 'start'. When the sequence is empty, returns start."); |
| 1859 | |
| 1860 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1861 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1862 | builtin_isinstance(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1863 | { |
| 1864 | PyObject *inst; |
| 1865 | PyObject *cls; |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1866 | int retval; |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1867 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1868 | if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1869 | return NULL; |
Guido van Rossum | f5dd914 | 1997-12-02 19:11:45 +0000 | [diff] [blame] | 1870 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1871 | retval = PyObject_IsInstance(inst, cls); |
| 1872 | if (retval < 0) |
Guido van Rossum | ad99177 | 2001-01-12 16:03:05 +0000 | [diff] [blame] | 1873 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1874 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1877 | PyDoc_STRVAR(isinstance_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1878 | "isinstance(object, class-or-type-or-tuple) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1879 | \n\ |
| 1880 | Return whether an object is an instance of a class or of a subclass thereof.\n\ |
Guido van Rossum | 03290ec | 2001-10-07 20:54:12 +0000 | [diff] [blame] | 1881 | With a type as second argument, return whether that is the object's type.\n\ |
| 1882 | The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1883 | isinstance(x, A) or isinstance(x, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1884 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1885 | |
| 1886 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1887 | builtin_issubclass(PyObject *self, PyObject *args) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1888 | { |
| 1889 | PyObject *derived; |
| 1890 | PyObject *cls; |
| 1891 | int retval; |
| 1892 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1893 | if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1894 | return NULL; |
Guido van Rossum | 668213d | 1999-06-16 17:28:37 +0000 | [diff] [blame] | 1895 | |
Guido van Rossum | 823649d | 2001-03-21 18:40:58 +0000 | [diff] [blame] | 1896 | retval = PyObject_IsSubclass(derived, cls); |
| 1897 | if (retval < 0) |
| 1898 | return NULL; |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1899 | return PyBool_FromLong(retval); |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1902 | PyDoc_STRVAR(issubclass_doc, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1903 | "issubclass(C, B) -> bool\n\ |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1904 | \n\ |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 1905 | Return whether class C is a subclass (i.e., a derived class) of class B.\n\ |
| 1906 | When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\ |
| 1907 | is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.)."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 1908 | |
Barry Warsaw | cde8b1b | 1997-08-22 21:14:38 +0000 | [diff] [blame] | 1909 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1910 | static PyObject* |
| 1911 | builtin_zip(PyObject *self, PyObject *args) |
| 1912 | { |
| 1913 | PyObject *ret; |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1914 | const int itemsize = PySequence_Length(args); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1915 | int i; |
| 1916 | PyObject *itlist; /* tuple of iterators */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1917 | int len; /* guess at result length */ |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1918 | |
Raymond Hettinger | eaef615 | 2003-08-02 07:42:57 +0000 | [diff] [blame] | 1919 | if (itemsize == 0) |
| 1920 | return PyList_New(0); |
| 1921 | |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1922 | /* args must be a tuple */ |
| 1923 | assert(PyTuple_Check(args)); |
| 1924 | |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1925 | /* Guess at result length: the shortest of the input lengths. |
| 1926 | If some argument refuses to say, we refuse to guess too, lest |
| 1927 | an argument like xrange(sys.maxint) lead us astray.*/ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1928 | len = -1; /* unknown */ |
| 1929 | for (i = 0; i < itemsize; ++i) { |
| 1930 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1931 | int thislen = PySequence_Length(item); |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1932 | if (thislen < 0) { |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1933 | PyErr_Clear(); |
Tim Peters | 39a86c2 | 2002-05-12 07:19:38 +0000 | [diff] [blame] | 1934 | len = -1; |
| 1935 | break; |
| 1936 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1937 | else if (len < 0 || thislen < len) |
| 1938 | len = thislen; |
| 1939 | } |
| 1940 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1941 | /* allocate result list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1942 | if (len < 0) |
| 1943 | len = 10; /* arbitrary */ |
| 1944 | if ((ret = PyList_New(len)) == NULL) |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1945 | return NULL; |
| 1946 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1947 | /* obtain iterators */ |
| 1948 | itlist = PyTuple_New(itemsize); |
| 1949 | if (itlist == NULL) |
| 1950 | goto Fail_ret; |
| 1951 | for (i = 0; i < itemsize; ++i) { |
| 1952 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 1953 | PyObject *it = PyObject_GetIter(item); |
| 1954 | if (it == NULL) { |
| 1955 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1956 | PyErr_Format(PyExc_TypeError, |
| 1957 | "zip argument #%d must support iteration", |
| 1958 | i+1); |
| 1959 | goto Fail_ret_itlist; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1960 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1961 | PyTuple_SET_ITEM(itlist, i, it); |
| 1962 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1963 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1964 | /* build result into ret list */ |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1965 | for (i = 0; ; ++i) { |
| 1966 | int j; |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1967 | PyObject *next = PyTuple_New(itemsize); |
| 1968 | if (!next) |
| 1969 | goto Fail_ret_itlist; |
| 1970 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1971 | for (j = 0; j < itemsize; j++) { |
| 1972 | PyObject *it = PyTuple_GET_ITEM(itlist, j); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1973 | PyObject *item = PyIter_Next(it); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1974 | if (!item) { |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1975 | if (PyErr_Occurred()) { |
| 1976 | Py_DECREF(ret); |
| 1977 | ret = NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1978 | } |
| 1979 | Py_DECREF(next); |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1980 | Py_DECREF(itlist); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1981 | goto Done; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1982 | } |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1983 | PyTuple_SET_ITEM(next, j, item); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1984 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1985 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1986 | if (i < len) |
| 1987 | PyList_SET_ITEM(ret, i, next); |
| 1988 | else { |
| 1989 | int status = PyList_Append(ret, next); |
| 1990 | Py_DECREF(next); |
| 1991 | ++len; |
| 1992 | if (status < 0) |
| 1993 | goto Fail_ret_itlist; |
| 1994 | } |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 1995 | } |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 1996 | |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 1997 | Done: |
| 1998 | if (ret != NULL && i < len) { |
| 1999 | /* The list is too big. */ |
| 2000 | if (PyList_SetSlice(ret, i, len, NULL) < 0) |
| 2001 | return NULL; |
| 2002 | } |
| 2003 | return ret; |
| 2004 | |
Tim Peters | 8572b4f | 2001-05-06 01:05:02 +0000 | [diff] [blame] | 2005 | Fail_ret_itlist: |
| 2006 | Py_DECREF(itlist); |
| 2007 | Fail_ret: |
| 2008 | Py_DECREF(ret); |
| 2009 | return NULL; |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2013 | PyDoc_STRVAR(zip_doc, |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2014 | "zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\ |
| 2015 | \n\ |
| 2016 | Return a list of tuples, where each tuple contains the i-th element\n\ |
| 2017 | from each of the argument sequences. The returned list is truncated\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2018 | in length to the length of the shortest argument sequence."); |
Barry Warsaw | bd599b5 | 2000-08-03 15:45:29 +0000 | [diff] [blame] | 2019 | |
| 2020 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2021 | static PyMethodDef builtin_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2022 | {"__import__", builtin___import__, METH_VARARGS, import_doc}, |
| 2023 | {"abs", builtin_abs, METH_O, abs_doc}, |
| 2024 | {"apply", builtin_apply, METH_VARARGS, apply_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2025 | {"callable", builtin_callable, METH_O, callable_doc}, |
| 2026 | {"chr", builtin_chr, METH_VARARGS, chr_doc}, |
| 2027 | {"cmp", builtin_cmp, METH_VARARGS, cmp_doc}, |
| 2028 | {"coerce", builtin_coerce, METH_VARARGS, coerce_doc}, |
| 2029 | {"compile", builtin_compile, METH_VARARGS, compile_doc}, |
| 2030 | {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, |
| 2031 | {"dir", builtin_dir, METH_VARARGS, dir_doc}, |
| 2032 | {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, |
| 2033 | {"eval", builtin_eval, METH_VARARGS, eval_doc}, |
| 2034 | {"execfile", builtin_execfile, METH_VARARGS, execfile_doc}, |
| 2035 | {"filter", builtin_filter, METH_VARARGS, filter_doc}, |
| 2036 | {"getattr", builtin_getattr, METH_VARARGS, getattr_doc}, |
| 2037 | {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc}, |
| 2038 | {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc}, |
| 2039 | {"hash", builtin_hash, METH_O, hash_doc}, |
| 2040 | {"hex", builtin_hex, METH_O, hex_doc}, |
| 2041 | {"id", builtin_id, METH_O, id_doc}, |
| 2042 | {"input", builtin_input, METH_VARARGS, input_doc}, |
| 2043 | {"intern", builtin_intern, METH_VARARGS, intern_doc}, |
| 2044 | {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, |
| 2045 | {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc}, |
| 2046 | {"iter", builtin_iter, METH_VARARGS, iter_doc}, |
| 2047 | {"len", builtin_len, METH_O, len_doc}, |
| 2048 | {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, |
| 2049 | {"map", builtin_map, METH_VARARGS, map_doc}, |
| 2050 | {"max", builtin_max, METH_VARARGS, max_doc}, |
| 2051 | {"min", builtin_min, METH_VARARGS, min_doc}, |
| 2052 | {"oct", builtin_oct, METH_O, oct_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2053 | {"ord", builtin_ord, METH_O, ord_doc}, |
| 2054 | {"pow", builtin_pow, METH_VARARGS, pow_doc}, |
| 2055 | {"range", builtin_range, METH_VARARGS, range_doc}, |
| 2056 | {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc}, |
| 2057 | {"reduce", builtin_reduce, METH_VARARGS, reduce_doc}, |
| 2058 | {"reload", builtin_reload, METH_O, reload_doc}, |
| 2059 | {"repr", builtin_repr, METH_O, repr_doc}, |
| 2060 | {"round", builtin_round, METH_VARARGS, round_doc}, |
| 2061 | {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, |
Alex Martelli | a70b191 | 2003-04-22 08:12:33 +0000 | [diff] [blame] | 2062 | {"sum", builtin_sum, METH_VARARGS, sum_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2063 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2064 | {"unichr", builtin_unichr, METH_VARARGS, unichr_doc}, |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2065 | #endif |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2066 | {"vars", builtin_vars, METH_VARARGS, vars_doc}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2067 | {"zip", builtin_zip, METH_VARARGS, zip_doc}, |
Guido van Rossum | c02e15c | 1991-12-16 13:03:00 +0000 | [diff] [blame] | 2068 | {NULL, NULL}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2069 | }; |
| 2070 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2071 | PyDoc_STRVAR(builtin_doc, |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2072 | "Built-in functions, exceptions, and other objects.\n\ |
| 2073 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices."); |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2075 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2076 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2077 | _PyBuiltin_Init(void) |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2078 | { |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2079 | PyObject *mod, *dict, *debug; |
Guido van Rossum | f9d9c6c | 1998-06-26 21:23:49 +0000 | [diff] [blame] | 2080 | mod = Py_InitModule4("__builtin__", builtin_methods, |
| 2081 | builtin_doc, (PyObject *)NULL, |
| 2082 | PYTHON_API_VERSION); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2083 | if (mod == NULL) |
| 2084 | return NULL; |
| 2085 | dict = PyModule_GetDict(mod); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2086 | |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2087 | #ifdef Py_TRACE_REFS |
| 2088 | /* __builtin__ exposes a number of statically allocated objects |
| 2089 | * that, before this code was added in 2.3, never showed up in |
| 2090 | * the list of "all objects" maintained by Py_TRACE_REFS. As a |
| 2091 | * result, programs leaking references to None and False (etc) |
| 2092 | * couldn't be diagnosed by examining sys.getobjects(0). |
| 2093 | */ |
| 2094 | #define ADD_TO_ALL(OBJECT) _Py_AddToAllObjects((PyObject *)(OBJECT), 0) |
| 2095 | #else |
| 2096 | #define ADD_TO_ALL(OBJECT) (void)0 |
| 2097 | #endif |
| 2098 | |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2099 | #define SETBUILTIN(NAME, OBJECT) \ |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2100 | if (PyDict_SetItemString(dict, NAME, (PyObject *)OBJECT) < 0) \ |
| 2101 | return NULL; \ |
| 2102 | ADD_TO_ALL(OBJECT) |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2103 | |
| 2104 | SETBUILTIN("None", Py_None); |
| 2105 | SETBUILTIN("Ellipsis", Py_Ellipsis); |
| 2106 | SETBUILTIN("NotImplemented", Py_NotImplemented); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2107 | SETBUILTIN("False", Py_False); |
| 2108 | SETBUILTIN("True", Py_True); |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 2109 | SETBUILTIN("basestring", &PyBaseString_Type); |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2110 | SETBUILTIN("bool", &PyBool_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2111 | SETBUILTIN("buffer", &PyBuffer_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2112 | SETBUILTIN("classmethod", &PyClassMethod_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2113 | #ifndef WITHOUT_COMPLEX |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2114 | SETBUILTIN("complex", &PyComplex_Type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2115 | #endif |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2116 | SETBUILTIN("dict", &PyDict_Type); |
Tim Peters | 67d687a | 2002-04-29 21:27:32 +0000 | [diff] [blame] | 2117 | SETBUILTIN("enumerate", &PyEnum_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2118 | SETBUILTIN("float", &PyFloat_Type); |
| 2119 | SETBUILTIN("property", &PyProperty_Type); |
| 2120 | SETBUILTIN("int", &PyInt_Type); |
| 2121 | SETBUILTIN("list", &PyList_Type); |
| 2122 | SETBUILTIN("long", &PyLong_Type); |
| 2123 | SETBUILTIN("object", &PyBaseObject_Type); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2124 | SETBUILTIN("slice", &PySlice_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2125 | SETBUILTIN("staticmethod", &PyStaticMethod_Type); |
| 2126 | SETBUILTIN("str", &PyString_Type); |
| 2127 | SETBUILTIN("super", &PySuper_Type); |
| 2128 | SETBUILTIN("tuple", &PyTuple_Type); |
| 2129 | SETBUILTIN("type", &PyType_Type); |
Raymond Hettinger | c4c453f | 2002-06-05 23:12:45 +0000 | [diff] [blame] | 2130 | SETBUILTIN("xrange", &PyRange_Type); |
Tim Peters | 742dfd6 | 2001-09-13 21:49:44 +0000 | [diff] [blame] | 2131 | |
| 2132 | /* Note that open() is just an alias of file(). */ |
| 2133 | SETBUILTIN("open", &PyFile_Type); |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2134 | SETBUILTIN("file", &PyFile_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2135 | #ifdef Py_USING_UNICODE |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2136 | SETBUILTIN("unicode", &PyUnicode_Type); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2137 | #endif |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2138 | debug = PyBool_FromLong(Py_OptimizeFlag == 0); |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2139 | if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { |
| 2140 | Py_XDECREF(debug); |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2141 | return NULL; |
Fred Drake | 5550de3 | 2000-06-20 04:54:19 +0000 | [diff] [blame] | 2142 | } |
| 2143 | Py_XDECREF(debug); |
Barry Warsaw | 757af0e | 1997-08-29 22:13:51 +0000 | [diff] [blame] | 2144 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 2145 | return mod; |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 2146 | #undef ADD_TO_ALL |
Tim Peters | 4b7625e | 2001-09-13 21:37:17 +0000 | [diff] [blame] | 2147 | #undef SETBUILTIN |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2150 | /* Helper for filter(): filter a tuple through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2151 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2152 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2153 | filtertuple(PyObject *func, PyObject *tuple) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2154 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2155 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2156 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2157 | int len = PyTuple_Size(tuple); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2158 | |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2159 | if (len == 0) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2160 | if (PyTuple_CheckExact(tuple)) |
| 2161 | Py_INCREF(tuple); |
| 2162 | else |
| 2163 | tuple = PyTuple_New(0); |
Guido van Rossum | b7b4562 | 1995-08-04 04:07:45 +0000 | [diff] [blame] | 2164 | return tuple; |
| 2165 | } |
| 2166 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2167 | if ((result = PyTuple_New(len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2168 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2169 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2170 | for (i = j = 0; i < len; ++i) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2171 | PyObject *item, *good; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2172 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2173 | |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2174 | if (tuple->ob_type->tp_as_sequence && |
| 2175 | tuple->ob_type->tp_as_sequence->sq_item) { |
| 2176 | item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2177 | if (item == NULL) |
| 2178 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2179 | } else { |
Alex Martelli | a9b9c9f | 2003-04-23 13:34:35 +0000 | [diff] [blame] | 2180 | PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple"); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2181 | goto Fail_1; |
Walter Dörwald | 8dd1932 | 2003-02-10 17:36:40 +0000 | [diff] [blame] | 2182 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2183 | if (func == Py_None) { |
| 2184 | Py_INCREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2185 | good = item; |
| 2186 | } |
| 2187 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2188 | PyObject *arg = Py_BuildValue("(O)", item); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2189 | if (arg == NULL) { |
| 2190 | Py_DECREF(item); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2191 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2192 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2193 | good = PyEval_CallObject(func, arg); |
| 2194 | Py_DECREF(arg); |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2195 | if (good == NULL) { |
| 2196 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2197 | goto Fail_1; |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2198 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2199 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2200 | ok = PyObject_IsTrue(good); |
| 2201 | Py_DECREF(good); |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2202 | if (ok) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2203 | if (PyTuple_SetItem(result, j++, item) < 0) |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2204 | goto Fail_1; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2205 | } |
Walter Dörwald | c58a3a1 | 2003-08-18 18:28:45 +0000 | [diff] [blame] | 2206 | else |
| 2207 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Tim Peters | 4324aa3 | 2001-05-28 22:30:08 +0000 | [diff] [blame] | 2210 | if (_PyTuple_Resize(&result, j) < 0) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2211 | return NULL; |
| 2212 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2213 | return result; |
| 2214 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2215 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2216 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2217 | return NULL; |
| 2218 | } |
| 2219 | |
| 2220 | |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 2221 | /* Helper for filter(): filter a string through a function */ |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2222 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2223 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2224 | filterstring(PyObject *func, PyObject *strobj) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2225 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2226 | PyObject *result; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2227 | register int i, j; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2228 | int len = PyString_Size(strobj); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2229 | int outlen = len; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2230 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2231 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2232 | /* If it's a real string we can return the original, |
| 2233 | * as no character is ever false and __getitem__ |
| 2234 | * does return this character. If it's a subclass |
| 2235 | * we must go through the __getitem__ loop */ |
| 2236 | if (PyString_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2237 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2238 | return strobj; |
| 2239 | } |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2240 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2241 | if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) |
Guido van Rossum | 2586bf0 | 1993-11-01 16:21:44 +0000 | [diff] [blame] | 2242 | return NULL; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2243 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2244 | for (i = j = 0; i < len; ++i) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2245 | PyObject *item; |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2246 | int ok; |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2247 | |
Guido van Rossum | dc4b93d | 1993-10-27 14:56:44 +0000 | [diff] [blame] | 2248 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2249 | if (item == NULL) |
| 2250 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2251 | if (func==Py_None) { |
| 2252 | ok = 1; |
| 2253 | } else { |
| 2254 | PyObject *arg, *good; |
| 2255 | arg = Py_BuildValue("(O)", item); |
| 2256 | if (arg == NULL) { |
| 2257 | Py_DECREF(item); |
| 2258 | goto Fail_1; |
| 2259 | } |
| 2260 | good = PyEval_CallObject(func, arg); |
| 2261 | Py_DECREF(arg); |
| 2262 | if (good == NULL) { |
| 2263 | Py_DECREF(item); |
| 2264 | goto Fail_1; |
| 2265 | } |
| 2266 | ok = PyObject_IsTrue(good); |
| 2267 | Py_DECREF(good); |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2268 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2269 | if (ok) { |
| 2270 | int reslen; |
| 2271 | if (!PyString_Check(item)) { |
| 2272 | PyErr_SetString(PyExc_TypeError, "can't filter str to str:" |
| 2273 | " __getitem__ returned different type"); |
| 2274 | Py_DECREF(item); |
| 2275 | goto Fail_1; |
| 2276 | } |
| 2277 | reslen = PyString_GET_SIZE(item); |
| 2278 | if (reslen == 1) { |
| 2279 | PyString_AS_STRING(result)[j++] = |
| 2280 | PyString_AS_STRING(item)[0]; |
| 2281 | } else { |
| 2282 | /* do we need more space? */ |
| 2283 | int need = j + reslen + len-i-1; |
| 2284 | if (need > outlen) { |
| 2285 | /* overallocate, to avoid reallocations */ |
| 2286 | if (need<2*outlen) |
| 2287 | need = 2*outlen; |
| 2288 | if (_PyString_Resize(&result, need)) { |
| 2289 | Py_DECREF(item); |
| 2290 | return NULL; |
| 2291 | } |
| 2292 | outlen = need; |
| 2293 | } |
| 2294 | memcpy( |
| 2295 | PyString_AS_STRING(result) + j, |
| 2296 | PyString_AS_STRING(item), |
| 2297 | reslen |
| 2298 | ); |
| 2299 | j += reslen; |
| 2300 | } |
| 2301 | } |
Tim Peters | 388ed08 | 2001-04-07 20:34:48 +0000 | [diff] [blame] | 2302 | Py_DECREF(item); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2305 | if (j < outlen) |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2306 | _PyString_Resize(&result, j); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2308 | return result; |
| 2309 | |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2310 | Fail_1: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2311 | Py_DECREF(result); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 2312 | return NULL; |
| 2313 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2314 | |
| 2315 | #ifdef Py_USING_UNICODE |
| 2316 | /* Helper for filter(): filter a Unicode object through a function */ |
| 2317 | |
| 2318 | static PyObject * |
| 2319 | filterunicode(PyObject *func, PyObject *strobj) |
| 2320 | { |
| 2321 | PyObject *result; |
| 2322 | register int i, j; |
| 2323 | int len = PyUnicode_GetSize(strobj); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2324 | int outlen = len; |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2325 | |
| 2326 | if (func == Py_None) { |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2327 | /* If it's a real string we can return the original, |
| 2328 | * as no character is ever false and __getitem__ |
| 2329 | * does return this character. If it's a subclass |
| 2330 | * we must go through the __getitem__ loop */ |
| 2331 | if (PyUnicode_CheckExact(strobj)) { |
Walter Dörwald | c3da83f | 2003-02-04 20:24:45 +0000 | [diff] [blame] | 2332 | Py_INCREF(strobj); |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2333 | return strobj; |
| 2334 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2335 | } |
| 2336 | if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL) |
| 2337 | return NULL; |
| 2338 | |
| 2339 | for (i = j = 0; i < len; ++i) { |
| 2340 | PyObject *item, *arg, *good; |
| 2341 | int ok; |
| 2342 | |
| 2343 | item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); |
| 2344 | if (item == NULL) |
| 2345 | goto Fail_1; |
Walter Dörwald | 1918f77 | 2003-02-10 13:19:13 +0000 | [diff] [blame] | 2346 | if (func == Py_None) { |
| 2347 | ok = 1; |
| 2348 | } else { |
| 2349 | arg = Py_BuildValue("(O)", item); |
| 2350 | if (arg == NULL) { |
| 2351 | Py_DECREF(item); |
| 2352 | goto Fail_1; |
| 2353 | } |
| 2354 | good = PyEval_CallObject(func, arg); |
| 2355 | Py_DECREF(arg); |
| 2356 | if (good == NULL) { |
| 2357 | Py_DECREF(item); |
| 2358 | goto Fail_1; |
| 2359 | } |
| 2360 | ok = PyObject_IsTrue(good); |
| 2361 | Py_DECREF(good); |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2362 | } |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2363 | if (ok) { |
| 2364 | int reslen; |
| 2365 | if (!PyUnicode_Check(item)) { |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2366 | PyErr_SetString(PyExc_TypeError, |
| 2367 | "can't filter unicode to unicode:" |
| 2368 | " __getitem__ returned different type"); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2369 | Py_DECREF(item); |
| 2370 | goto Fail_1; |
| 2371 | } |
| 2372 | reslen = PyUnicode_GET_SIZE(item); |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2373 | if (reslen == 1) |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2374 | PyUnicode_AS_UNICODE(result)[j++] = |
| 2375 | PyUnicode_AS_UNICODE(item)[0]; |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2376 | else { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2377 | /* do we need more space? */ |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2378 | int need = j + reslen + len - i - 1; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2379 | if (need > outlen) { |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2380 | /* overallocate, |
| 2381 | to avoid reallocations */ |
| 2382 | if (need < 2 * outlen) |
| 2383 | need = 2 * outlen; |
Jeremy Hylton | 364f6be | 2003-09-16 03:17:16 +0000 | [diff] [blame] | 2384 | if (PyUnicode_Resize( |
| 2385 | &result, need) < 0) { |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2386 | Py_DECREF(item); |
Walter Dörwald | 531e000 | 2003-02-04 16:57:49 +0000 | [diff] [blame] | 2387 | goto Fail_1; |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2388 | } |
| 2389 | outlen = need; |
| 2390 | } |
Jeremy Hylton | 1aad9c7 | 2003-09-16 03:10:59 +0000 | [diff] [blame] | 2391 | memcpy(PyUnicode_AS_UNICODE(result) + j, |
| 2392 | PyUnicode_AS_UNICODE(item), |
| 2393 | reslen*sizeof(Py_UNICODE)); |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2394 | j += reslen; |
| 2395 | } |
| 2396 | } |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2397 | Py_DECREF(item); |
| 2398 | } |
| 2399 | |
Walter Dörwald | 903f1e0 | 2003-02-04 16:28:00 +0000 | [diff] [blame] | 2400 | if (j < outlen) |
Martin v. Löwis | 8afd757 | 2003-01-25 22:46:11 +0000 | [diff] [blame] | 2401 | PyUnicode_Resize(&result, j); |
| 2402 | |
| 2403 | return result; |
| 2404 | |
| 2405 | Fail_1: |
| 2406 | Py_DECREF(result); |
| 2407 | return NULL; |
| 2408 | } |
| 2409 | #endif |