summaryrefslogtreecommitdiffstats
path: root/Python/bltinmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r--Python/bltinmodule.c3813
1 files changed, 2031 insertions, 1782 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 3426768..6d47de1 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1,287 +1,57 @@
/* Built-in functions */
#include "Python.h"
-#include <ctype.h>
-#include "ast.h"
-#undef Yield /* undefine macro conflicting with <winbase.h> */
-#include "pycore_pyerrors.h"
-#include "pycore_pystate.h"
-#include "pycore_tupleobject.h"
-
-_Py_IDENTIFIER(__builtins__);
-_Py_IDENTIFIER(__dict__);
-_Py_IDENTIFIER(__prepare__);
-_Py_IDENTIFIER(__round__);
-_Py_IDENTIFIER(__mro_entries__);
-_Py_IDENTIFIER(encoding);
-_Py_IDENTIFIER(errors);
-_Py_IDENTIFIER(fileno);
-_Py_IDENTIFIER(flush);
-_Py_IDENTIFIER(metaclass);
-_Py_IDENTIFIER(sort);
-_Py_IDENTIFIER(stdin);
-_Py_IDENTIFIER(stdout);
-_Py_IDENTIFIER(stderr);
-
-#include "clinic/bltinmodule.c.h"
-
-static PyObject*
-update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
-{
- Py_ssize_t i, j;
- PyObject *base, *meth, *new_base, *result, *new_bases = NULL;
- assert(PyTuple_Check(bases));
-
- for (i = 0; i < nargs; i++) {
- base = args[i];
- if (PyType_Check(base)) {
- if (new_bases) {
- /* If we already have made a replacement, then we append every normal base,
- otherwise just skip it. */
- if (PyList_Append(new_bases, base) < 0) {
- goto error;
- }
- }
- continue;
- }
- if (_PyObject_LookupAttrId(base, &PyId___mro_entries__, &meth) < 0) {
- goto error;
- }
- if (!meth) {
- if (new_bases) {
- if (PyList_Append(new_bases, base) < 0) {
- goto error;
- }
- }
- continue;
- }
- new_base = _PyObject_CallOneArg(meth, bases);
- Py_DECREF(meth);
- if (!new_base) {
- goto error;
- }
- if (!PyTuple_Check(new_base)) {
- PyErr_SetString(PyExc_TypeError,
- "__mro_entries__ must return a tuple");
- Py_DECREF(new_base);
- goto error;
- }
- if (!new_bases) {
- /* If this is a first successful replacement, create new_bases list and
- copy previously encountered bases. */
- if (!(new_bases = PyList_New(i))) {
- goto error;
- }
- for (j = 0; j < i; j++) {
- base = args[j];
- PyList_SET_ITEM(new_bases, j, base);
- Py_INCREF(base);
- }
- }
- j = PyList_GET_SIZE(new_bases);
- if (PyList_SetSlice(new_bases, j, j, new_base) < 0) {
- goto error;
- }
- Py_DECREF(new_base);
- }
- if (!new_bases) {
- return bases;
- }
- result = PyList_AsTuple(new_bases);
- Py_DECREF(new_bases);
- return result;
+#include "Python-ast.h"
-error:
- Py_XDECREF(new_bases);
- return NULL;
-}
+#include "node.h"
+#include "code.h"
+#include "eval.h"
-/* AC: cannot convert yet, waiting for *args support */
-static PyObject *
-builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
- PyObject *kwnames)
-{
- PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *orig_bases;
- PyObject *cls = NULL, *cell = NULL;
- int isclass = 0; /* initialize to prevent gcc warning */
-
- if (nargs < 2) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: not enough arguments");
- return NULL;
- }
- func = args[0]; /* Better be callable */
- if (!PyFunction_Check(func)) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: func must be a function");
- return NULL;
- }
- name = args[1];
- if (!PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "__build_class__: name is not a string");
- return NULL;
- }
- orig_bases = _PyTuple_FromArray(args + 2, nargs - 2);
- if (orig_bases == NULL)
- return NULL;
-
- bases = update_bases(orig_bases, args + 2, nargs - 2);
- if (bases == NULL) {
- Py_DECREF(orig_bases);
- return NULL;
- }
+#include <ctype.h>
+#include <float.h> /* for DBL_MANT_DIG and friends */
- if (kwnames == NULL) {
- meta = NULL;
- mkw = NULL;
- }
- else {
- mkw = _PyStack_AsDict(args + nargs, kwnames);
- if (mkw == NULL) {
- Py_DECREF(bases);
- return NULL;
- }
+#ifdef RISCOS
+#include "unixstuff.h"
+#endif
- meta = _PyDict_GetItemIdWithError(mkw, &PyId_metaclass);
- if (meta != NULL) {
- Py_INCREF(meta);
- if (_PyDict_DelItemId(mkw, &PyId_metaclass) < 0) {
- Py_DECREF(meta);
- Py_DECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- /* metaclass is explicitly given, check if it's indeed a class */
- isclass = PyType_Check(meta);
- }
- else if (PyErr_Occurred()) {
- Py_DECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- }
- if (meta == NULL) {
- /* if there are no bases, use type: */
- if (PyTuple_GET_SIZE(bases) == 0) {
- meta = (PyObject *) (&PyType_Type);
- }
- /* else get the type of the first base */
- else {
- PyObject *base0 = PyTuple_GET_ITEM(bases, 0);
- meta = (PyObject *) (base0->ob_type);
- }
- Py_INCREF(meta);
- isclass = 1; /* meta is really a class */
- }
-
- if (isclass) {
- /* meta is really a class, so check for a more derived
- metaclass, or possible metaclass conflicts: */
- winner = (PyObject *)_PyType_CalculateMetaclass((PyTypeObject *)meta,
- bases);
- if (winner == NULL) {
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- if (winner != meta) {
- Py_DECREF(meta);
- meta = winner;
- Py_INCREF(meta);
- }
- }
- /* else: meta is not a class, so we cannot do the metaclass
- calculation, so we will use the explicitly given object as it is */
- if (_PyObject_LookupAttrId(meta, &PyId___prepare__, &prep) < 0) {
- ns = NULL;
- }
- else if (prep == NULL) {
- ns = PyDict_New();
- }
- else {
- PyObject *pargs[2] = {name, bases};
- ns = _PyObject_FastCallDict(prep, pargs, 2, mkw);
- Py_DECREF(prep);
- }
- if (ns == NULL) {
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
- if (!PyMapping_Check(ns)) {
- PyErr_Format(PyExc_TypeError,
- "%.200s.__prepare__() must return a mapping, not %.200s",
- isclass ? ((PyTypeObject *)meta)->tp_name : "<metaclass>",
- Py_TYPE(ns)->tp_name);
- goto error;
- }
- cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns,
- NULL, 0, NULL, 0, NULL, 0, NULL,
- PyFunction_GET_CLOSURE(func));
- if (cell != NULL) {
- if (bases != orig_bases) {
- if (PyMapping_SetItemString(ns, "__orig_bases__", orig_bases) < 0) {
- goto error;
- }
- }
- PyObject *margs[3] = {name, bases, ns};
- cls = _PyObject_FastCallDict(meta, margs, 3, mkw);
- if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
- PyObject *cell_cls = PyCell_GET(cell);
- if (cell_cls != cls) {
- if (cell_cls == NULL) {
- const char *msg =
- "__class__ not set defining %.200R as %.200R. "
- "Was __classcell__ propagated to type.__new__?";
- PyErr_Format(PyExc_RuntimeError, msg, name, cls);
- } else {
- const char *msg =
- "__class__ set to %.200R defining %.200R as %.200R";
- PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
- }
- Py_DECREF(cls);
- cls = NULL;
- goto error;
- }
- }
- }
-error:
- Py_XDECREF(cell);
- Py_DECREF(ns);
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- if (bases != orig_bases) {
- Py_DECREF(orig_bases);
- }
- return cls;
-}
+/* The default encoding used by the platform file system APIs
+ Can remain NULL for all platforms that don't have such a concept
+*/
+#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
+const char *Py_FileSystemDefaultEncoding = "mbcs";
+#elif defined(__APPLE__)
+const char *Py_FileSystemDefaultEncoding = "utf-8";
+#else
+const char *Py_FileSystemDefaultEncoding = NULL; /* use default */
+#endif
-PyDoc_STRVAR(build_class_doc,
-"__build_class__(func, name, /, *bases, [metaclass], **kwds) -> class\n\
-\n\
-Internal helper function used by the class statement.");
+/* Forward */
+static PyObject *filterstring(PyObject *, PyObject *);
+#ifdef Py_USING_UNICODE
+static PyObject *filterunicode(PyObject *, PyObject *);
+#endif
+static PyObject *filtertuple (PyObject *, PyObject *);
static PyObject *
builtin___import__(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"name", "globals", "locals", "fromlist",
"level", 0};
- PyObject *name, *globals = NULL, *locals = NULL, *fromlist = NULL;
- int level = 0;
+ char *name;
+ PyObject *globals = NULL;
+ PyObject *locals = NULL;
+ PyObject *fromlist = NULL;
+ int level = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|OOOi:__import__",
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|OOOi:__import__",
kwlist, &name, &globals, &locals, &fromlist, &level))
return NULL;
- return PyImport_ImportModuleLevelObject(name, globals, locals,
- fromlist, level);
+ return PyImport_ImportModuleLevel(name, globals, locals,
+ fromlist, level);
}
PyDoc_STRVAR(import_doc,
-"__import__(name, globals=None, locals=None, fromlist=(), level=0) -> module\n\
+"__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module\n\
\n\
Import a module. Because this function is meant for use by the Python\n\
interpreter and not for general use, it is better to use\n\
@@ -298,42 +68,25 @@ perform absolute or relative imports: 0 is absolute, while a positive number\n\
is the number of parent directories to search relative to the current module.");
-/*[clinic input]
-abs as builtin_abs
-
- x: object
- /
-
-Return the absolute value of the argument.
-[clinic start generated code]*/
-
static PyObject *
-builtin_abs(PyObject *module, PyObject *x)
-/*[clinic end generated code: output=b1b433b9e51356f5 input=bed4ca14e29c20d1]*/
+builtin_abs(PyObject *self, PyObject *v)
{
- return PyNumber_Absolute(x);
+ return PyNumber_Absolute(v);
}
-/*[clinic input]
-all as builtin_all
-
- iterable: object
- /
-
-Return True if bool(x) is True for all values x in the iterable.
-
-If the iterable is empty, return True.
-[clinic start generated code]*/
+PyDoc_STRVAR(abs_doc,
+"abs(number) -> number\n\
+\n\
+Return the absolute value of the argument.");
static PyObject *
-builtin_all(PyObject *module, PyObject *iterable)
-/*[clinic end generated code: output=ca2a7127276f79b3 input=1a7c5d1bc3438a21]*/
+builtin_all(PyObject *self, PyObject *v)
{
PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int cmp;
- it = PyObject_GetIter(iterable);
+ it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
@@ -363,26 +116,20 @@ builtin_all(PyObject *module, PyObject *iterable)
Py_RETURN_TRUE;
}
-/*[clinic input]
-any as builtin_any
-
- iterable: object
- /
-
-Return True if bool(x) is True for any x in the iterable.
-
-If the iterable is empty, return False.
-[clinic start generated code]*/
+PyDoc_STRVAR(all_doc,
+"all(iterable) -> bool\n\
+\n\
+Return True if bool(x) is True for all values x in the iterable.\n\
+If the iterable is empty, return True.");
static PyObject *
-builtin_any(PyObject *module, PyObject *iterable)
-/*[clinic end generated code: output=fa65684748caa60e input=41d7451c23384f24]*/
+builtin_any(PyObject *self, PyObject *v)
{
PyObject *it, *item;
PyObject *(*iternext)(PyObject *);
int cmp;
- it = PyObject_GetIter(iterable);
+ it = PyObject_GetIter(v);
if (it == NULL)
return NULL;
iternext = *Py_TYPE(it)->tp_iternext;
@@ -397,7 +144,7 @@ builtin_any(PyObject *module, PyObject *iterable)
Py_DECREF(it);
return NULL;
}
- if (cmp > 0) {
+ if (cmp == 1) {
Py_DECREF(it);
Py_RETURN_TRUE;
}
@@ -412,381 +159,368 @@ builtin_any(PyObject *module, PyObject *iterable)
Py_RETURN_FALSE;
}
-/*[clinic input]
-ascii as builtin_ascii
-
- obj: object
- /
-
-Return an ASCII-only representation of an object.
-
-As repr(), return a string containing a printable representation of an
-object, but escape the non-ASCII characters in the string returned by
-repr() using \\x, \\u or \\U escapes. This generates a string similar
-to that returned by repr() in Python 2.
-[clinic start generated code]*/
+PyDoc_STRVAR(any_doc,
+"any(iterable) -> bool\n\
+\n\
+Return True if bool(x) is True for any x in the iterable.\n\
+If the iterable is empty, return False.");
static PyObject *
-builtin_ascii(PyObject *module, PyObject *obj)
-/*[clinic end generated code: output=6d37b3f0984c7eb9 input=4c62732e1b3a3cc9]*/
+builtin_apply(PyObject *self, PyObject *args)
{
- return PyObject_ASCII(obj);
-}
-
+ PyObject *func, *alist = NULL, *kwdict = NULL;
+ PyObject *t = NULL, *retval = NULL;
-/*[clinic input]
-bin as builtin_bin
+ if (PyErr_WarnPy3k("apply() not supported in 3.x; "
+ "use func(*args, **kwargs)", 1) < 0)
+ return NULL;
- number: object
- /
+ if (!PyArg_UnpackTuple(args, "apply", 1, 3, &func, &alist, &kwdict))
+ return NULL;
+ if (alist != NULL) {
+ if (!PyTuple_Check(alist)) {
+ if (!PySequence_Check(alist)) {
+ PyErr_Format(PyExc_TypeError,
+ "apply() arg 2 expected sequence, found %s",
+ alist->ob_type->tp_name);
+ return NULL;
+ }
+ t = PySequence_Tuple(alist);
+ if (t == NULL)
+ return NULL;
+ alist = t;
+ }
+ }
+ if (kwdict != NULL && !PyDict_Check(kwdict)) {
+ PyErr_Format(PyExc_TypeError,
+ "apply() arg 3 expected dictionary, found %s",
+ kwdict->ob_type->tp_name);
+ goto finally;
+ }
+ retval = PyEval_CallObjectWithKeywords(func, alist, kwdict);
+ finally:
+ Py_XDECREF(t);
+ return retval;
+}
-Return the binary representation of an integer.
+PyDoc_STRVAR(apply_doc,
+"apply(object[, args[, kwargs]]) -> value\n\
+\n\
+Call a callable object with positional arguments taken from the tuple args,\n\
+and keyword arguments taken from the optional dictionary kwargs.\n\
+Note that classes are callable, as are instances with a __call__() method.\n\
+\n\
+Deprecated since release 2.3. Instead, use the extended call syntax:\n\
+ function(*args, **keywords).");
- >>> bin(2796202)
- '0b1010101010101010101010'
-[clinic start generated code]*/
static PyObject *
-builtin_bin(PyObject *module, PyObject *number)
-/*[clinic end generated code: output=b6fc4ad5e649f4f7 input=53f8a0264bacaf90]*/
+builtin_bin(PyObject *self, PyObject *v)
{
- return PyNumber_ToBase(number, 2);
+ return PyNumber_ToBase(v, 2);
}
+PyDoc_STRVAR(bin_doc,
+"bin(number) -> string\n\
+\n\
+Return the binary representation of an integer or long integer.");
-/*[clinic input]
-callable as builtin_callable
-
- obj: object
- /
-
-Return whether the object is callable (i.e., some kind of function).
-
-Note that classes are callable, as are instances of classes with a
-__call__() method.
-[clinic start generated code]*/
static PyObject *
-builtin_callable(PyObject *module, PyObject *obj)
-/*[clinic end generated code: output=2b095d59d934cb7e input=1423bab99cc41f58]*/
+builtin_callable(PyObject *self, PyObject *v)
{
- return PyBool_FromLong((long)PyCallable_Check(obj));
+ return PyBool_FromLong((long)PyCallable_Check(v));
}
-static PyObject *
-builtin_breakpoint(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
-{
- PyObject *hook = PySys_GetObject("breakpointhook");
-
- if (hook == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "lost sys.breakpointhook");
- return NULL;
- }
-
- if (PySys_Audit("builtins.breakpoint", "O", hook) < 0) {
- return NULL;
- }
-
- Py_INCREF(hook);
- PyObject *retval = _PyObject_Vectorcall(hook, args, nargs, keywords);
- Py_DECREF(hook);
- return retval;
-}
-
-PyDoc_STRVAR(breakpoint_doc,
-"breakpoint(*args, **kws)\n\
-\n\
-Call sys.breakpointhook(*args, **kws). sys.breakpointhook() must accept\n\
-whatever arguments are passed.\n\
+PyDoc_STRVAR(callable_doc,
+"callable(object) -> bool\n\
\n\
-By default, this drops you into the pdb debugger.");
+Return whether the object is callable (i.e., some kind of function).\n\
+Note that classes are callable, as are instances with a __call__() method.");
-typedef struct {
- PyObject_HEAD
- PyObject *func;
- PyObject *it;
-} filterobject;
static PyObject *
-filter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+builtin_filter(PyObject *self, PyObject *args)
{
- PyObject *func, *seq;
- PyObject *it;
- filterobject *lz;
+ PyObject *func, *seq, *result, *it, *arg;
+ Py_ssize_t len; /* guess for result list size */
+ register Py_ssize_t j;
- if (type == &PyFilter_Type && !_PyArg_NoKeywords("filter", kwds))
+ if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
return NULL;
- if (!PyArg_UnpackTuple(args, "filter", 2, 2, &func, &seq))
+ /* Strings and tuples return a result of the same type. */
+ if (PyString_Check(seq))
+ return filterstring(func, seq);
+#ifdef Py_USING_UNICODE
+ if (PyUnicode_Check(seq))
+ return filterunicode(func, seq);
+#endif
+ if (PyTuple_Check(seq))
+ return filtertuple(func, seq);
+
+ /* Pre-allocate argument list tuple. */
+ arg = PyTuple_New(1);
+ if (arg == NULL)
return NULL;
/* Get iterator. */
it = PyObject_GetIter(seq);
if (it == NULL)
- return NULL;
-
- /* create filterobject structure */
- lz = (filterobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(it);
- return NULL;
- }
- Py_INCREF(func);
- lz->func = func;
- lz->it = it;
-
- return (PyObject *)lz;
-}
-
-static void
-filter_dealloc(filterobject *lz)
-{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->func);
- Py_XDECREF(lz->it);
- Py_TYPE(lz)->tp_free(lz);
-}
+ goto Fail_arg;
-static int
-filter_traverse(filterobject *lz, visitproc visit, void *arg)
-{
- Py_VISIT(lz->it);
- Py_VISIT(lz->func);
- return 0;
-}
+ /* Guess a result list size. */
+ len = _PyObject_LengthHint(seq, 8);
+ if (len == -1)
+ goto Fail_it;
-static PyObject *
-filter_next(filterobject *lz)
-{
- PyObject *item;
- PyObject *it = lz->it;
- long ok;
- PyObject *(*iternext)(PyObject *);
- int checktrue = lz->func == Py_None || lz->func == (PyObject *)&PyBool_Type;
+ /* Get a result list. */
+ if (PyList_Check(seq) && seq->ob_refcnt == 1) {
+ /* Eww - can modify the list in-place. */
+ Py_INCREF(seq);
+ result = seq;
+ }
+ else {
+ result = PyList_New(len);
+ if (result == NULL)
+ goto Fail_it;
+ }
- iternext = *Py_TYPE(it)->tp_iternext;
+ /* Build the result list. */
+ j = 0;
for (;;) {
- item = iternext(it);
- if (item == NULL)
- return NULL;
+ PyObject *item;
+ int ok;
- if (checktrue) {
+ item = PyIter_Next(it);
+ if (item == NULL) {
+ if (PyErr_Occurred())
+ goto Fail_result_it;
+ break;
+ }
+
+ if (func == (PyObject *)&PyBool_Type || func == Py_None) {
ok = PyObject_IsTrue(item);
- } else {
+ }
+ else {
PyObject *good;
- good = _PyObject_CallOneArg(lz->func, item);
+ PyTuple_SET_ITEM(arg, 0, item);
+ good = PyObject_Call(func, arg, NULL);
+ PyTuple_SET_ITEM(arg, 0, NULL);
if (good == NULL) {
Py_DECREF(item);
- return NULL;
+ goto Fail_result_it;
}
ok = PyObject_IsTrue(good);
Py_DECREF(good);
}
- if (ok > 0)
- return item;
- Py_DECREF(item);
- if (ok < 0)
- return NULL;
+ if (ok > 0) {
+ if (j < len)
+ PyList_SET_ITEM(result, j, item);
+ else {
+ int status = PyList_Append(result, item);
+ Py_DECREF(item);
+ if (status < 0)
+ goto Fail_result_it;
+ }
+ ++j;
+ }
+ else {
+ Py_DECREF(item);
+ if (ok < 0)
+ goto Fail_result_it;
+ }
}
+
+
+ /* Cut back result list if len is too big. */
+ if (j < len && PyList_SetSlice(result, j, len, NULL) < 0)
+ goto Fail_result_it;
+
+ Py_DECREF(it);
+ Py_DECREF(arg);
+ return result;
+
+Fail_result_it:
+ Py_DECREF(result);
+Fail_it:
+ Py_DECREF(it);
+Fail_arg:
+ Py_DECREF(arg);
+ return NULL;
}
+PyDoc_STRVAR(filter_doc,
+"filter(function or None, iterable) -> list, string or tuple\n\
+\n\
+Return a sequence yielding those items of iterable for which function(item)\n\
+is true. If function is None, return the items that are true.\n\
+If iterable is a string or a tuple, the result also has that type; otherwise\n\
+it is always a list.");
+
static PyObject *
-filter_reduce(filterobject *lz, PyObject *Py_UNUSED(ignored))
+builtin_format(PyObject *self, PyObject *args)
{
- return Py_BuildValue("O(OO)", Py_TYPE(lz), lz->func, lz->it);
+ PyObject *value;
+ PyObject *format_spec = NULL;
+
+ if (!PyArg_ParseTuple(args, "O|O:format", &value, &format_spec))
+ return NULL;
+
+ return PyObject_Format(value, format_spec);
}
-PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
+PyDoc_STRVAR(format_doc,
+"format(value[, format_spec]) -> string\n\
+\n\
+Returns value.__format__(format_spec)\n\
+format_spec defaults to the empty string.\n\
+See the Format Specification Mini-Language section of help('FORMATTING') for\n\
+details.");
-static PyMethodDef filter_methods[] = {
- {"__reduce__", (PyCFunction)filter_reduce, METH_NOARGS, reduce_doc},
- {NULL, NULL} /* sentinel */
-};
+static PyObject *
+builtin_chr(PyObject *self, PyObject *args)
+{
+ long x;
+ char s[1];
-PyDoc_STRVAR(filter_doc,
-"filter(function or None, iterable) --> filter object\n\
+ if (!PyArg_ParseTuple(args, "l:chr", &x))
+ return NULL;
+ if (x < 0 || x >= 256) {
+ PyErr_SetString(PyExc_ValueError,
+ "chr() arg not in range(256)");
+ return NULL;
+ }
+ s[0] = (char)x;
+ return PyString_FromStringAndSize(s, 1);
+}
+
+PyDoc_STRVAR(chr_doc,
+"chr(i) -> character\n\
\n\
-Return an iterator yielding those items of iterable for which function(item)\n\
-is true. If function is None, return the items that are true.");
-
-PyTypeObject PyFilter_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "filter", /* tp_name */
- sizeof(filterobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)filter_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_as_async */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- filter_doc, /* tp_doc */
- (traverseproc)filter_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)filter_next, /* tp_iternext */
- filter_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- filter_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
-};
+Return a string of one character with ordinal i; 0 <= i < 256.");
-/*[clinic input]
-format as builtin_format
+#ifdef Py_USING_UNICODE
+static PyObject *
+builtin_unichr(PyObject *self, PyObject *args)
+{
+ int x;
- value: object
- format_spec: unicode(c_default="NULL") = ''
- /
+ if (!PyArg_ParseTuple(args, "i:unichr", &x))
+ return NULL;
-Return value.__format__(format_spec)
+ return PyUnicode_FromOrdinal(x);
+}
+
+PyDoc_STRVAR(unichr_doc,
+"unichr(i) -> Unicode character\n\
+\n\
+Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.");
+#endif
-format_spec defaults to the empty string.
-See the Format Specification Mini-Language section of help('FORMATTING') for
-details.
-[clinic start generated code]*/
static PyObject *
-builtin_format_impl(PyObject *module, PyObject *value, PyObject *format_spec)
-/*[clinic end generated code: output=2f40bdfa4954b077 input=88339c93ea522b33]*/
+builtin_cmp(PyObject *self, PyObject *args)
{
- return PyObject_Format(value, format_spec);
-}
+ PyObject *a, *b;
+ int c;
-/*[clinic input]
-chr as builtin_chr
+ if (!PyArg_UnpackTuple(args, "cmp", 2, 2, &a, &b))
+ return NULL;
+ if (PyObject_Cmp(a, b, &c) < 0)
+ return NULL;
+ return PyInt_FromLong((long)c);
+}
- i: int
- /
+PyDoc_STRVAR(cmp_doc,
+"cmp(x, y) -> integer\n\
+\n\
+Return negative if x<y, zero if x==y, positive if x>y.");
-Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
-[clinic start generated code]*/
static PyObject *
-builtin_chr_impl(PyObject *module, int i)
-/*[clinic end generated code: output=c733afcd200afcb7 input=3f604ef45a70750d]*/
+builtin_coerce(PyObject *self, PyObject *args)
{
- return PyUnicode_FromOrdinal(i);
-}
+ PyObject *v, *w;
+ PyObject *res;
+ if (PyErr_WarnPy3k("coerce() not supported in 3.x", 1) < 0)
+ return NULL;
-/*[clinic input]
-compile as builtin_compile
-
- source: object
- filename: object(converter="PyUnicode_FSDecoder")
- mode: str
- flags: int = 0
- dont_inherit: bool(accept={int}) = False
- optimize: int = -1
- *
- _feature_version as feature_version: int = -1
-
-Compile source into a code object that can be executed by exec() or eval().
-
-The source code may represent a Python module, statement or expression.
-The filename will be used for run-time error messages.
-The mode must be 'exec' to compile a module, 'single' to compile a
-single (interactive) statement, or 'eval' to compile an expression.
-The flags argument, if present, controls which future statements influence
-the compilation of the code.
-The dont_inherit argument, if true, stops the compilation inheriting
-the effects of any future statements in effect in the code calling
-compile; if absent or false these statements do influence the compilation,
-in addition to any features explicitly specified.
-[clinic start generated code]*/
+ if (!PyArg_UnpackTuple(args, "coerce", 2, 2, &v, &w))
+ return NULL;
+ if (PyNumber_Coerce(&v, &w) < 0)
+ return NULL;
+ res = PyTuple_Pack(2, v, w);
+ Py_DECREF(v);
+ Py_DECREF(w);
+ return res;
+}
+
+PyDoc_STRVAR(coerce_doc,
+"coerce(x, y) -> (x1, y1)\n\
+\n\
+Return a tuple consisting of the two numeric arguments converted to\n\
+a common type, using the same rules as used by arithmetic operations.\n\
+If coercion is not possible, raise TypeError.");
static PyObject *
-builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
- const char *mode, int flags, int dont_inherit,
- int optimize, int feature_version)
-/*[clinic end generated code: output=b0c09c84f116d3d7 input=40171fb92c1d580d]*/
+builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *source_copy;
- const char *str;
- int compile_mode = -1;
+ char *str;
+ char *filename;
+ char *startstr;
+ int mode = -1;
+ int dont_inherit = 0;
+ int supplied_flags = 0;
int is_ast;
- int start[] = {Py_file_input, Py_eval_input, Py_single_input, Py_func_type_input};
- PyObject *result;
+ PyCompilerFlags cf;
+ PyObject *result = NULL, *cmd, *tmp = NULL;
+ Py_ssize_t length;
+ static char *kwlist[] = {"source", "filename", "mode", "flags",
+ "dont_inherit", NULL};
+ int start[] = {Py_file_input, Py_eval_input, Py_single_input};
- PyCompilerFlags cf = _PyCompilerFlags_INIT;
- cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
- if (feature_version >= 0 && (flags & PyCF_ONLY_AST)) {
- cf.cf_feature_version = feature_version;
- }
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile",
+ kwlist, &cmd, &filename, &startstr,
+ &supplied_flags, &dont_inherit))
+ return NULL;
- if (flags &
- ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS))
+ cf.cf_flags = supplied_flags;
+
+ if (supplied_flags &
+ ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
{
PyErr_SetString(PyExc_ValueError,
"compile(): unrecognised flags");
- goto error;
+ return NULL;
}
/* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */
- if (optimize < -1 || optimize > 2) {
- PyErr_SetString(PyExc_ValueError,
- "compile(): invalid optimize value");
- goto error;
- }
-
if (!dont_inherit) {
PyEval_MergeCompilerFlags(&cf);
}
- if (strcmp(mode, "exec") == 0)
- compile_mode = 0;
- else if (strcmp(mode, "eval") == 0)
- compile_mode = 1;
- else if (strcmp(mode, "single") == 0)
- compile_mode = 2;
- else if (strcmp(mode, "func_type") == 0) {
- if (!(flags & PyCF_ONLY_AST)) {
- PyErr_SetString(PyExc_ValueError,
- "compile() mode 'func_type' requires flag PyCF_ONLY_AST");
- goto error;
- }
- compile_mode = 3;
- }
+ if (strcmp(startstr, "exec") == 0)
+ mode = 0;
+ else if (strcmp(startstr, "eval") == 0)
+ mode = 1;
+ else if (strcmp(startstr, "single") == 0)
+ mode = 2;
else {
- const char *msg;
- if (flags & PyCF_ONLY_AST)
- msg = "compile() mode must be 'exec', 'eval', 'single' or 'func_type'";
- else
- msg = "compile() mode must be 'exec', 'eval' or 'single'";
- PyErr_SetString(PyExc_ValueError, msg);
- goto error;
+ PyErr_SetString(PyExc_ValueError,
+ "compile() arg 3 must be 'exec', 'eval' or 'single'");
+ return NULL;
}
- is_ast = PyAST_Check(source);
+ is_ast = PyAST_Check(cmd);
if (is_ast == -1)
- goto error;
+ return NULL;
if (is_ast) {
- if (flags & PyCF_ONLY_AST) {
- Py_INCREF(source);
- result = source;
+ if (supplied_flags & PyCF_ONLY_AST) {
+ Py_INCREF(cmd);
+ result = cmd;
}
else {
PyArena *arena;
@@ -794,39 +528,68 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
arena = PyArena_New();
if (arena == NULL)
- goto error;
- mod = PyAST_obj2mod(source, arena, compile_mode);
+ return NULL;
+ mod = PyAST_obj2mod(cmd, arena, mode);
if (mod == NULL) {
PyArena_Free(arena);
- goto error;
- }
- if (!PyAST_Validate(mod)) {
- PyArena_Free(arena);
- goto error;
+ return NULL;
}
- result = (PyObject*)PyAST_CompileObject(mod, filename,
- &cf, optimize, arena);
+ result = (PyObject*)PyAST_Compile(mod, filename,
+ &cf, arena);
PyArena_Free(arena);
}
- goto finally;
+ return result;
}
-
- str = _Py_SourceAsString(source, "compile", "string, bytes or AST", &cf, &source_copy);
- if (str == NULL)
- goto error;
-
- result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize);
- Py_XDECREF(source_copy);
- goto finally;
-
-error:
- result = NULL;
-finally:
- Py_DECREF(filename);
+ if (PyString_Check(cmd)) {
+ str = PyString_AS_STRING(cmd);
+ length = PyString_GET_SIZE(cmd);
+ }
+#ifdef Py_USING_UNICODE
+ else if (PyUnicode_Check(cmd)) {
+ tmp = PyUnicode_AsUTF8String(cmd);
+ if (tmp == NULL)
+ return NULL;
+ cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
+ str = PyString_AS_STRING(tmp);
+ length = PyString_GET_SIZE(tmp);
+ }
+#endif
+ else if (!PyObject_AsReadBuffer(cmd, (const void **)&str, &length)) {
+ /* Copy to NUL-terminated buffer. */
+ tmp = PyString_FromStringAndSize(str, length);
+ if (tmp == NULL)
+ return NULL;
+ str = PyString_AS_STRING(tmp);
+ length = PyString_GET_SIZE(tmp);
+ }
+ else
+ goto cleanup;
+ if ((size_t)length != strlen(str)) {
+ PyErr_SetString(PyExc_TypeError,
+ "compile() expected string without null bytes");
+ goto cleanup;
+ }
+ result = Py_CompileStringFlags(str, filename, start[mode], &cf);
+cleanup:
+ Py_XDECREF(tmp);
return result;
}
-/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
+PyDoc_STRVAR(compile_doc,
+"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\
+\n\
+Compile the source string (a Python module, statement or expression)\n\
+into a code object that can be executed by the exec statement or eval().\n\
+The filename will be used for run-time error messages.\n\
+The mode must be 'exec' to compile a module, 'single' to compile a\n\
+single (interactive) statement, or 'eval' to compile an expression.\n\
+The flags argument, if present, controls which future statements influence\n\
+the compilation of the code.\n\
+The dont_inherit argument, if non-zero, stops the compilation inheriting\n\
+the effects of any future statements in effect in the code calling\n\
+compile; if absent or zero these statements do influence the compilation,\n\
+in addition to any features explicitly specified.");
+
static PyObject *
builtin_dir(PyObject *self, PyObject *args)
{
@@ -851,49 +614,32 @@ PyDoc_STRVAR(dir_doc,
" for any other object: its attributes, its class's attributes, and\n"
" recursively the attributes of its class's base classes.");
-/*[clinic input]
-divmod as builtin_divmod
-
- x: object
- y: object
- /
-
-Return the tuple (x//y, x%y). Invariant: div*y + mod == x.
-[clinic start generated code]*/
-
static PyObject *
-builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
-/*[clinic end generated code: output=b06d8a5f6e0c745e input=175ad9c84ff41a85]*/
+builtin_divmod(PyObject *self, PyObject *args)
{
- return PyNumber_Divmod(x, y);
-}
-
+ PyObject *v, *w;
-/*[clinic input]
-eval as builtin_eval
-
- source: object
- globals: object = None
- locals: object = None
- /
+ if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w))
+ return NULL;
+ return PyNumber_Divmod(v, w);
+}
-Evaluate the given source in the context of globals and locals.
+PyDoc_STRVAR(divmod_doc,
+"divmod(x, y) -> (quotient, remainder)\n\
+\n\
+Return the tuple (x//y, x%y). Invariant: div*y + mod == x.");
-The source may be a string representing a Python expression
-or a code object as returned by compile().
-The globals must be a dictionary and locals can be any mapping,
-defaulting to the current globals and locals.
-If only globals is given, locals defaults to it.
-[clinic start generated code]*/
static PyObject *
-builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
- PyObject *locals)
-/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
+builtin_eval(PyObject *self, PyObject *args)
{
- PyObject *result, *source_copy;
- const char *str;
+ PyObject *cmd, *result, *tmp = NULL;
+ PyObject *globals = Py_None, *locals = Py_None;
+ char *str;
+ PyCompilerFlags cf;
+ if (!PyArg_UnpackTuple(args, "eval", 1, 3, &cmd, &globals, &locals))
+ return NULL;
if (locals != Py_None && !PyMapping_Check(locals)) {
PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
return NULL;
@@ -906,11 +652,8 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
}
if (globals == Py_None) {
globals = PyEval_GetGlobals();
- if (locals == Py_None) {
+ if (locals == Py_None)
locals = PyEval_GetLocals();
- if (locals == NULL)
- return NULL;
- }
}
else if (locals == Py_None)
locals = globals;
@@ -922,165 +665,195 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
return NULL;
}
- if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
- if (_PyDict_SetItemId(globals, &PyId___builtins__,
- PyEval_GetBuiltins()) != 0)
+ if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
return NULL;
}
- else if (PyErr_Occurred()) {
- return NULL;
- }
- if (PyCode_Check(source)) {
- if (PySys_Audit("exec", "O", source) < 0) {
- return NULL;
- }
-
- if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
+ if (PyCode_Check(cmd)) {
+ if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) {
PyErr_SetString(PyExc_TypeError,
- "code object passed to eval() may not contain free variables");
+ "code object passed to eval() may not contain free variables");
return NULL;
}
- return PyEval_EvalCode(source, globals, locals);
+ return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals);
}
- PyCompilerFlags cf = _PyCompilerFlags_INIT;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
- str = _Py_SourceAsString(source, "eval", "string, bytes or code", &cf, &source_copy);
- if (str == NULL)
+ if (!PyString_Check(cmd) &&
+ !PyUnicode_Check(cmd)) {
+ PyErr_SetString(PyExc_TypeError,
+ "eval() arg 1 must be a string or code object");
return NULL;
+ }
+ cf.cf_flags = 0;
+#ifdef Py_USING_UNICODE
+ if (PyUnicode_Check(cmd)) {
+ tmp = PyUnicode_AsUTF8String(cmd);
+ if (tmp == NULL)
+ return NULL;
+ cmd = tmp;
+ cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
+ }
+#endif
+ if (PyString_AsStringAndSize(cmd, &str, NULL)) {
+ Py_XDECREF(tmp);
+ return NULL;
+ }
while (*str == ' ' || *str == '\t')
str++;
(void)PyEval_MergeCompilerFlags(&cf);
result = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
- Py_XDECREF(source_copy);
+ Py_XDECREF(tmp);
return result;
}
-/*[clinic input]
-exec as builtin_exec
-
- source: object
- globals: object = None
- locals: object = None
- /
-
-Execute the given source in the context of globals and locals.
+PyDoc_STRVAR(eval_doc,
+"eval(source[, globals[, locals]]) -> value\n\
+\n\
+Evaluate the source in the context of globals and locals.\n\
+The source may be a string representing a Python expression\n\
+or a code object as returned by compile().\n\
+The globals must be a dictionary and locals can be any mapping,\n\
+defaulting to the current globals and locals.\n\
+If only globals is given, locals defaults to it.\n");
-The source may be a string representing one or more Python statements
-or a code object as returned by compile().
-The globals must be a dictionary and locals can be any mapping,
-defaulting to the current globals and locals.
-If only globals is given, locals defaults to it.
-[clinic start generated code]*/
static PyObject *
-builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
- PyObject *locals)
-/*[clinic end generated code: output=3c90efc6ab68ef5d input=01ca3e1c01692829]*/
+builtin_execfile(PyObject *self, PyObject *args)
{
- PyObject *v;
+ char *filename;
+ PyObject *globals = Py_None, *locals = Py_None;
+ PyObject *res;
+ FILE* fp = NULL;
+ PyCompilerFlags cf;
+ int exists;
+
+ if (PyErr_WarnPy3k("execfile() not supported in 3.x; use exec()",
+ 1) < 0)
+ return NULL;
+ if (!PyArg_ParseTuple(args, "s|O!O:execfile",
+ &filename,
+ &PyDict_Type, &globals,
+ &locals))
+ return NULL;
+ if (locals != Py_None && !PyMapping_Check(locals)) {
+ PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
+ return NULL;
+ }
if (globals == Py_None) {
globals = PyEval_GetGlobals();
- if (locals == Py_None) {
+ if (locals == Py_None)
locals = PyEval_GetLocals();
- if (locals == NULL)
- return NULL;
- }
- if (!globals || !locals) {
- PyErr_SetString(PyExc_SystemError,
- "globals and locals cannot be NULL");
- return NULL;
- }
}
else if (locals == Py_None)
locals = globals;
-
- if (!PyDict_Check(globals)) {
- PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not %.100s",
- globals->ob_type->tp_name);
- return NULL;
- }
- if (!PyMapping_Check(locals)) {
- PyErr_Format(PyExc_TypeError,
- "locals must be a mapping or None, not %.100s",
- locals->ob_type->tp_name);
- return NULL;
- }
- if (_PyDict_GetItemIdWithError(globals, &PyId___builtins__) == NULL) {
- if (_PyDict_SetItemId(globals, &PyId___builtins__,
- PyEval_GetBuiltins()) != 0)
+ if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
return NULL;
}
- else if (PyErr_Occurred()) {
- return NULL;
- }
- if (PyCode_Check(source)) {
- if (PySys_Audit("exec", "O", source) < 0) {
- return NULL;
- }
+ exists = 0;
+ /* Test for existence or directory. */
+#if defined(PLAN9)
+ {
+ Dir *d;
- if (PyCode_GetNumFree((PyCodeObject *)source) > 0) {
- PyErr_SetString(PyExc_TypeError,
- "code object passed to exec() may not "
- "contain free variables");
- return NULL;
+ if ((d = dirstat(filename))!=nil) {
+ if(d->mode & DMDIR)
+ werrstr("is a directory");
+ else
+ exists = 1;
+ free(d);
}
- v = PyEval_EvalCode(source, globals, locals);
}
- else {
- PyObject *source_copy;
- const char *str;
- PyCompilerFlags cf = _PyCompilerFlags_INIT;
- cf.cf_flags = PyCF_SOURCE_IS_UTF8;
- str = _Py_SourceAsString(source, "exec",
- "string, bytes or code", &cf,
- &source_copy);
- if (str == NULL)
- return NULL;
- if (PyEval_MergeCompilerFlags(&cf))
- v = PyRun_StringFlags(str, Py_file_input, globals,
- locals, &cf);
+#elif defined(RISCOS)
+ if (object_exists(filename)) {
+ if (isdir(filename))
+ errno = EISDIR;
else
- v = PyRun_String(str, Py_file_input, globals, locals);
- Py_XDECREF(source_copy);
+ exists = 1;
}
- if (v == NULL)
+#else /* standard Posix */
+ {
+ struct stat s;
+ if (stat(filename, &s) == 0) {
+ if (S_ISDIR(s.st_mode))
+# if defined(PYOS_OS2) && defined(PYCC_VACPP)
+ errno = EOS2ERR;
+# else
+ errno = EISDIR;
+# endif
+ else
+ exists = 1;
+ }
+ }
+#endif
+
+ if (exists) {
+ Py_BEGIN_ALLOW_THREADS
+ fp = fopen(filename, "r" PY_STDIOTEXTMODE);
+ Py_END_ALLOW_THREADS
+
+ if (fp == NULL) {
+ exists = 0;
+ }
+ }
+
+ if (!exists) {
+ PyErr_SetFromErrnoWithFilename(PyExc_IOError, filename);
return NULL;
- Py_DECREF(v);
- Py_RETURN_NONE;
+ }
+ cf.cf_flags = 0;
+ if (PyEval_MergeCompilerFlags(&cf))
+ res = PyRun_FileExFlags(fp, filename, Py_file_input, globals,
+ locals, 1, &cf);
+ else
+ res = PyRun_FileEx(fp, filename, Py_file_input, globals,
+ locals, 1);
+ return res;
}
+PyDoc_STRVAR(execfile_doc,
+"execfile(filename[, globals[, locals]])\n\
+\n\
+Read and execute a Python script from a file.\n\
+The globals and locals are dictionaries, defaulting to the current\n\
+globals and locals. If only globals is given, locals defaults to it.");
+
-/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
-builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+builtin_getattr(PyObject *self, PyObject *args)
{
- PyObject *v, *name, *result;
+ PyObject *v, *result, *dflt = NULL;
+ PyObject *name;
- if (!_PyArg_CheckPositional("getattr", nargs, 2, 3))
+ if (!PyArg_UnpackTuple(args, "getattr", 2, 3, &v, &name, &dflt))
return NULL;
+#ifdef Py_USING_UNICODE
+ if (PyUnicode_Check(name)) {
+ name = _PyUnicode_AsDefaultEncodedString(name, NULL);
+ if (name == NULL)
+ return NULL;
+ }
+#endif
- v = args[0];
- name = args[1];
- if (!PyUnicode_Check(name)) {
+ if (!PyString_Check(name)) {
PyErr_SetString(PyExc_TypeError,
"getattr(): attribute name must be string");
return NULL;
}
- if (nargs > 2) {
- if (_PyObject_LookupAttr(v, name, &result) == 0) {
- PyObject *dflt = args[2];
- Py_INCREF(dflt);
- return dflt;
- }
- }
- else {
- result = PyObject_GetAttr(v, name);
+ result = PyObject_GetAttr(v, name);
+ if (result == NULL && dflt != NULL &&
+ PyErr_ExceptionMatches(PyExc_AttributeError))
+ {
+ PyErr_Clear();
+ Py_INCREF(dflt);
+ result = dflt;
}
return result;
}
@@ -1093,18 +866,8 @@ When a default argument is given, it is returned when the attribute doesn't\n\
exist; without it, an exception is raised in that case.");
-/*[clinic input]
-globals as builtin_globals
-
-Return the dictionary containing the current scope's global variables.
-
-NOTE: Updates to this dictionary *will* affect name lookups in the current
-global scope and vice-versa.
-[clinic start generated code]*/
-
static PyObject *
-builtin_globals_impl(PyObject *module)
-/*[clinic end generated code: output=e5dd1527067b94d2 input=9327576f92bb48ba]*/
+builtin_globals(PyObject *self)
{
PyObject *d;
@@ -1113,275 +876,243 @@ builtin_globals_impl(PyObject *module)
return d;
}
+PyDoc_STRVAR(globals_doc,
+"globals() -> dictionary\n\
+\n\
+Return the dictionary containing the current scope's global variables.");
-/*[clinic input]
-hasattr as builtin_hasattr
-
- obj: object
- name: object
- /
-
-Return whether the object has an attribute with the given name.
-
-This is done by calling getattr(obj, name) and catching AttributeError.
-[clinic start generated code]*/
static PyObject *
-builtin_hasattr_impl(PyObject *module, PyObject *obj, PyObject *name)
-/*[clinic end generated code: output=a7aff2090a4151e5 input=0faec9787d979542]*/
+builtin_hasattr(PyObject *self, PyObject *args)
{
PyObject *v;
+ PyObject *name;
- if (!PyUnicode_Check(name)) {
- PyErr_SetString(PyExc_TypeError,
- "hasattr(): attribute name must be string");
+ if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name))
return NULL;
+#ifdef Py_USING_UNICODE
+ if (PyUnicode_Check(name)) {
+ name = _PyUnicode_AsDefaultEncodedString(name, NULL);
+ if (name == NULL)
+ return NULL;
}
- if (_PyObject_LookupAttr(obj, name, &v) < 0) {
+#endif
+
+ if (!PyString_Check(name)) {
+ PyErr_SetString(PyExc_TypeError,
+ "hasattr(): attribute name must be string");
return NULL;
}
+ v = PyObject_GetAttr(v, name);
if (v == NULL) {
- Py_RETURN_FALSE;
+ if (!PyErr_ExceptionMatches(PyExc_Exception))
+ return NULL;
+ else {
+ PyErr_Clear();
+ Py_INCREF(Py_False);
+ return Py_False;
+ }
}
Py_DECREF(v);
- Py_RETURN_TRUE;
+ Py_INCREF(Py_True);
+ return Py_True;
}
+PyDoc_STRVAR(hasattr_doc,
+"hasattr(object, name) -> bool\n\
+\n\
+Return whether the object has an attribute with the given name.\n\
+(This is done by calling getattr(object, name) and catching exceptions.)");
-/* AC: gdb's integration with CPython relies on builtin_id having
- * the *exact* parameter names of "self" and "v", so we ensure we
- * preserve those name rather than using the AC defaults.
- */
-/*[clinic input]
-id as builtin_id
-
- self: self(type="PyModuleDef *")
- obj as v: object
- /
-
-Return the identity of an object.
-
-This is guaranteed to be unique among simultaneously existing objects.
-(CPython uses the object's memory address.)
-[clinic start generated code]*/
static PyObject *
-builtin_id(PyModuleDef *self, PyObject *v)
-/*[clinic end generated code: output=0aa640785f697f65 input=5a534136419631f4]*/
+builtin_id(PyObject *self, PyObject *v)
{
- PyObject *id = PyLong_FromVoidPtr(v);
-
- if (id && PySys_Audit("builtins.id", "O", id) < 0) {
- Py_DECREF(id);
- return NULL;
- }
-
- return id;
+ return PyLong_FromVoidPtr(v);
}
+PyDoc_STRVAR(id_doc,
+"id(object) -> integer\n\
+\n\
+Return the identity of an object. This is guaranteed to be unique among\n\
+simultaneously existing objects. (Hint: it's the object's memory address.)");
-/* map object ************************************************************/
-
-typedef struct {
- PyObject_HEAD
- PyObject *iters;
- PyObject *func;
-} mapobject;
static PyObject *
-map_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+builtin_map(PyObject *self, PyObject *args)
{
- PyObject *it, *iters, *func;
- mapobject *lz;
- Py_ssize_t numargs, i;
-
- if (type == &PyMap_Type && !_PyArg_NoKeywords("map", kwds))
- return NULL;
-
- numargs = PyTuple_Size(args);
- if (numargs < 2) {
+ typedef struct {
+ PyObject *it; /* the iterator object */
+ int saw_StopIteration; /* bool: did the iterator end? */
+ } sequence;
+
+ PyObject *func, *result;
+ sequence *seqs = NULL, *sqp;
+ Py_ssize_t n, len;
+ register int i, j;
+
+ n = PyTuple_Size(args);
+ if (n < 2) {
PyErr_SetString(PyExc_TypeError,
- "map() must have at least two arguments.");
+ "map() requires at least two args");
return NULL;
}
- iters = PyTuple_New(numargs-1);
- if (iters == NULL)
- return NULL;
+ func = PyTuple_GetItem(args, 0);
+ n--;
- for (i=1 ; i<numargs ; i++) {
- /* Get iterator. */
- it = PyObject_GetIter(PyTuple_GET_ITEM(args, i));
- if (it == NULL) {
- Py_DECREF(iters);
+ if (func == Py_None) {
+ if (PyErr_WarnPy3k("map(None, ...) not supported in 3.x; "
+ "use list(...)", 1) < 0)
return NULL;
+ if (n == 1) {
+ /* map(None, S) is the same as list(S). */
+ return PySequence_List(PyTuple_GetItem(args, 1));
}
- PyTuple_SET_ITEM(iters, i-1, it);
}
- /* create mapobject structure */
- lz = (mapobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(iters);
+ /* Get space for sequence descriptors. Must NULL out the iterator
+ * pointers so that jumping to Fail_2 later doesn't see trash.
+ */
+ if ((seqs = PyMem_NEW(sequence, n)) == NULL) {
+ PyErr_NoMemory();
return NULL;
}
- lz->iters = iters;
- func = PyTuple_GET_ITEM(args, 0);
- Py_INCREF(func);
- lz->func = func;
-
- return (PyObject *)lz;
-}
+ for (i = 0; i < n; ++i) {
+ seqs[i].it = (PyObject*)NULL;
+ seqs[i].saw_StopIteration = 0;
+ }
-static void
-map_dealloc(mapobject *lz)
-{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->iters);
- Py_XDECREF(lz->func);
- Py_TYPE(lz)->tp_free(lz);
-}
+ /* Do a first pass to obtain iterators for the arguments, and set len
+ * to the largest of their lengths.
+ */
+ len = 0;
+ for (i = 0, sqp = seqs; i < n; ++i, ++sqp) {
+ PyObject *curseq;
+ Py_ssize_t curlen;
-static int
-map_traverse(mapobject *lz, visitproc visit, void *arg)
-{
- Py_VISIT(lz->iters);
- Py_VISIT(lz->func);
- return 0;
-}
+ /* Get iterator. */
+ curseq = PyTuple_GetItem(args, i+1);
+ sqp->it = PyObject_GetIter(curseq);
+ if (sqp->it == NULL) {
+ static char errmsg[] =
+ "argument %d to map() must support iteration";
+ char errbuf[sizeof(errmsg) + 25];
+ PyOS_snprintf(errbuf, sizeof(errbuf), errmsg, i+2);
+ PyErr_SetString(PyExc_TypeError, errbuf);
+ goto Fail_2;
+ }
+
+ /* Update len. */
+ curlen = _PyObject_LengthHint(curseq, 8);
+ if (curlen > len)
+ len = curlen;
+ }
+
+ /* Get space for the result list. */
+ if ((result = (PyObject *) PyList_New(len)) == NULL)
+ goto Fail_2;
+
+ /* Iterate over the sequences until all have stopped. */
+ for (i = 0; ; ++i) {
+ PyObject *alist, *item=NULL, *value;
+ int numactive = 0;
+
+ if (func == Py_None && n == 1)
+ alist = NULL;
+ else if ((alist = PyTuple_New(n)) == NULL)
+ goto Fail_1;
+
+ for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
+ if (sqp->saw_StopIteration) {
+ Py_INCREF(Py_None);
+ item = Py_None;
+ }
+ else {
+ item = PyIter_Next(sqp->it);
+ if (item)
+ ++numactive;
+ else {
+ if (PyErr_Occurred()) {
+ Py_XDECREF(alist);
+ goto Fail_1;
+ }
+ Py_INCREF(Py_None);
+ item = Py_None;
+ sqp->saw_StopIteration = 1;
+ }
+ }
+ if (alist)
+ PyTuple_SET_ITEM(alist, j, item);
+ else
+ break;
+ }
-static PyObject *
-map_next(mapobject *lz)
-{
- PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
- PyObject **stack;
- PyObject *result = NULL;
- PyThreadState *tstate = _PyThreadState_GET();
+ if (!alist)
+ alist = item;
- const Py_ssize_t niters = PyTuple_GET_SIZE(lz->iters);
- if (niters <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
- stack = small_stack;
- }
- else {
- stack = PyMem_Malloc(niters * sizeof(stack[0]));
- if (stack == NULL) {
- _PyErr_NoMemory(tstate);
- return NULL;
+ if (numactive == 0) {
+ Py_DECREF(alist);
+ break;
}
- }
- Py_ssize_t nargs = 0;
- for (Py_ssize_t i=0; i < niters; i++) {
- PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
- PyObject *val = Py_TYPE(it)->tp_iternext(it);
- if (val == NULL) {
- goto exit;
+ if (func == Py_None)
+ value = alist;
+ else {
+ value = PyEval_CallObject(func, alist);
+ Py_DECREF(alist);
+ if (value == NULL)
+ goto Fail_1;
+ }
+ if (i >= len) {
+ int status = PyList_Append(result, value);
+ Py_DECREF(value);
+ if (status < 0)
+ goto Fail_1;
}
- stack[i] = val;
- nargs++;
+ else if (PyList_SetItem(result, i, value) < 0)
+ goto Fail_1;
}
- result = _PyObject_VectorcallTstate(tstate, lz->func, stack, nargs, NULL);
-
-exit:
- for (Py_ssize_t i=0; i < nargs; i++) {
- Py_DECREF(stack[i]);
- }
- if (stack != small_stack) {
- PyMem_Free(stack);
- }
- return result;
-}
+ if (i < len && PyList_SetSlice(result, i, len, NULL) < 0)
+ goto Fail_1;
-static PyObject *
-map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
-{
- Py_ssize_t numargs = PyTuple_GET_SIZE(lz->iters);
- PyObject *args = PyTuple_New(numargs+1);
- Py_ssize_t i;
- if (args == NULL)
- return NULL;
- Py_INCREF(lz->func);
- PyTuple_SET_ITEM(args, 0, lz->func);
- for (i = 0; i<numargs; i++){
- PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
- Py_INCREF(it);
- PyTuple_SET_ITEM(args, i+1, it);
- }
+ goto Succeed;
- return Py_BuildValue("ON", Py_TYPE(lz), args);
+Fail_1:
+ Py_DECREF(result);
+Fail_2:
+ result = NULL;
+Succeed:
+ assert(seqs);
+ for (i = 0; i < n; ++i)
+ Py_XDECREF(seqs[i].it);
+ PyMem_DEL(seqs);
+ return result;
}
-static PyMethodDef map_methods[] = {
- {"__reduce__", (PyCFunction)map_reduce, METH_NOARGS, reduce_doc},
- {NULL, NULL} /* sentinel */
-};
-
-
PyDoc_STRVAR(map_doc,
-"map(func, *iterables) --> map object\n\
+"map(function, sequence[, sequence, ...]) -> list\n\
\n\
-Make an iterator that computes the function using arguments from\n\
-each of the iterables. Stops when the shortest iterable is exhausted.");
-
-PyTypeObject PyMap_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "map", /* tp_name */
- sizeof(mapobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)map_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_as_async */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- map_doc, /* tp_doc */
- (traverseproc)map_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)map_next, /* tp_iternext */
- map_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- map_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
-};
+Return a list of the results of applying the function to the items of\n\
+the argument sequence(s). If more than one sequence is given, the\n\
+function is called with an argument list consisting of the corresponding\n\
+item of each sequence, substituting None for missing values when not all\n\
+sequences have the same length. If the function is None, return a list of\n\
+the items of the sequence (or a list of tuples if more than one sequence).");
-/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
-builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+builtin_next(PyObject *self, PyObject *args)
{
PyObject *it, *res;
+ PyObject *def = NULL;
- if (!_PyArg_CheckPositional("next", nargs, 1, 2))
+ if (!PyArg_UnpackTuple(args, "next", 1, 2, &it, &def))
return NULL;
-
- it = args[0];
if (!PyIter_Check(it)) {
PyErr_Format(PyExc_TypeError,
- "'%.200s' object is not an iterator",
+ "%.200s object is not an iterator",
it->ob_type->tp_name);
return NULL;
}
@@ -1389,10 +1120,9 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
res = (*it->ob_type->tp_iternext)(it);
if (res != NULL) {
return res;
- } else if (nargs > 1) {
- PyObject *def = args[1];
+ } else if (def != NULL) {
if (PyErr_Occurred()) {
- if(!PyErr_ExceptionMatches(PyExc_StopIteration))
+ if (!PyErr_ExceptionMatches(PyExc_StopIteration))
return NULL;
PyErr_Clear();
}
@@ -1413,119 +1143,178 @@ Return the next item from the iterator. If default is given and the iterator\n\
is exhausted, it is returned instead of raising StopIteration.");
-/*[clinic input]
-setattr as builtin_setattr
+static PyObject *
+builtin_setattr(PyObject *self, PyObject *args)
+{
+ PyObject *v;
+ PyObject *name;
+ PyObject *value;
- obj: object
- name: object
- value: object
- /
+ if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value))
+ return NULL;
+ if (PyObject_SetAttr(v, name, value) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
+}
-Sets the named attribute on the given object to the specified value.
+PyDoc_STRVAR(setattr_doc,
+"setattr(object, name, value)\n\
+\n\
+Set a named attribute on an object; setattr(x, 'y', v) is equivalent to\n\
+``x.y = v''.");
-setattr(x, 'y', v) is equivalent to ``x.y = v''
-[clinic start generated code]*/
static PyObject *
-builtin_setattr_impl(PyObject *module, PyObject *obj, PyObject *name,
- PyObject *value)
-/*[clinic end generated code: output=dc2ce1d1add9acb4 input=bd2b7ca6875a1899]*/
+builtin_delattr(PyObject *self, PyObject *args)
{
- if (PyObject_SetAttr(obj, name, value) != 0)
+ PyObject *v;
+ PyObject *name;
+
+ if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name))
return NULL;
- Py_RETURN_NONE;
+ if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0)
+ return NULL;
+ Py_INCREF(Py_None);
+ return Py_None;
}
+PyDoc_STRVAR(delattr_doc,
+"delattr(object, name)\n\
+\n\
+Delete a named attribute on an object; delattr(x, 'y') is equivalent to\n\
+``del x.y''.");
-/*[clinic input]
-delattr as builtin_delattr
- obj: object
- name: object
- /
+static PyObject *
+builtin_hash(PyObject *self, PyObject *v)
+{
+ long x;
+
+ x = PyObject_Hash(v);
+ if (x == -1)
+ return NULL;
+ return PyInt_FromLong(x);
+}
-Deletes the named attribute from the given object.
+PyDoc_STRVAR(hash_doc,
+"hash(object) -> integer\n\
+\n\
+Return a hash value for the object. Two objects with the same value have\n\
+the same hash value. The reverse is not necessarily true, but likely.");
-delattr(x, 'y') is equivalent to ``del x.y''
-[clinic start generated code]*/
static PyObject *
-builtin_delattr_impl(PyObject *module, PyObject *obj, PyObject *name)
-/*[clinic end generated code: output=85134bc58dff79fa input=db16685d6b4b9410]*/
+builtin_hex(PyObject *self, PyObject *v)
{
- if (PyObject_SetAttr(obj, name, (PyObject *)NULL) != 0)
+ PyNumberMethods *nb;
+ PyObject *res;
+
+ if ((nb = v->ob_type->tp_as_number) == NULL ||
+ nb->nb_hex == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "hex() argument can't be converted to hex");
return NULL;
- Py_RETURN_NONE;
+ }
+ res = (*nb->nb_hex)(v);
+ if (res && !PyString_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__hex__ returned non-string (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
}
+PyDoc_STRVAR(hex_doc,
+"hex(number) -> string\n\
+\n\
+Return the hexadecimal representation of an integer or long integer.");
-/*[clinic input]
-hash as builtin_hash
-
- obj: object
- /
-
-Return the hash value for the given object.
-Two objects that compare equal must also have the same hash value, but the
-reverse is not necessarily true.
-[clinic start generated code]*/
+static PyObject *builtin_raw_input(PyObject *, PyObject *);
static PyObject *
-builtin_hash(PyObject *module, PyObject *obj)
-/*[clinic end generated code: output=237668e9d7688db7 input=58c48be822bf9c54]*/
+builtin_input(PyObject *self, PyObject *args)
{
- Py_hash_t x;
+ PyObject *line;
+ char *str;
+ PyObject *res;
+ PyObject *globals, *locals;
+ PyCompilerFlags cf;
- x = PyObject_Hash(obj);
- if (x == -1)
+ line = builtin_raw_input(self, args);
+ if (line == NULL)
+ return line;
+ if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
return NULL;
- return PyLong_FromSsize_t(x);
+ while (*str == ' ' || *str == '\t')
+ str++;
+ globals = PyEval_GetGlobals();
+ locals = PyEval_GetLocals();
+ if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
+ if (PyDict_SetItemString(globals, "__builtins__",
+ PyEval_GetBuiltins()) != 0)
+ return NULL;
+ }
+ cf.cf_flags = 0;
+ PyEval_MergeCompilerFlags(&cf);
+ res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
+ Py_DECREF(line);
+ return res;
}
+PyDoc_STRVAR(input_doc,
+"input([prompt]) -> value\n\
+\n\
+Equivalent to eval(raw_input(prompt)).");
-/*[clinic input]
-hex as builtin_hex
-
- number: object
- /
-
-Return the hexadecimal representation of an integer.
-
- >>> hex(12648430)
- '0xc0ffee'
-[clinic start generated code]*/
static PyObject *
-builtin_hex(PyObject *module, PyObject *number)
-/*[clinic end generated code: output=e46b612169099408 input=e645aff5fc7d540e]*/
+builtin_intern(PyObject *self, PyObject *args)
{
- return PyNumber_ToBase(number, 16);
+ PyObject *s;
+ if (!PyArg_ParseTuple(args, "S:intern", &s))
+ return NULL;
+ if (!PyString_CheckExact(s)) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't intern subclass of string");
+ return NULL;
+ }
+ Py_INCREF(s);
+ PyString_InternInPlace(&s);
+ return s;
}
+PyDoc_STRVAR(intern_doc,
+"intern(string) -> string\n\
+\n\
+``Intern'' the given string. This enters the string in the (global)\n\
+table of interned strings whose purpose is to speed up dictionary lookups.\n\
+Return the string itself or the previously interned string object with the\n\
+same value.");
+
-/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
-builtin_iter(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
+builtin_iter(PyObject *self, PyObject *args)
{
- PyObject *v;
+ PyObject *v, *w = NULL;
- if (!_PyArg_CheckPositional("iter", nargs, 1, 2))
+ if (!PyArg_UnpackTuple(args, "iter", 1, 2, &v, &w))
return NULL;
- v = args[0];
- if (nargs == 1)
+ if (w == NULL)
return PyObject_GetIter(v);
if (!PyCallable_Check(v)) {
PyErr_SetString(PyExc_TypeError,
"iter(v, w): v must be callable");
return NULL;
}
- PyObject *sentinel = args[1];
- return PyCallIter_New(v, sentinel);
+ return PyCallIter_New(v, w);
}
PyDoc_STRVAR(iter_doc,
-"iter(iterable) -> iterator\n\
+"iter(collection) -> iterator\n\
iter(callable, sentinel) -> iterator\n\
\n\
Get an iterator from an object. In the first form, the argument must\n\
@@ -1533,43 +1322,25 @@ supply its own iterator, or be a sequence.\n\
In the second form, the callable is called until it returns the sentinel.");
-/*[clinic input]
-len as builtin_len
-
- obj: object
- /
-
-Return the number of items in a container.
-[clinic start generated code]*/
-
static PyObject *
-builtin_len(PyObject *module, PyObject *obj)
-/*[clinic end generated code: output=fa7a270d314dfb6c input=bc55598da9e9c9b5]*/
+builtin_len(PyObject *self, PyObject *v)
{
Py_ssize_t res;
- res = PyObject_Size(obj);
- if (res < 0) {
- assert(PyErr_Occurred());
+ res = PyObject_Size(v);
+ if (res < 0 && PyErr_Occurred())
return NULL;
- }
- return PyLong_FromSsize_t(res);
+ return PyInt_FromSsize_t(res);
}
+PyDoc_STRVAR(len_doc,
+"len(object) -> integer\n\
+\n\
+Return the number of items of a sequence or collection.");
-/*[clinic input]
-locals as builtin_locals
-
-Return a dictionary containing the current scope's local variables.
-
-NOTE: Whether or not updates to this dictionary will affect name lookups in
-the local scope and vice-versa is *implementation dependent* and not
-covered by any backwards compatibility guarantees.
-[clinic start generated code]*/
static PyObject *
-builtin_locals_impl(PyObject *module)
-/*[clinic end generated code: output=b46c94015ce11448 input=7874018d478d5c4b]*/
+builtin_locals(PyObject *self)
{
PyObject *d;
@@ -1578,54 +1349,45 @@ builtin_locals_impl(PyObject *module)
return d;
}
+PyDoc_STRVAR(locals_doc,
+"locals() -> dictionary\n\
+\n\
+Update and return a dictionary containing the current scope's local variables.");
+
static PyObject *
min_max(PyObject *args, PyObject *kwds, int op)
{
PyObject *v, *it, *item, *val, *maxitem, *maxval, *keyfunc=NULL;
- PyObject *emptytuple, *defaultval = NULL;
- static char *kwlist[] = {"key", "default", NULL};
const char *name = op == Py_LT ? "min" : "max";
- const int positional = PyTuple_Size(args) > 1;
- int ret;
- if (positional)
+ if (PyTuple_Size(args) > 1)
v = args;
- else if (!PyArg_UnpackTuple(args, name, 1, 1, &v))
+ else if (!PyArg_UnpackTuple(args, (char *)name, 1, 1, &v))
return NULL;
- emptytuple = PyTuple_New(0);
- if (emptytuple == NULL)
- return NULL;
- ret = PyArg_ParseTupleAndKeywords(emptytuple, kwds,
- (op == Py_LT) ? "|$OO:min" : "|$OO:max",
- kwlist, &keyfunc, &defaultval);
- Py_DECREF(emptytuple);
- if (!ret)
- return NULL;
-
- if (positional && defaultval != NULL) {
- PyErr_Format(PyExc_TypeError,
- "Cannot specify a default for %s() with multiple "
- "positional arguments", name);
- return NULL;
+ if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds)) {
+ keyfunc = PyDict_GetItemString(kwds, "key");
+ if (PyDict_Size(kwds)!=1 || keyfunc == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "%s() got an unexpected keyword argument", name);
+ return NULL;
+ }
+ Py_INCREF(keyfunc);
}
it = PyObject_GetIter(v);
if (it == NULL) {
+ Py_XDECREF(keyfunc);
return NULL;
}
- if (keyfunc == Py_None) {
- keyfunc = NULL;
- }
-
maxitem = NULL; /* the result */
maxval = NULL; /* the value associated with the result */
while (( item = PyIter_Next(it) )) {
/* get the value from the key function */
if (keyfunc != NULL) {
- val = _PyObject_CallOneArg(keyfunc, item);
+ val = PyObject_CallFunctionObjArgs(keyfunc, item, NULL);
if (val == NULL)
goto Fail_it_item;
}
@@ -1660,18 +1422,14 @@ min_max(PyObject *args, PyObject *kwds, int op)
if (PyErr_Occurred())
goto Fail_it;
if (maxval == NULL) {
+ PyErr_Format(PyExc_ValueError,
+ "%s() arg is an empty sequence", name);
assert(maxitem == NULL);
- if (defaultval != NULL) {
- Py_INCREF(defaultval);
- maxitem = defaultval;
- } else {
- PyErr_Format(PyExc_ValueError,
- "%s() arg is an empty sequence", name);
- }
}
else
Py_DECREF(maxval);
Py_DECREF(it);
+ Py_XDECREF(keyfunc);
return maxitem;
Fail_it_item_and_val:
@@ -1682,10 +1440,10 @@ Fail_it:
Py_XDECREF(maxval);
Py_XDECREF(maxitem);
Py_DECREF(it);
+ Py_XDECREF(keyfunc);
return NULL;
}
-/* AC: cannot convert yet, waiting for *args support */
static PyObject *
builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
{
@@ -1693,16 +1451,13 @@ builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
}
PyDoc_STRVAR(min_doc,
-"min(iterable, *[, default=obj, key=func]) -> value\n\
-min(arg1, arg2, *args, *[, key=func]) -> value\n\
+"min(iterable[, key=func]) -> value\n\
+min(a, b, c, ...[, key=func]) -> value\n\
\n\
-With a single iterable argument, return its smallest item. The\n\
-default keyword-only argument specifies an object to return if\n\
-the provided iterable is empty.\n\
+With a single iterable argument, return its smallest item.\n\
With two or more arguments, return the smallest argument.");
-/* AC: cannot convert yet, waiting for *args support */
static PyObject *
builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
{
@@ -1710,79 +1465,86 @@ builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
}
PyDoc_STRVAR(max_doc,
-"max(iterable, *[, default=obj, key=func]) -> value\n\
-max(arg1, arg2, *args, *[, key=func]) -> value\n\
+"max(iterable[, key=func]) -> value\n\
+max(a, b, c, ...[, key=func]) -> value\n\
\n\
-With a single iterable argument, return its biggest item. The\n\
-default keyword-only argument specifies an object to return if\n\
-the provided iterable is empty.\n\
+With a single iterable argument, return its largest item.\n\
With two or more arguments, return the largest argument.");
-/*[clinic input]
-oct as builtin_oct
+static PyObject *
+builtin_oct(PyObject *self, PyObject *v)
+{
+ PyNumberMethods *nb;
+ PyObject *res;
- number: object
- /
+ if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
+ nb->nb_oct == NULL) {
+ PyErr_SetString(PyExc_TypeError,
+ "oct() argument can't be converted to oct");
+ return NULL;
+ }
+ res = (*nb->nb_oct)(v);
+ if (res && !PyString_Check(res)) {
+ PyErr_Format(PyExc_TypeError,
+ "__oct__ returned non-string (type %.200s)",
+ res->ob_type->tp_name);
+ Py_DECREF(res);
+ return NULL;
+ }
+ return res;
+}
-Return the octal representation of an integer.
+PyDoc_STRVAR(oct_doc,
+"oct(number) -> string\n\
+\n\
+Return the octal representation of an integer or long integer.");
- >>> oct(342391)
- '0o1234567'
-[clinic start generated code]*/
static PyObject *
-builtin_oct(PyObject *module, PyObject *number)
-/*[clinic end generated code: output=40a34656b6875352 input=ad6b274af4016c72]*/
+builtin_open(PyObject *self, PyObject *args, PyObject *kwds)
{
- return PyNumber_ToBase(number, 8);
+ return PyObject_Call((PyObject*)&PyFile_Type, args, kwds);
}
+PyDoc_STRVAR(open_doc,
+"open(name[, mode[, buffering]]) -> file object\n\
+\n\
+Open a file using the file() type, returns a file object. This is the\n\
+preferred way to open a file. See file.__doc__ for further information.");
-/*[clinic input]
-ord as builtin_ord
-
- c: object
- /
-
-Return the Unicode code point for a one-character string.
-[clinic start generated code]*/
static PyObject *
-builtin_ord(PyObject *module, PyObject *c)
-/*[clinic end generated code: output=4fa5e87a323bae71 input=3064e5d6203ad012]*/
+builtin_ord(PyObject *self, PyObject* obj)
{
long ord;
Py_ssize_t size;
- if (PyBytes_Check(c)) {
- size = PyBytes_GET_SIZE(c);
+ if (PyString_Check(obj)) {
+ size = PyString_GET_SIZE(obj);
if (size == 1) {
- ord = (long)((unsigned char)*PyBytes_AS_STRING(c));
- return PyLong_FromLong(ord);
+ ord = (long)((unsigned char)*PyString_AS_STRING(obj));
+ return PyInt_FromLong(ord);
}
- }
- else if (PyUnicode_Check(c)) {
- if (PyUnicode_READY(c) == -1)
- return NULL;
- size = PyUnicode_GET_LENGTH(c);
+ } else if (PyByteArray_Check(obj)) {
+ size = PyByteArray_GET_SIZE(obj);
if (size == 1) {
- ord = (long)PyUnicode_READ_CHAR(c, 0);
- return PyLong_FromLong(ord);
+ ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj));
+ return PyInt_FromLong(ord);
}
- }
- else if (PyByteArray_Check(c)) {
- /* XXX Hopefully this is temporary */
- size = PyByteArray_GET_SIZE(c);
+
+#ifdef Py_USING_UNICODE
+ } else if (PyUnicode_Check(obj)) {
+ size = PyUnicode_GET_SIZE(obj);
if (size == 1) {
- ord = (long)((unsigned char)*PyByteArray_AS_STRING(c));
- return PyLong_FromLong(ord);
+ ord = (long)*PyUnicode_AS_UNICODE(obj);
+ return PyInt_FromLong(ord);
}
- }
- else {
+#endif
+ } else {
PyErr_Format(PyExc_TypeError,
"ord() expected string of length 1, but " \
- "%.200s found", c->ob_type->tp_name);
+ "%.200s found", obj->ob_type->tp_name);
return NULL;
}
@@ -1793,442 +1555,708 @@ builtin_ord(PyObject *module, PyObject *c)
return NULL;
}
+PyDoc_STRVAR(ord_doc,
+"ord(c) -> integer\n\
+\n\
+Return the integer ordinal of a one-character string.");
-/*[clinic input]
-pow as builtin_pow
-
- base: object
- exp: object
- mod: object = None
-
-Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
-
-Some types, such as ints, are able to use a more efficient algorithm when
-invoked using the three argument form.
-[clinic start generated code]*/
static PyObject *
-builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp,
- PyObject *mod)
-/*[clinic end generated code: output=3ca1538221bbf15f input=435dbd48a12efb23]*/
+builtin_pow(PyObject *self, PyObject *args)
{
- return PyNumber_Power(base, exp, mod);
+ PyObject *v, *w, *z = Py_None;
+
+ if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z))
+ return NULL;
+ return PyNumber_Power(v, w, z);
}
+PyDoc_STRVAR(pow_doc,
+"pow(x, y[, z]) -> number\n\
+\n\
+With two arguments, equivalent to x**y. With three arguments,\n\
+equivalent to (x**y) % z, but may be more efficient (e.g. for longs).");
+
-/* AC: cannot convert yet, waiting for *args support */
static PyObject *
-builtin_print(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+builtin_print(PyObject *self, PyObject *args, PyObject *kwds)
{
- static const char * const _keywords[] = {"sep", "end", "file", "flush", 0};
- static struct _PyArg_Parser _parser = {"|OOOp:print", _keywords, 0};
+ static char *kwlist[] = {"sep", "end", "file", 0};
+ static PyObject *dummy_args = NULL;
+ static PyObject *unicode_newline = NULL, *unicode_space = NULL;
+ static PyObject *str_newline = NULL, *str_space = NULL;
+ PyObject *newline, *space;
PyObject *sep = NULL, *end = NULL, *file = NULL;
- int flush = 0;
- int i, err;
+ int i, err, use_unicode = 0;
- if (kwnames != NULL &&
- !_PyArg_ParseStackAndKeywords(args + nargs, 0, kwnames, &_parser,
- &sep, &end, &file, &flush)) {
- return NULL;
+ if (dummy_args == NULL) {
+ if (!(dummy_args = PyTuple_New(0)))
+ return NULL;
}
-
- if (file == NULL || file == Py_None) {
- file = _PySys_GetObjectId(&PyId_stdout);
- if (file == NULL) {
- PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
+ if (str_newline == NULL) {
+ str_newline = PyString_FromString("\n");
+ if (str_newline == NULL)
+ return NULL;
+ str_space = PyString_FromString(" ");
+ if (str_space == NULL) {
+ Py_CLEAR(str_newline);
return NULL;
}
-
+#ifdef Py_USING_UNICODE
+ unicode_newline = PyUnicode_FromString("\n");
+ if (unicode_newline == NULL) {
+ Py_CLEAR(str_newline);
+ Py_CLEAR(str_space);
+ return NULL;
+ }
+ unicode_space = PyUnicode_FromString(" ");
+ if (unicode_space == NULL) {
+ Py_CLEAR(str_newline);
+ Py_CLEAR(str_space);
+ Py_CLEAR(unicode_space);
+ return NULL;
+ }
+#endif
+ }
+ if (!PyArg_ParseTupleAndKeywords(dummy_args, kwds, "|OOO:print",
+ kwlist, &sep, &end, &file))
+ return NULL;
+ if (file == NULL || file == Py_None) {
+ file = PySys_GetObject("stdout");
/* sys.stdout may be None when FILE* stdout isn't connected */
if (file == Py_None)
Py_RETURN_NONE;
}
-
if (sep == Py_None) {
sep = NULL;
}
- else if (sep && !PyUnicode_Check(sep)) {
- PyErr_Format(PyExc_TypeError,
- "sep must be None or a string, not %.200s",
- sep->ob_type->tp_name);
- return NULL;
+ else if (sep) {
+ if (PyUnicode_Check(sep)) {
+ use_unicode = 1;
+ }
+ else if (!PyString_Check(sep)) {
+ PyErr_Format(PyExc_TypeError,
+ "sep must be None, str or unicode, not %.200s",
+ sep->ob_type->tp_name);
+ return NULL;
+ }
}
- if (end == Py_None) {
+ if (end == Py_None)
end = NULL;
+ else if (end) {
+ if (PyUnicode_Check(end)) {
+ use_unicode = 1;
+ }
+ else if (!PyString_Check(end)) {
+ PyErr_Format(PyExc_TypeError,
+ "end must be None, str or unicode, not %.200s",
+ end->ob_type->tp_name);
+ return NULL;
+ }
}
- else if (end && !PyUnicode_Check(end)) {
- PyErr_Format(PyExc_TypeError,
- "end must be None or a string, not %.200s",
- end->ob_type->tp_name);
- return NULL;
+
+ if (!use_unicode) {
+ for (i = 0; i < PyTuple_Size(args); i++) {
+ if (PyUnicode_Check(PyTuple_GET_ITEM(args, i))) {
+ use_unicode = 1;
+ break;
+ }
+ }
+ }
+ if (use_unicode) {
+ newline = unicode_newline;
+ space = unicode_space;
+ }
+ else {
+ newline = str_newline;
+ space = str_space;
}
- for (i = 0; i < nargs; i++) {
+ for (i = 0; i < PyTuple_Size(args); i++) {
if (i > 0) {
if (sep == NULL)
- err = PyFile_WriteString(" ", file);
+ err = PyFile_WriteObject(space, file,
+ Py_PRINT_RAW);
else
err = PyFile_WriteObject(sep, file,
Py_PRINT_RAW);
if (err)
return NULL;
}
- err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
+ err = PyFile_WriteObject(PyTuple_GetItem(args, i), file,
+ Py_PRINT_RAW);
if (err)
return NULL;
}
if (end == NULL)
- err = PyFile_WriteString("\n", file);
+ err = PyFile_WriteObject(newline, file, Py_PRINT_RAW);
else
err = PyFile_WriteObject(end, file, Py_PRINT_RAW);
if (err)
return NULL;
- if (flush) {
- PyObject *tmp = _PyObject_CallMethodIdNoArgs(file, &PyId_flush);
- if (tmp == NULL)
- return NULL;
- Py_DECREF(tmp);
- }
-
Py_RETURN_NONE;
}
PyDoc_STRVAR(print_doc,
-"print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n\
+"print(value, ..., sep=' ', end='\\n', file=sys.stdout)\n\
\n\
Prints the values to a stream, or to sys.stdout by default.\n\
Optional keyword arguments:\n\
-file: a file-like object (stream); defaults to the current sys.stdout.\n\
-sep: string inserted between values, default a space.\n\
-end: string appended after the last value, default a newline.\n\
-flush: whether to forcibly flush the stream.");
+file: a file-like object (stream); defaults to the current sys.stdout.\n\
+sep: string inserted between values, default a space.\n\
+end: string appended after the last value, default a newline.");
+
+/* Return number of items in range (lo, hi, step), when arguments are
+ * PyInt or PyLong objects. step > 0 required. Return a value < 0 if
+ * & only if the true value is too large to fit in a signed long.
+ * Arguments MUST return 1 with either PyInt_Check() or
+ * PyLong_Check(). Return -1 when there is an error.
+ */
+static long
+get_len_of_range_longs(PyObject *lo, PyObject *hi, PyObject *step)
+{
+ /* -------------------------------------------------------------
+ Algorithm is equal to that of get_len_of_range(), but it operates
+ on PyObjects (which are assumed to be PyLong or PyInt objects).
+ ---------------------------------------------------------------*/
+ long n;
+ PyObject *diff = NULL;
+ PyObject *one = NULL;
+ PyObject *tmp1 = NULL, *tmp2 = NULL, *tmp3 = NULL;
+ /* holds sub-expression evaluations */
-/*[clinic input]
-input as builtin_input
+ /* if (lo >= hi), return length of 0. */
+ if (PyObject_Compare(lo, hi) >= 0)
+ return 0;
- prompt: object(c_default="NULL") = None
- /
+ if ((one = PyLong_FromLong(1L)) == NULL)
+ goto Fail;
-Read a string from standard input. The trailing newline is stripped.
+ if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
+ goto Fail;
-The prompt string, if given, is printed to standard output without a
-trailing newline before reading input.
+ if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
+ goto Fail;
-If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
-On *nix systems, readline is used if available.
-[clinic start generated code]*/
+ if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
+ goto Fail;
+ if ((tmp3 = PyNumber_Add(tmp2, one)) == NULL)
+ goto Fail;
+
+ n = PyLong_AsLong(tmp3);
+ if (PyErr_Occurred()) { /* Check for Overflow */
+ PyErr_Clear();
+ goto Fail;
+ }
+
+ Py_DECREF(tmp3);
+ Py_DECREF(tmp2);
+ Py_DECREF(diff);
+ Py_DECREF(tmp1);
+ Py_DECREF(one);
+ return n;
+
+ Fail:
+ Py_XDECREF(tmp3);
+ Py_XDECREF(tmp2);
+ Py_XDECREF(diff);
+ Py_XDECREF(tmp1);
+ Py_XDECREF(one);
+ return -1;
+}
+
+/* Helper function for handle_range_longs. If arg is int or long
+ object, returns it with incremented reference count. If arg is
+ float, raises type error. As a last resort, creates a new int by
+ calling arg type's nb_int method if it is defined. Returns NULL
+ and sets exception on error.
+
+ Returns a new reference to an int object. */
static PyObject *
-builtin_input_impl(PyObject *module, PyObject *prompt)
-/*[clinic end generated code: output=83db5a191e7a0d60 input=5e8bb70c2908fe3c]*/
+get_range_long_argument(PyObject *arg, const char *name)
{
- PyObject *fin = _PySys_GetObjectId(&PyId_stdin);
- PyObject *fout = _PySys_GetObjectId(&PyId_stdout);
- PyObject *ferr = _PySys_GetObjectId(&PyId_stderr);
- PyObject *tmp;
- long fd;
- int tty;
-
- /* Check that stdin/out/err are intact */
- if (fin == NULL || fin == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stdin");
+ PyObject *v;
+ PyNumberMethods *nb;
+ if (_PyAnyInt_Check(arg)) {
+ Py_INCREF(arg);
+ return arg;
+ }
+ if (PyFloat_Check(arg) ||
+ (nb = Py_TYPE(arg)->tp_as_number) == NULL ||
+ nb->nb_int == NULL) {
+ PyErr_Format(PyExc_TypeError,
+ "range() integer %s argument expected, got %s.",
+ name, arg->ob_type->tp_name);
return NULL;
}
- if (fout == NULL || fout == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stdout");
+ v = nb->nb_int(arg);
+ if (v == NULL)
return NULL;
- }
- if (ferr == NULL || ferr == Py_None) {
- PyErr_SetString(PyExc_RuntimeError,
- "input(): lost sys.stderr");
+ if (_PyAnyInt_Check(v))
+ return v;
+ Py_DECREF(v);
+ PyErr_SetString(PyExc_TypeError,
+ "__int__ should return int object");
+ return NULL;
+}
+
+/* An extension of builtin_range() that handles the case when PyLong
+ * arguments are given. */
+static PyObject *
+handle_range_longs(PyObject *self, PyObject *args)
+{
+ PyObject *ilow = NULL;
+ PyObject *ihigh = NULL;
+ PyObject *istep = NULL;
+
+ PyObject *low = NULL;
+ PyObject *high = NULL;
+ PyObject *step = NULL;
+
+ PyObject *curnum = NULL;
+ PyObject *v = NULL;
+ long bign;
+ Py_ssize_t i, n;
+ int cmp_result;
+
+ PyObject *zero = PyLong_FromLong(0);
+
+ if (zero == NULL)
return NULL;
- }
- if (PySys_Audit("builtins.input", "O", prompt ? prompt : Py_None) < 0) {
+ if (!PyArg_UnpackTuple(args, "range", 1, 3, &ilow, &ihigh, &istep)) {
+ Py_DECREF(zero);
return NULL;
}
- /* First of all, flush stderr */
- tmp = _PyObject_CallMethodIdNoArgs(ferr, &PyId_flush);
- if (tmp == NULL)
- PyErr_Clear();
+ /* Figure out which way we were called, supply defaults, and be
+ * sure to incref everything so that the decrefs at the end
+ * are correct. NB: ilow, ihigh and istep are borrowed references.
+ */
+ assert(ilow != NULL);
+ if (ihigh == NULL) {
+ /* only 1 arg -- it's the upper limit */
+ ihigh = ilow;
+ ilow = NULL;
+ }
+
+ /* convert ihigh if necessary */
+ assert(ihigh != NULL);
+ high = get_range_long_argument(ihigh, "end");
+ if (high == NULL)
+ goto Fail;
+
+ /* ihigh correct now; do ilow */
+ if (ilow == NULL) {
+ Py_INCREF(zero);
+ low = zero;
+ }
+ else {
+ low = get_range_long_argument(ilow, "start");
+ if (low == NULL)
+ goto Fail;
+ }
+
+ /* ilow and ihigh correct now; do istep */
+ if (istep == NULL)
+ step = PyLong_FromLong(1);
else
- Py_DECREF(tmp);
+ step = get_range_long_argument(istep, "step");
+ if (step == NULL)
+ goto Fail;
- /* We should only use (GNU) readline if Python's sys.stdin and
- sys.stdout are the same as C's stdin and stdout, because we
- need to pass it those. */
- tmp = _PyObject_CallMethodIdNoArgs(fin, &PyId_fileno);
- if (tmp == NULL) {
- PyErr_Clear();
- tty = 0;
+ if (PyObject_Cmp(step, zero, &cmp_result) == -1)
+ goto Fail;
+
+ if (cmp_result == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "range() step argument must not be zero");
+ goto Fail;
}
+
+ if (cmp_result > 0)
+ bign = get_len_of_range_longs(low, high, step);
else {
- fd = PyLong_AsLong(tmp);
- Py_DECREF(tmp);
- if (fd < 0 && PyErr_Occurred())
- return NULL;
- tty = fd == fileno(stdin) && isatty(fd);
+ PyObject *neg_step = PyNumber_Negative(step);
+ if (neg_step == NULL)
+ goto Fail;
+ bign = get_len_of_range_longs(high, low, neg_step);
+ Py_DECREF(neg_step);
}
- if (tty) {
- tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_fileno);
- if (tmp == NULL) {
+
+ n = (Py_ssize_t)bign;
+ if (bign < 0 || (long)n != bign) {
+ PyErr_SetString(PyExc_OverflowError,
+ "range() result has too many items");
+ goto Fail;
+ }
+
+ v = PyList_New(n);
+ if (v == NULL)
+ goto Fail;
+
+ curnum = low;
+ Py_INCREF(curnum);
+
+ for (i = 0; i < n; i++) {
+ PyObject *w = PyNumber_Long(curnum);
+ PyObject *tmp_num;
+ if (w == NULL)
+ goto Fail;
+
+ PyList_SET_ITEM(v, i, w);
+
+ tmp_num = PyNumber_Add(curnum, step);
+ if (tmp_num == NULL)
+ goto Fail;
+
+ Py_DECREF(curnum);
+ curnum = tmp_num;
+ }
+ Py_DECREF(low);
+ Py_DECREF(high);
+ Py_DECREF(step);
+ Py_DECREF(zero);
+ Py_DECREF(curnum);
+ return v;
+
+ Fail:
+ Py_XDECREF(low);
+ Py_XDECREF(high);
+ Py_XDECREF(step);
+ Py_DECREF(zero);
+ Py_XDECREF(curnum);
+ Py_XDECREF(v);
+ return NULL;
+}
+
+/* Return number of items in range/xrange (lo, hi, step). step > 0
+ * required. Return a value < 0 if & only if the true value is too
+ * large to fit in a signed long.
+ */
+static long
+get_len_of_range(long lo, long hi, long step)
+{
+ /* -------------------------------------------------------------
+ If lo >= hi, the range is empty.
+ Else if n values are in the range, the last one is
+ lo + (n-1)*step, which must be <= hi-1. Rearranging,
+ n <= (hi - lo - 1)/step + 1, so taking the floor of the RHS gives
+ the proper value. Since lo < hi in this case, hi-lo-1 >= 0, so
+ the RHS is non-negative and so truncation is the same as the
+ floor. Letting M be the largest positive long, the worst case
+ for the RHS numerator is hi=M, lo=-M-1, and then
+ hi-lo-1 = M-(-M-1)-1 = 2*M. Therefore unsigned long has enough
+ precision to compute the RHS exactly.
+ ---------------------------------------------------------------*/
+ long n = 0;
+ if (lo < hi) {
+ unsigned long uhi = (unsigned long)hi;
+ unsigned long ulo = (unsigned long)lo;
+ unsigned long diff = uhi - ulo - 1;
+ n = (long)(diff / (unsigned long)step + 1);
+ }
+ return n;
+}
+
+static PyObject *
+builtin_range(PyObject *self, PyObject *args)
+{
+ long ilow = 0, ihigh = 0, istep = 1;
+ long bign;
+ Py_ssize_t i, n;
+
+ PyObject *v;
+
+ if (PyTuple_Size(args) <= 1) {
+ if (!PyArg_ParseTuple(args,
+ "l;range() requires 1-3 int arguments",
+ &ihigh)) {
PyErr_Clear();
- tty = 0;
+ return handle_range_longs(self, args);
}
- else {
- fd = PyLong_AsLong(tmp);
- Py_DECREF(tmp);
- if (fd < 0 && PyErr_Occurred())
- return NULL;
- tty = fd == fileno(stdout) && isatty(fd);
+ }
+ else {
+ if (!PyArg_ParseTuple(args,
+ "ll|l;range() requires 1-3 int arguments",
+ &ilow, &ihigh, &istep)) {
+ PyErr_Clear();
+ return handle_range_longs(self, args);
}
}
+ if (istep == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "range() step argument must not be zero");
+ return NULL;
+ }
+ if (istep > 0)
+ bign = get_len_of_range(ilow, ihigh, istep);
+ else
+ bign = get_len_of_range(ihigh, ilow, -istep);
+ n = (Py_ssize_t)bign;
+ if (bign < 0 || (long)n != bign) {
+ PyErr_SetString(PyExc_OverflowError,
+ "range() result has too many items");
+ return NULL;
+ }
+ v = PyList_New(n);
+ if (v == NULL)
+ return NULL;
+ for (i = 0; i < n; i++) {
+ PyObject *w = PyInt_FromLong(ilow);
+ if (w == NULL) {
+ Py_DECREF(v);
+ return NULL;
+ }
+ PyList_SET_ITEM(v, i, w);
+ ilow += istep;
+ }
+ return v;
+}
+
+PyDoc_STRVAR(range_doc,
+"range(stop) -> list of integers\n\
+range(start, stop[, step]) -> list of integers\n\
+\n\
+Return a list containing an arithmetic progression of integers.\n\
+range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.\n\
+When step is given, it specifies the increment (or decrement).\n\
+For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
+These are exactly the valid indices for a list of 4 elements.");
+
- /* If we're interactive, use (GNU) readline */
- if (tty) {
- PyObject *po = NULL;
- const char *promptstr;
- char *s = NULL;
- PyObject *stdin_encoding = NULL, *stdin_errors = NULL;
- PyObject *stdout_encoding = NULL, *stdout_errors = NULL;
- const char *stdin_encoding_str, *stdin_errors_str;
+static PyObject *
+builtin_raw_input(PyObject *self, PyObject *args)
+{
+ PyObject *v = NULL;
+ PyObject *fin = PySys_GetObject("stdin");
+ PyObject *fout = PySys_GetObject("stdout");
+
+ if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
+ return NULL;
+
+ if (fin == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
+ return NULL;
+ }
+ if (fout == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
+ return NULL;
+ }
+ if (PyFile_SoftSpace(fout, 0)) {
+ if (PyFile_WriteString(" ", fout) != 0)
+ return NULL;
+ }
+ if (PyFile_AsFile(fin) && PyFile_AsFile(fout)
+ && isatty(fileno(PyFile_AsFile(fin)))
+ && isatty(fileno(PyFile_AsFile(fout)))) {
+ PyObject *po;
+ char *prompt;
+ char *s;
PyObject *result;
- size_t len;
-
- /* stdin is a text stream, so it must have an encoding. */
- stdin_encoding = _PyObject_GetAttrId(fin, &PyId_encoding);
- stdin_errors = _PyObject_GetAttrId(fin, &PyId_errors);
- if (!stdin_encoding || !stdin_errors ||
- !PyUnicode_Check(stdin_encoding) ||
- !PyUnicode_Check(stdin_errors)) {
- tty = 0;
- goto _readline_errors;
- }
- stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
- stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
- if (!stdin_encoding_str || !stdin_errors_str)
- goto _readline_errors;
- tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
- if (prompt != NULL) {
- /* We have a prompt, encode it as stdout would */
- const char *stdout_encoding_str, *stdout_errors_str;
- PyObject *stringpo;
- stdout_encoding = _PyObject_GetAttrId(fout, &PyId_encoding);
- stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors);
- if (!stdout_encoding || !stdout_errors ||
- !PyUnicode_Check(stdout_encoding) ||
- !PyUnicode_Check(stdout_errors)) {
- tty = 0;
- goto _readline_errors;
- }
- stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
- stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
- if (!stdout_encoding_str || !stdout_errors_str)
- goto _readline_errors;
- stringpo = PyObject_Str(prompt);
- if (stringpo == NULL)
- goto _readline_errors;
- po = PyUnicode_AsEncodedString(stringpo,
- stdout_encoding_str, stdout_errors_str);
- Py_CLEAR(stdout_encoding);
- Py_CLEAR(stdout_errors);
- Py_CLEAR(stringpo);
+ if (v != NULL) {
+ po = PyObject_Str(v);
if (po == NULL)
- goto _readline_errors;
- assert(PyBytes_Check(po));
- promptstr = PyBytes_AS_STRING(po);
+ return NULL;
+ prompt = PyString_AsString(po);
+ if (prompt == NULL)
+ return NULL;
}
else {
po = NULL;
- promptstr = "";
+ prompt = "";
}
- s = PyOS_Readline(stdin, stdout, promptstr);
+ s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
+ prompt);
+ Py_XDECREF(po);
if (s == NULL) {
- PyErr_CheckSignals();
if (!PyErr_Occurred())
PyErr_SetNone(PyExc_KeyboardInterrupt);
- goto _readline_errors;
+ return NULL;
}
-
- len = strlen(s);
- if (len == 0) {
+ if (*s == '\0') {
PyErr_SetNone(PyExc_EOFError);
result = NULL;
}
- else {
+ else { /* strip trailing '\n' */
+ size_t len = strlen(s);
if (len > PY_SSIZE_T_MAX) {
PyErr_SetString(PyExc_OverflowError,
- "input: input too long");
+ "[raw_]input: input too long");
result = NULL;
}
else {
- len--; /* strip trailing '\n' */
- if (len != 0 && s[len-1] == '\r')
- len--; /* strip trailing '\r' */
- result = PyUnicode_Decode(s, len, stdin_encoding_str,
- stdin_errors_str);
+ result = PyString_FromStringAndSize(s, len-1);
}
}
- Py_DECREF(stdin_encoding);
- Py_DECREF(stdin_errors);
- Py_XDECREF(po);
PyMem_FREE(s);
-
- if (result != NULL) {
- if (PySys_Audit("builtins.input/result", "O", result) < 0) {
- return NULL;
- }
- }
-
return result;
-
- _readline_errors:
- Py_XDECREF(stdin_encoding);
- Py_XDECREF(stdout_encoding);
- Py_XDECREF(stdin_errors);
- Py_XDECREF(stdout_errors);
- Py_XDECREF(po);
- if (tty)
- return NULL;
-
- PyErr_Clear();
}
-
- /* Fallback if we're not interactive */
- if (prompt != NULL) {
- if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0)
+ if (v != NULL) {
+ if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
return NULL;
}
- tmp = _PyObject_CallMethodIdNoArgs(fout, &PyId_flush);
- if (tmp == NULL)
- PyErr_Clear();
- else
- Py_DECREF(tmp);
return PyFile_GetLine(fin, -1);
}
+PyDoc_STRVAR(raw_input_doc,
+"raw_input([prompt]) -> string\n\
+\n\
+Read a string from standard input. The trailing newline is stripped.\n\
+If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
+On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
+is printed without a trailing newline before reading.");
-/*[clinic input]
-repr as builtin_repr
- obj: object
- /
+static PyObject *
+builtin_reduce(PyObject *self, PyObject *args)
+{
+ static PyObject *functools_reduce = NULL;
+
+ if (PyErr_WarnPy3k("reduce() not supported in 3.x; "
+ "use functools.reduce()", 1) < 0)
+ return NULL;
+
+ if (functools_reduce == NULL) {
+ PyObject *functools = PyImport_ImportModule("functools");
+ if (functools == NULL)
+ return NULL;
+ functools_reduce = PyObject_GetAttrString(functools, "reduce");
+ Py_DECREF(functools);
+ if (functools_reduce == NULL)
+ return NULL;
+ }
+ return PyObject_Call(functools_reduce, args, NULL);
+}
-Return the canonical string representation of the object.
+PyDoc_STRVAR(reduce_doc,
+"reduce(function, sequence[, initial]) -> value\n\
+\n\
+Apply a function of two arguments cumulatively to the items of a sequence,\n\
+from left to right, so as to reduce the sequence to a single value.\n\
+For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
+((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
+of the sequence in the calculation, and serves as a default when the\n\
+sequence is empty.");
-For many object types, including most builtins, eval(repr(obj)) == obj.
-[clinic start generated code]*/
static PyObject *
-builtin_repr(PyObject *module, PyObject *obj)
-/*[clinic end generated code: output=7ed3778c44fd0194 input=1c9e6d66d3e3be04]*/
+builtin_reload(PyObject *self, PyObject *v)
{
- return PyObject_Repr(obj);
+ if (PyErr_WarnPy3k("In 3.x, reload() is renamed to imp.reload()",
+ 1) < 0)
+ return NULL;
+
+ return PyImport_ReloadModule(v);
}
+PyDoc_STRVAR(reload_doc,
+"reload(module) -> module\n\
+\n\
+Reload the module. The module must have been successfully imported before.");
-/*[clinic input]
-round as builtin_round
- number: object
- ndigits: object = None
+static PyObject *
+builtin_repr(PyObject *self, PyObject *v)
+{
+ return PyObject_Repr(v);
+}
-Round a number to a given precision in decimal digits.
+PyDoc_STRVAR(repr_doc,
+"repr(object) -> string\n\
+\n\
+Return the canonical string representation of the object.\n\
+For most object types, eval(repr(object)) == object.");
-The return value is an integer if ndigits is omitted or None. Otherwise
-the return value has the same type as the number. ndigits may be negative.
-[clinic start generated code]*/
static PyObject *
-builtin_round_impl(PyObject *module, PyObject *number, PyObject *ndigits)
-/*[clinic end generated code: output=ff0d9dd176c02ede input=275678471d7aca15]*/
+builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *round, *result;
-
- if (Py_TYPE(number)->tp_dict == NULL) {
- if (PyType_Ready(Py_TYPE(number)) < 0)
- return NULL;
- }
+ double x;
+ PyObject *o_ndigits = NULL;
+ Py_ssize_t ndigits;
+ static char *kwlist[] = {"number", "ndigits", 0};
- round = _PyObject_LookupSpecial(number, &PyId___round__);
- if (round == NULL) {
- if (!PyErr_Occurred())
- PyErr_Format(PyExc_TypeError,
- "type %.100s doesn't define __round__ method",
- Py_TYPE(number)->tp_name);
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "d|O:round",
+ kwlist, &x, &o_ndigits))
return NULL;
+
+ if (o_ndigits == NULL) {
+ /* second argument defaults to 0 */
+ ndigits = 0;
+ }
+ else {
+ /* interpret 2nd argument as a Py_ssize_t; clip on overflow */
+ ndigits = PyNumber_AsSsize_t(o_ndigits, NULL);
+ if (ndigits == -1 && PyErr_Occurred())
+ return NULL;
}
- if (ndigits == Py_None)
- result = _PyObject_CallNoArg(round);
+ /* nans, infinities and zeros round to themselves */
+ if (!Py_IS_FINITE(x) || x == 0.0)
+ return PyFloat_FromDouble(x);
+
+ /* Deal with extreme values for ndigits. For ndigits > NDIGITS_MAX, x
+ always rounds to itself. For ndigits < NDIGITS_MIN, x always
+ rounds to +-0.0. Here 0.30103 is an upper bound for log10(2). */
+#define NDIGITS_MAX ((int)((DBL_MANT_DIG-DBL_MIN_EXP) * 0.30103))
+#define NDIGITS_MIN (-(int)((DBL_MAX_EXP + 1) * 0.30103))
+ if (ndigits > NDIGITS_MAX)
+ /* return x */
+ return PyFloat_FromDouble(x);
+ else if (ndigits < NDIGITS_MIN)
+ /* return 0.0, but with sign of x */
+ return PyFloat_FromDouble(0.0*x);
else
- result = _PyObject_CallOneArg(round, ndigits);
- Py_DECREF(round);
- return result;
+ /* finite x, and ndigits is not unreasonably large */
+ /* _Py_double_round is defined in floatobject.c */
+ return _Py_double_round(x, (int)ndigits);
+#undef NDIGITS_MAX
+#undef NDIGITS_MIN
}
-
-/*AC: we need to keep the kwds dict intact to easily call into the
- * list.sort method, which isn't currently supported in AC. So we just use
- * the initially generated signature with a custom implementation.
- */
-/* [disabled clinic input]
-sorted as builtin_sorted
-
- iterable as seq: object
- key as keyfunc: object = None
- reverse: object = False
-
-Return a new list containing all items from the iterable in ascending order.
-
-A custom key function can be supplied to customize the sort order, and the
-reverse flag can be set to request the result in descending order.
-[end disabled clinic input]*/
-
-PyDoc_STRVAR(builtin_sorted__doc__,
-"sorted($module, iterable, /, *, key=None, reverse=False)\n"
-"--\n"
-"\n"
-"Return a new list containing all items from the iterable in ascending order.\n"
-"\n"
-"A custom key function can be supplied to customize the sort order, and the\n"
-"reverse flag can be set to request the result in descending order.");
-
-#define BUILTIN_SORTED_METHODDEF \
- {"sorted", (PyCFunction)(void(*)(void))builtin_sorted, METH_FASTCALL | METH_KEYWORDS, builtin_sorted__doc__},
+PyDoc_STRVAR(round_doc,
+"round(number[, ndigits]) -> floating point number\n\
+\n\
+Round a number to a given precision in decimal digits (default 0 digits).\n\
+This always returns a floating point number. Precision may be negative.");
static PyObject *
-builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds)
{
- PyObject *newlist, *v, *seq, *callable;
+ PyObject *newlist, *v, *seq, *compare=NULL, *keyfunc=NULL, *newargs;
+ PyObject *callable;
+ static char *kwlist[] = {"iterable", "cmp", "key", "reverse", 0};
+ int reverse;
- /* Keyword arguments are passed through list.sort() which will check
- them. */
- if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
+ /* args 1-4 should match listsort in Objects/listobject.c */
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|OOi:sorted",
+ kwlist, &seq, &compare, &keyfunc, &reverse))
return NULL;
newlist = PySequence_List(seq);
if (newlist == NULL)
return NULL;
- callable = _PyObject_GetAttrId(newlist, &PyId_sort);
+ callable = PyObject_GetAttrString(newlist, "sort");
if (callable == NULL) {
Py_DECREF(newlist);
return NULL;
}
- assert(nargs >= 1);
- v = _PyObject_Vectorcall(callable, args + 1, nargs - 1, kwnames);
+ newargs = PyTuple_GetSlice(args, 1, 4);
+ if (newargs == NULL) {
+ Py_DECREF(newlist);
+ Py_DECREF(callable);
+ return NULL;
+ }
+
+ v = PyObject_Call(callable, newargs, kwds);
+ Py_DECREF(newargs);
Py_DECREF(callable);
if (v == NULL) {
Py_DECREF(newlist);
@@ -2238,8 +2266,9 @@ builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject
return newlist;
}
+PyDoc_STRVAR(sorted_doc,
+"sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list");
-/* AC: cannot convert yet, as needs PEP 457 group support in inspect */
static PyObject *
builtin_vars(PyObject *self, PyObject *args)
{
@@ -2250,12 +2279,20 @@ builtin_vars(PyObject *self, PyObject *args)
return NULL;
if (v == NULL) {
d = PyEval_GetLocals();
- Py_XINCREF(d);
+ if (d == NULL) {
+ if (!PyErr_Occurred())
+ PyErr_SetString(PyExc_SystemError,
+ "vars(): no locals!?");
+ }
+ else
+ Py_INCREF(d);
}
else {
- if (_PyObject_LookupAttrId(v, &PyId___dict__, &d) == 0) {
+ d = PyObject_GetAttrString(v, "__dict__");
+ if (d == NULL) {
PyErr_SetString(PyExc_TypeError,
"vars() argument must have __dict__ attribute");
+ return NULL;
}
}
return d;
@@ -2268,57 +2305,34 @@ Without arguments, equivalent to locals().\n\
With an argument, equivalent to object.__dict__.");
-/*[clinic input]
-sum as builtin_sum
-
- iterable: object
- /
- start: object(c_default="NULL") = 0
-
-Return the sum of a 'start' value (default: 0) plus an iterable of numbers
-
-When the iterable is empty, return the start value.
-This function is intended specifically for use with numeric values and may
-reject non-numeric types.
-[clinic start generated code]*/
-
-static PyObject *
-builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
-/*[clinic end generated code: output=df758cec7d1d302f input=162b50765250d222]*/
+static PyObject*
+builtin_sum(PyObject *self, PyObject *args)
{
- PyObject *result = start;
+ PyObject *seq;
+ PyObject *result = NULL;
PyObject *temp, *item, *iter;
- iter = PyObject_GetIter(iterable);
+ if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result))
+ return NULL;
+
+ iter = PyObject_GetIter(seq);
if (iter == NULL)
return NULL;
if (result == NULL) {
- result = PyLong_FromLong(0);
+ result = PyInt_FromLong(0);
if (result == NULL) {
Py_DECREF(iter);
return NULL;
}
} else {
/* reject string values for 'start' parameter */
- if (PyUnicode_Check(result)) {
+ if (PyObject_TypeCheck(result, &PyBaseString_Type)) {
PyErr_SetString(PyExc_TypeError,
"sum() can't sum strings [use ''.join(seq) instead]");
Py_DECREF(iter);
return NULL;
}
- if (PyBytes_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "sum() can't sum bytes [use b''.join(seq) instead]");
- Py_DECREF(iter);
- return NULL;
- }
- if (PyByteArray_Check(result)) {
- PyErr_SetString(PyExc_TypeError,
- "sum() can't sum bytearray [use b''.join(seq) instead]");
- Py_DECREF(iter);
- return NULL;
- }
Py_INCREF(result);
}
@@ -2327,35 +2341,29 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
Assumes all inputs are the same type. If the assumption fails, default
to the more general routine.
*/
- if (PyLong_CheckExact(result)) {
- int overflow;
- long i_result = PyLong_AsLongAndOverflow(result, &overflow);
- /* If this already overflowed, don't even enter the loop. */
- if (overflow == 0) {
- Py_DECREF(result);
- result = NULL;
- }
+ if (PyInt_CheckExact(result)) {
+ long i_result = PyInt_AS_LONG(result);
+ Py_DECREF(result);
+ result = NULL;
while(result == NULL) {
item = PyIter_Next(iter);
if (item == NULL) {
Py_DECREF(iter);
if (PyErr_Occurred())
return NULL;
- return PyLong_FromLong(i_result);
+ return PyInt_FromLong(i_result);
}
- if (PyLong_CheckExact(item) || PyBool_Check(item)) {
- long b = PyLong_AsLongAndOverflow(item, &overflow);
- if (overflow == 0 &&
- (i_result >= 0 ? (b <= LONG_MAX - i_result)
- : (b >= LONG_MIN - i_result)))
- {
- i_result += b;
+ if (PyInt_CheckExact(item)) {
+ long b = PyInt_AS_LONG(item);
+ long x = i_result + b;
+ if ((x^i_result) >= 0 || (x^b) >= 0) {
+ i_result = x;
Py_DECREF(item);
continue;
}
}
/* Either overflowed or is not an int. Restore real objects and process normally */
- result = PyLong_FromLong(i_result);
+ result = PyInt_FromLong(i_result);
if (result == NULL) {
Py_DECREF(item);
Py_DECREF(iter);
@@ -2385,19 +2393,18 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
return PyFloat_FromDouble(f_result);
}
if (PyFloat_CheckExact(item)) {
+ PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
f_result += PyFloat_AS_DOUBLE(item);
+ PyFPE_END_PROTECT(f_result)
Py_DECREF(item);
continue;
}
- if (PyLong_Check(item)) {
- long value;
- int overflow;
- value = PyLong_AsLongAndOverflow(item, &overflow);
- if (!overflow) {
- f_result += (double)value;
- Py_DECREF(item);
- continue;
- }
+ if (PyInt_CheckExact(item)) {
+ PyFPE_START_PROTECT("add", Py_DECREF(item); Py_DECREF(iter); return 0)
+ f_result += (double)PyInt_AS_LONG(item);
+ PyFPE_END_PROTECT(f_result)
+ Py_DECREF(item);
+ continue;
}
result = PyFloat_FromDouble(f_result);
if (result == NULL) {
@@ -2447,293 +2454,230 @@ builtin_sum_impl(PyObject *module, PyObject *iterable, PyObject *start)
return result;
}
+PyDoc_STRVAR(sum_doc,
+"sum(iterable[, start]) -> value\n\
+\n\
+Return the sum of an iterable or sequence of numbers (NOT strings)\n\
+plus the value of 'start' (which defaults to 0). When the sequence is\n\
+empty, return start.");
-/*[clinic input]
-isinstance as builtin_isinstance
-
- obj: object
- class_or_tuple: object
- /
-
-Return whether an object is an instance of a class or of a subclass thereof.
-
-A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to
-check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)
-or ...`` etc.
-[clinic start generated code]*/
static PyObject *
-builtin_isinstance_impl(PyObject *module, PyObject *obj,
- PyObject *class_or_tuple)
-/*[clinic end generated code: output=6faf01472c13b003 input=ffa743db1daf7549]*/
+builtin_isinstance(PyObject *self, PyObject *args)
{
+ PyObject *inst;
+ PyObject *cls;
int retval;
- retval = PyObject_IsInstance(obj, class_or_tuple);
+ if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls))
+ return NULL;
+
+ retval = PyObject_IsInstance(inst, cls);
if (retval < 0)
return NULL;
return PyBool_FromLong(retval);
}
+PyDoc_STRVAR(isinstance_doc,
+"isinstance(object, class-or-type-or-tuple) -> bool\n\
+\n\
+Return whether an object is an instance of a class or of a subclass thereof.\n\
+With a type as second argument, return whether that is the object's type.\n\
+The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for\n\
+isinstance(x, A) or isinstance(x, B) or ... (etc.).");
-/*[clinic input]
-issubclass as builtin_issubclass
-
- cls: object
- class_or_tuple: object
- /
-
-Return whether 'cls' is a derived from another class or is the same class.
-
-A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to
-check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)
-or ...`` etc.
-[clinic start generated code]*/
static PyObject *
-builtin_issubclass_impl(PyObject *module, PyObject *cls,
- PyObject *class_or_tuple)
-/*[clinic end generated code: output=358412410cd7a250 input=af5f35e9ceaddaf6]*/
+builtin_issubclass(PyObject *self, PyObject *args)
{
+ PyObject *derived;
+ PyObject *cls;
int retval;
- retval = PyObject_IsSubclass(cls, class_or_tuple);
+ if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls))
+ return NULL;
+
+ retval = PyObject_IsSubclass(derived, cls);
if (retval < 0)
return NULL;
return PyBool_FromLong(retval);
}
+PyDoc_STRVAR(issubclass_doc,
+"issubclass(C, B) -> bool\n\
+\n\
+Return whether class C is a subclass (i.e., a derived class) of class B.\n\
+When using a tuple as the second argument issubclass(X, (A, B, ...)),\n\
+is a shortcut for issubclass(X, A) or issubclass(X, B) or ... (etc.).");
-typedef struct {
- PyObject_HEAD
- Py_ssize_t tuplesize;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
-} zipobject;
-static PyObject *
-zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+static PyObject*
+builtin_zip(PyObject *self, PyObject *args)
{
- zipobject *lz;
+ PyObject *ret;
+ const Py_ssize_t itemsize = PySequence_Length(args);
Py_ssize_t i;
- PyObject *ittuple; /* tuple of iterators */
- PyObject *result;
- Py_ssize_t tuplesize;
+ PyObject *itlist; /* tuple of iterators */
+ Py_ssize_t len; /* guess at result length */
- if (type == &PyZip_Type && !_PyArg_NoKeywords("zip", kwds))
- return NULL;
+ if (itemsize == 0)
+ return PyList_New(0);
/* args must be a tuple */
assert(PyTuple_Check(args));
- tuplesize = PyTuple_GET_SIZE(args);
- /* obtain iterators */
- ittuple = PyTuple_New(tuplesize);
- if (ittuple == NULL)
- return NULL;
- for (i=0; i < tuplesize; ++i) {
+ /* Guess at result length: the shortest of the input lengths.
+ If some argument refuses to say, we refuse to guess too, lest
+ an argument like xrange(sys.maxint) lead us astray.*/
+ len = -1; /* unknown */
+ for (i = 0; i < itemsize; ++i) {
PyObject *item = PyTuple_GET_ITEM(args, i);
- PyObject *it = PyObject_GetIter(item);
- if (it == NULL) {
- Py_DECREF(ittuple);
- return NULL;
+ Py_ssize_t thislen = _PyObject_LengthHint(item, -2);
+ if (thislen < 0) {
+ if (thislen == -1)
+ return NULL;
+ len = -1;
+ break;
}
- PyTuple_SET_ITEM(ittuple, i, it);
+ else if (len < 0 || thislen < len)
+ len = thislen;
}
- /* create a result holder */
- result = PyTuple_New(tuplesize);
- if (result == NULL) {
- Py_DECREF(ittuple);
+ /* allocate result list */
+ if (len < 0)
+ len = 10; /* arbitrary */
+ if ((ret = PyList_New(len)) == NULL)
return NULL;
- }
- for (i=0 ; i < tuplesize ; i++) {
- Py_INCREF(Py_None);
- PyTuple_SET_ITEM(result, i, Py_None);
- }
-
- /* create zipobject structure */
- lz = (zipobject *)type->tp_alloc(type, 0);
- if (lz == NULL) {
- Py_DECREF(ittuple);
- Py_DECREF(result);
- return NULL;
- }
- lz->ittuple = ittuple;
- lz->tuplesize = tuplesize;
- lz->result = result;
- return (PyObject *)lz;
-}
-
-static void
-zip_dealloc(zipobject *lz)
-{
- PyObject_GC_UnTrack(lz);
- Py_XDECREF(lz->ittuple);
- Py_XDECREF(lz->result);
- Py_TYPE(lz)->tp_free(lz);
-}
-
-static int
-zip_traverse(zipobject *lz, visitproc visit, void *arg)
-{
- Py_VISIT(lz->ittuple);
- Py_VISIT(lz->result);
- return 0;
-}
-
-static PyObject *
-zip_next(zipobject *lz)
-{
- Py_ssize_t i;
- Py_ssize_t tuplesize = lz->tuplesize;
- PyObject *result = lz->result;
- PyObject *it;
- PyObject *item;
- PyObject *olditem;
-
- if (tuplesize == 0)
- return NULL;
- if (Py_REFCNT(result) == 1) {
- Py_INCREF(result);
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- item = (*Py_TYPE(it)->tp_iternext)(it);
- if (item == NULL) {
- Py_DECREF(result);
- return NULL;
+ /* obtain iterators */
+ itlist = PyTuple_New(itemsize);
+ if (itlist == NULL)
+ goto Fail_ret;
+ for (i = 0; i < itemsize; ++i) {
+ PyObject *item = PyTuple_GET_ITEM(args, i);
+ PyObject *it = PyObject_GetIter(item);
+ if (it == NULL) {
+ if (PyErr_ExceptionMatches(PyExc_TypeError))
+ PyErr_Format(PyExc_TypeError,
+ "zip argument #%zd must support iteration",
+ i+1);
+ goto Fail_ret_itlist;
+ }
+ PyTuple_SET_ITEM(itlist, i, it);
+ }
+
+ /* build result into ret list */
+ for (i = 0; ; ++i) {
+ int j;
+ PyObject *next = PyTuple_New(itemsize);
+ if (!next)
+ goto Fail_ret_itlist;
+
+ for (j = 0; j < itemsize; j++) {
+ PyObject *it = PyTuple_GET_ITEM(itlist, j);
+ PyObject *item = PyIter_Next(it);
+ if (!item) {
+ if (PyErr_Occurred()) {
+ Py_DECREF(ret);
+ ret = NULL;
+ }
+ Py_DECREF(next);
+ Py_DECREF(itlist);
+ goto Done;
}
- olditem = PyTuple_GET_ITEM(result, i);
- PyTuple_SET_ITEM(result, i, item);
- Py_DECREF(olditem);
+ PyTuple_SET_ITEM(next, j, item);
}
- } else {
- result = PyTuple_New(tuplesize);
- if (result == NULL)
- return NULL;
- for (i=0 ; i < tuplesize ; i++) {
- it = PyTuple_GET_ITEM(lz->ittuple, i);
- item = (*Py_TYPE(it)->tp_iternext)(it);
- if (item == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- PyTuple_SET_ITEM(result, i, item);
+
+ if (i < len)
+ PyList_SET_ITEM(ret, i, next);
+ else {
+ int status = PyList_Append(ret, next);
+ Py_DECREF(next);
+ ++len;
+ if (status < 0)
+ goto Fail_ret_itlist;
}
}
- return result;
-}
-static PyObject *
-zip_reduce(zipobject *lz, PyObject *Py_UNUSED(ignored))
-{
- /* Just recreate the zip with the internal iterator tuple */
- return Py_BuildValue("OO", Py_TYPE(lz), lz->ittuple);
+Done:
+ if (ret != NULL && i < len) {
+ /* The list is too big. */
+ if (PyList_SetSlice(ret, i, len, NULL) < 0)
+ return NULL;
+ }
+ return ret;
+
+Fail_ret_itlist:
+ Py_DECREF(itlist);
+Fail_ret:
+ Py_DECREF(ret);
+ return NULL;
}
-static PyMethodDef zip_methods[] = {
- {"__reduce__", (PyCFunction)zip_reduce, METH_NOARGS, reduce_doc},
- {NULL, NULL} /* sentinel */
-};
PyDoc_STRVAR(zip_doc,
-"zip(*iterables) --> zip object\n\
+"zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]\n\
\n\
-Return a zip object whose .__next__() method returns a tuple where\n\
-the i-th element comes from the i-th iterable argument. The .__next__()\n\
-method continues until the shortest iterable in the argument sequence\n\
-is exhausted and then it raises StopIteration.");
-
-PyTypeObject PyZip_Type = {
- PyVarObject_HEAD_INIT(&PyType_Type, 0)
- "zip", /* tp_name */
- sizeof(zipobject), /* tp_basicsize */
- 0, /* tp_itemsize */
- /* methods */
- (destructor)zip_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
- 0, /* tp_getattr */
- 0, /* tp_setattr */
- 0, /* tp_as_async */
- 0, /* tp_repr */
- 0, /* tp_as_number */
- 0, /* tp_as_sequence */
- 0, /* tp_as_mapping */
- 0, /* tp_hash */
- 0, /* tp_call */
- 0, /* tp_str */
- PyObject_GenericGetAttr, /* tp_getattro */
- 0, /* tp_setattro */
- 0, /* tp_as_buffer */
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
- Py_TPFLAGS_BASETYPE, /* tp_flags */
- zip_doc, /* tp_doc */
- (traverseproc)zip_traverse, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- PyObject_SelfIter, /* tp_iter */
- (iternextfunc)zip_next, /* tp_iternext */
- zip_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- PyType_GenericAlloc, /* tp_alloc */
- zip_new, /* tp_new */
- PyObject_GC_Del, /* tp_free */
-};
+Return a list of tuples, where each tuple contains the i-th element\n\
+from each of the argument sequences. The returned list is truncated\n\
+in length to the length of the shortest argument sequence.");
static PyMethodDef builtin_methods[] = {
- {"__build_class__", (PyCFunction)(void(*)(void))builtin___build_class__,
- METH_FASTCALL | METH_KEYWORDS, build_class_doc},
- {"__import__", (PyCFunction)(void(*)(void))builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
- BUILTIN_ABS_METHODDEF
- BUILTIN_ALL_METHODDEF
- BUILTIN_ANY_METHODDEF
- BUILTIN_ASCII_METHODDEF
- BUILTIN_BIN_METHODDEF
- {"breakpoint", (PyCFunction)(void(*)(void))builtin_breakpoint, METH_FASTCALL | METH_KEYWORDS, breakpoint_doc},
- BUILTIN_CALLABLE_METHODDEF
- BUILTIN_CHR_METHODDEF
- BUILTIN_COMPILE_METHODDEF
- BUILTIN_DELATTR_METHODDEF
+ {"__import__", (PyCFunction)builtin___import__, METH_VARARGS | METH_KEYWORDS, import_doc},
+ {"abs", builtin_abs, METH_O, abs_doc},
+ {"all", builtin_all, METH_O, all_doc},
+ {"any", builtin_any, METH_O, any_doc},
+ {"apply", builtin_apply, METH_VARARGS, apply_doc},
+ {"bin", builtin_bin, METH_O, bin_doc},
+ {"callable", builtin_callable, METH_O, callable_doc},
+ {"chr", builtin_chr, METH_VARARGS, chr_doc},
+ {"cmp", builtin_cmp, METH_VARARGS, cmp_doc},
+ {"coerce", builtin_coerce, METH_VARARGS, coerce_doc},
+ {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc},
+ {"delattr", builtin_delattr, METH_VARARGS, delattr_doc},
{"dir", builtin_dir, METH_VARARGS, dir_doc},
- BUILTIN_DIVMOD_METHODDEF
- BUILTIN_EVAL_METHODDEF
- BUILTIN_EXEC_METHODDEF
- BUILTIN_FORMAT_METHODDEF
- {"getattr", (PyCFunction)(void(*)(void))builtin_getattr, METH_FASTCALL, getattr_doc},
- BUILTIN_GLOBALS_METHODDEF
- BUILTIN_HASATTR_METHODDEF
- BUILTIN_HASH_METHODDEF
- BUILTIN_HEX_METHODDEF
- BUILTIN_ID_METHODDEF
- BUILTIN_INPUT_METHODDEF
- BUILTIN_ISINSTANCE_METHODDEF
- BUILTIN_ISSUBCLASS_METHODDEF
- {"iter", (PyCFunction)(void(*)(void))builtin_iter, METH_FASTCALL, iter_doc},
- BUILTIN_LEN_METHODDEF
- BUILTIN_LOCALS_METHODDEF
- {"max", (PyCFunction)(void(*)(void))builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
- {"min", (PyCFunction)(void(*)(void))builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
- {"next", (PyCFunction)(void(*)(void))builtin_next, METH_FASTCALL, next_doc},
- BUILTIN_OCT_METHODDEF
- BUILTIN_ORD_METHODDEF
- BUILTIN_POW_METHODDEF
- {"print", (PyCFunction)(void(*)(void))builtin_print, METH_FASTCALL | METH_KEYWORDS, print_doc},
- BUILTIN_REPR_METHODDEF
- BUILTIN_ROUND_METHODDEF
- BUILTIN_SETATTR_METHODDEF
- BUILTIN_SORTED_METHODDEF
- BUILTIN_SUM_METHODDEF
+ {"divmod", builtin_divmod, METH_VARARGS, divmod_doc},
+ {"eval", builtin_eval, METH_VARARGS, eval_doc},
+ {"execfile", builtin_execfile, METH_VARARGS, execfile_doc},
+ {"filter", builtin_filter, METH_VARARGS, filter_doc},
+ {"format", builtin_format, METH_VARARGS, format_doc},
+ {"getattr", builtin_getattr, METH_VARARGS, getattr_doc},
+ {"globals", (PyCFunction)builtin_globals, METH_NOARGS, globals_doc},
+ {"hasattr", builtin_hasattr, METH_VARARGS, hasattr_doc},
+ {"hash", builtin_hash, METH_O, hash_doc},
+ {"hex", builtin_hex, METH_O, hex_doc},
+ {"id", builtin_id, METH_O, id_doc},
+ {"input", builtin_input, METH_VARARGS, input_doc},
+ {"intern", builtin_intern, METH_VARARGS, intern_doc},
+ {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc},
+ {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_doc},
+ {"iter", builtin_iter, METH_VARARGS, iter_doc},
+ {"len", builtin_len, METH_O, len_doc},
+ {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc},
+ {"map", builtin_map, METH_VARARGS, map_doc},
+ {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc},
+ {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc},
+ {"next", builtin_next, METH_VARARGS, next_doc},
+ {"oct", builtin_oct, METH_O, oct_doc},
+ {"open", (PyCFunction)builtin_open, METH_VARARGS | METH_KEYWORDS, open_doc},
+ {"ord", builtin_ord, METH_O, ord_doc},
+ {"pow", builtin_pow, METH_VARARGS, pow_doc},
+ {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc},
+ {"range", builtin_range, METH_VARARGS, range_doc},
+ {"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
+ {"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
+ {"reload", builtin_reload, METH_O, reload_doc},
+ {"repr", builtin_repr, METH_O, repr_doc},
+ {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc},
+ {"setattr", builtin_setattr, METH_VARARGS, setattr_doc},
+ {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc},
+ {"sum", builtin_sum, METH_VARARGS, sum_doc},
+#ifdef Py_USING_UNICODE
+ {"unichr", builtin_unichr, METH_VARARGS, unichr_doc},
+#endif
{"vars", builtin_vars, METH_VARARGS, vars_doc},
+ {"zip", builtin_zip, METH_VARARGS, zip_doc},
{NULL, NULL},
};
@@ -2742,38 +2686,19 @@ PyDoc_STRVAR(builtin_doc,
\n\
Noteworthy: None is the `nil' object; Ellipsis represents `...' in slices.");
-static struct PyModuleDef builtinsmodule = {
- PyModuleDef_HEAD_INIT,
- "builtins",
- builtin_doc,
- -1, /* multiple "initialization" just copies the module dict. */
- builtin_methods,
- NULL,
- NULL,
- NULL,
- NULL
-};
-
-
PyObject *
-_PyBuiltin_Init(PyThreadState *tstate)
+_PyBuiltin_Init(void)
{
PyObject *mod, *dict, *debug;
-
- const PyConfig *config = &tstate->interp->config;
-
- if (PyType_Ready(&PyFilter_Type) < 0 ||
- PyType_Ready(&PyMap_Type) < 0 ||
- PyType_Ready(&PyZip_Type) < 0)
- return NULL;
-
- mod = _PyModule_CreateInitialized(&builtinsmodule, PYTHON_API_VERSION);
+ mod = Py_InitModule4("__builtin__", builtin_methods,
+ builtin_doc, (PyObject *)NULL,
+ PYTHON_API_VERSION);
if (mod == NULL)
return NULL;
dict = PyModule_GetDict(mod);
#ifdef Py_TRACE_REFS
- /* "builtins" exposes a number of statically allocated objects
+ /* __builtin__ exposes a number of statically allocated objects
* that, before this code was added in 2.3, never showed up in
* the list of "all objects" maintained by Py_TRACE_REFS. As a
* result, programs leaking references to None and False (etc)
@@ -2794,40 +2719,364 @@ _PyBuiltin_Init(PyThreadState *tstate)
SETBUILTIN("NotImplemented", Py_NotImplemented);
SETBUILTIN("False", Py_False);
SETBUILTIN("True", Py_True);
+ SETBUILTIN("basestring", &PyBaseString_Type);
SETBUILTIN("bool", &PyBool_Type);
SETBUILTIN("memoryview", &PyMemoryView_Type);
SETBUILTIN("bytearray", &PyByteArray_Type);
- SETBUILTIN("bytes", &PyBytes_Type);
+ SETBUILTIN("bytes", &PyString_Type);
+ SETBUILTIN("buffer", &PyBuffer_Type);
SETBUILTIN("classmethod", &PyClassMethod_Type);
+#ifndef WITHOUT_COMPLEX
SETBUILTIN("complex", &PyComplex_Type);
+#endif
SETBUILTIN("dict", &PyDict_Type);
SETBUILTIN("enumerate", &PyEnum_Type);
- SETBUILTIN("filter", &PyFilter_Type);
+ SETBUILTIN("file", &PyFile_Type);
SETBUILTIN("float", &PyFloat_Type);
SETBUILTIN("frozenset", &PyFrozenSet_Type);
SETBUILTIN("property", &PyProperty_Type);
- SETBUILTIN("int", &PyLong_Type);
+ SETBUILTIN("int", &PyInt_Type);
SETBUILTIN("list", &PyList_Type);
- SETBUILTIN("map", &PyMap_Type);
+ SETBUILTIN("long", &PyLong_Type);
SETBUILTIN("object", &PyBaseObject_Type);
- SETBUILTIN("range", &PyRange_Type);
SETBUILTIN("reversed", &PyReversed_Type);
SETBUILTIN("set", &PySet_Type);
SETBUILTIN("slice", &PySlice_Type);
SETBUILTIN("staticmethod", &PyStaticMethod_Type);
- SETBUILTIN("str", &PyUnicode_Type);
+ SETBUILTIN("str", &PyString_Type);
SETBUILTIN("super", &PySuper_Type);
SETBUILTIN("tuple", &PyTuple_Type);
SETBUILTIN("type", &PyType_Type);
- SETBUILTIN("zip", &PyZip_Type);
- debug = PyBool_FromLong(config->optimization_level == 0);
+ SETBUILTIN("xrange", &PyRange_Type);
+#ifdef Py_USING_UNICODE
+ SETBUILTIN("unicode", &PyUnicode_Type);
+#endif
+ debug = PyBool_FromLong(Py_OptimizeFlag == 0);
if (PyDict_SetItemString(dict, "__debug__", debug) < 0) {
- Py_DECREF(debug);
+ Py_XDECREF(debug);
return NULL;
}
- Py_DECREF(debug);
+ Py_XDECREF(debug);
return mod;
#undef ADD_TO_ALL
#undef SETBUILTIN
}
+
+/* Helper for filter(): filter a tuple through a function */
+
+static PyObject *
+filtertuple(PyObject *func, PyObject *tuple)
+{
+ PyObject *result;
+ Py_ssize_t i, j;
+ Py_ssize_t len = PyTuple_Size(tuple);
+
+ if (len == 0) {
+ if (PyTuple_CheckExact(tuple))
+ Py_INCREF(tuple);
+ else
+ tuple = PyTuple_New(0);
+ return tuple;
+ }
+
+ if ((result = PyTuple_New(len)) == NULL)
+ return NULL;
+
+ for (i = j = 0; i < len; ++i) {
+ PyObject *item, *good;
+ int ok;
+
+ if (tuple->ob_type->tp_as_sequence &&
+ tuple->ob_type->tp_as_sequence->sq_item) {
+ item = tuple->ob_type->tp_as_sequence->sq_item(tuple, i);
+ if (item == NULL)
+ goto Fail_1;
+ } else {
+ PyErr_SetString(PyExc_TypeError, "filter(): unsubscriptable tuple");
+ goto Fail_1;
+ }
+ if (func == Py_None) {
+ Py_INCREF(item);
+ good = item;
+ }
+ else {
+ PyObject *arg = PyTuple_Pack(1, item);
+ if (arg == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ good = PyEval_CallObject(func, arg);
+ Py_DECREF(arg);
+ if (good == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ if (ok > 0) {
+ if (PyTuple_SetItem(result, j++, item) < 0)
+ goto Fail_1;
+ }
+ else {
+ Py_DECREF(item);
+ if (ok < 0)
+ goto Fail_1;
+ }
+ }
+
+ if (_PyTuple_Resize(&result, j) < 0)
+ return NULL;
+
+ return result;
+
+Fail_1:
+ Py_DECREF(result);
+ return NULL;
+}
+
+
+/* Helper for filter(): filter a string through a function */
+
+static PyObject *
+filterstring(PyObject *func, PyObject *strobj)
+{
+ PyObject *result;
+ Py_ssize_t i, j;
+ Py_ssize_t len = PyString_Size(strobj);
+ Py_ssize_t outlen = len;
+
+ if (func == Py_None) {
+ /* If it's a real string we can return the original,
+ * as no character is ever false and __getitem__
+ * does return this character. If it's a subclass
+ * we must go through the __getitem__ loop */
+ if (PyString_CheckExact(strobj)) {
+ Py_INCREF(strobj);
+ return strobj;
+ }
+ }
+ if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
+ return NULL;
+
+ for (i = j = 0; i < len; ++i) {
+ PyObject *item;
+ int ok;
+
+ item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
+ if (item == NULL)
+ goto Fail_1;
+ if (func==Py_None) {
+ ok = 1;
+ } else {
+ PyObject *arg, *good;
+ arg = PyTuple_Pack(1, item);
+ if (arg == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ good = PyEval_CallObject(func, arg);
+ Py_DECREF(arg);
+ if (good == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ }
+ if (ok > 0) {
+ Py_ssize_t reslen;
+ if (!PyString_Check(item)) {
+ PyErr_SetString(PyExc_TypeError, "can't filter str to str:"
+ " __getitem__ returned different type");
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ reslen = PyString_GET_SIZE(item);
+ if (reslen == 1) {
+ PyString_AS_STRING(result)[j++] =
+ PyString_AS_STRING(item)[0];
+ } else {
+ /* do we need more space? */
+ Py_ssize_t need = j;
+
+ /* calculate space requirements while checking for overflow */
+ if (need > PY_SSIZE_T_MAX - reslen) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+
+ need += reslen;
+
+ if (need > PY_SSIZE_T_MAX - len) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+
+ need += len;
+
+ if (need <= i) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+
+ need = need - i - 1;
+
+ assert(need >= 0);
+ assert(outlen >= 0);
+
+ if (need > outlen) {
+ /* overallocate, to avoid reallocations */
+ if (outlen > PY_SSIZE_T_MAX / 2) {
+ Py_DECREF(item);
+ return NULL;
+ }
+
+ if (need<2*outlen) {
+ need = 2*outlen;
+ }
+ if (_PyString_Resize(&result, need)) {
+ Py_DECREF(item);
+ return NULL;
+ }
+ outlen = need;
+ }
+ memcpy(
+ PyString_AS_STRING(result) + j,
+ PyString_AS_STRING(item),
+ reslen
+ );
+ j += reslen;
+ }
+ }
+ Py_DECREF(item);
+ if (ok < 0)
+ goto Fail_1;
+ }
+
+ if (j < outlen)
+ _PyString_Resize(&result, j);
+
+ return result;
+
+Fail_1:
+ Py_DECREF(result);
+ return NULL;
+}
+
+#ifdef Py_USING_UNICODE
+/* Helper for filter(): filter a Unicode object through a function */
+
+static PyObject *
+filterunicode(PyObject *func, PyObject *strobj)
+{
+ PyObject *result;
+ register Py_ssize_t i, j;
+ Py_ssize_t len = PyUnicode_GetSize(strobj);
+ Py_ssize_t outlen = len;
+
+ if (func == Py_None) {
+ /* If it's a real string we can return the original,
+ * as no character is ever false and __getitem__
+ * does return this character. If it's a subclass
+ * we must go through the __getitem__ loop */
+ if (PyUnicode_CheckExact(strobj)) {
+ Py_INCREF(strobj);
+ return strobj;
+ }
+ }
+ if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
+ return NULL;
+
+ for (i = j = 0; i < len; ++i) {
+ PyObject *item, *arg, *good;
+ int ok;
+
+ item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
+ if (item == NULL)
+ goto Fail_1;
+ if (func == Py_None) {
+ ok = 1;
+ } else {
+ arg = PyTuple_Pack(1, item);
+ if (arg == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ good = PyEval_CallObject(func, arg);
+ Py_DECREF(arg);
+ if (good == NULL) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ ok = PyObject_IsTrue(good);
+ Py_DECREF(good);
+ }
+ if (ok > 0) {
+ Py_ssize_t reslen;
+ if (!PyUnicode_Check(item)) {
+ PyErr_SetString(PyExc_TypeError,
+ "can't filter unicode to unicode:"
+ " __getitem__ returned different type");
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ reslen = PyUnicode_GET_SIZE(item);
+ if (reslen == 1)
+ PyUnicode_AS_UNICODE(result)[j++] =
+ PyUnicode_AS_UNICODE(item)[0];
+ else {
+ /* do we need more space? */
+ Py_ssize_t need = j + reslen + len - i - 1;
+
+ /* check that didnt overflow */
+ if ((j > PY_SSIZE_T_MAX - reslen) ||
+ ((j + reslen) > PY_SSIZE_T_MAX - len) ||
+ ((j + reslen + len) < i) ||
+ ((j + reslen + len - i) <= 0)) {
+ Py_DECREF(item);
+ return NULL;
+ }
+
+ assert(need >= 0);
+ assert(outlen >= 0);
+
+ if (need > outlen) {
+ /* overallocate, to avoid reallocations */
+ if (need < 2 * outlen) {
+ if (outlen > PY_SSIZE_T_MAX / 2) {
+ Py_DECREF(item);
+ return NULL;
+ } else {
+ need = 2 * outlen;
+ }
+ }
+
+ if (PyUnicode_Resize(&result, need) < 0) {
+ Py_DECREF(item);
+ goto Fail_1;
+ }
+ outlen = need;
+ }
+ memcpy(PyUnicode_AS_UNICODE(result) + j,
+ PyUnicode_AS_UNICODE(item),
+ reslen*sizeof(Py_UNICODE));
+ j += reslen;
+ }
+ }
+ Py_DECREF(item);
+ if (ok < 0)
+ goto Fail_1;
+ }
+
+ if (j < outlen)
+ PyUnicode_Resize(&result, j);
+
+ return result;
+
+Fail_1:
+ Py_DECREF(result);
+ return NULL;
+}
+#endif