diff -r de2b96f220a5 Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py Sat Jan 25 00:00:28 2014 -0500 +++ b/Lib/test/test_inspect.py Sun Jan 26 01:02:16 2014 +1000 @@ -1,4 +1,5 @@ import _testcapi +import builtins import collections import datetime import functools @@ -1650,6 +1651,62 @@ __call__ = type test_callable(ThisWorksNow()) + @unittest.skipIf(MISSING_C_DOCSTRINGS, + "Signature information for builtins requires docstrings") + def test_builtins_have_signatures(self): + # This checks all builtin callables have signatures + # A few have signatures Signature can't yet handle, so we skip those + # since they will have to until until PEP 457 adds the required + # introspection support in Python 3.5 + needs_groups = ["range", "slice", "dir", "getattr", + "next", "iter", "vars"] + # Others need either group support or a semantic change + needs_semantic_update = ["round"] + # Argument Clinic can't handle varargs yet... + needs_varargs = ["min", "max", "print", "__build_class__"] + # ... or bools as defaults for integer parameters + needs_bool_as_int_default = ["sorted"] + # Not all builtins are implemented in the builtins module itself... + implemented_elsewhere = ["open"] + # Check the signatures we expect to be there + ns = vars(builtins) + for name, obj in sorted(ns.items()): + if not callable(obj): + continue + # These are implemented outside the builtins module + if (name in implemented_elsewhere): + # Converting these is out of scope for this initial patch + continue + # Also postpone converting any types for the moment + if isinstance(obj, type): + # Note that this also skips all the exception types + continue + # These can't be changed until Argument Clinic supports them + if name in needs_bool_as_int_default: + # Postponed until AC allows bools as int defaults + continue + if name in needs_varargs: + # Postponed until AC handles varargs + continue + # These can't be changed until Python 3.5 + if name in needs_groups: + # Postponed until PEP 457 is introduced in 3.5 + continue + if name in needs_semantic_update: + # Postponed since adapting to Argument Clinic involves a + # semantic change (e.g. accepting None as equivalent to + # the arg not being passed in at all) + continue + with self.subTest(builtin=name): + self.assertIsNotNone(inspect.signature(obj)) + # Check functions that can't be represented don't claim a signature + no_signature = needs_groups + needs_varargs + for name in no_signature: + # Can't test this yet, see http://bugs.python.org/issue20326 + return + with self.subTest(builtin=name): + self.assertIsNone(ns[name].__text_signature__) + def test_signature_on_builtins_no_signature(self): with self.assertRaisesRegex(ValueError, 'no signature found for builtin'): diff -r de2b96f220a5 Python/bltinmodule.c --- a/Python/bltinmodule.c Sat Jan 25 00:00:28 2014 -0500 +++ b/Python/bltinmodule.c Sun Jan 26 01:02:16 2014 +1000 @@ -46,6 +46,7 @@ _Py_IDENTIFIER(stdout); _Py_IDENTIFIER(stderr); +/* AC 3.4: waiting for *args support */ static PyObject * builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) { @@ -229,25 +230,58 @@ is the number of parent directories to search relative to the current module."); +/*[clinic input] +abs as builtin_abs + + x: 'O' + / + +Return the absolute value of the argument. +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_abs__doc__, +"abs(module, x)\n" +"Return the absolute value of the argument."); + +#define BUILTIN_ABS_METHODDEF \ + {"abs", (PyCFunction)builtin_abs, METH_O, builtin_abs__doc__}, + static PyObject * -builtin_abs(PyObject *self, PyObject *v) +builtin_abs(PyModuleDef *module, PyObject *x) +/*[clinic end generated code: checksum=39a49de51b4714d5ea858c85075f4f422b208af1]*/ { - return PyNumber_Absolute(v); + return PyNumber_Absolute(x); } -PyDoc_STRVAR(abs_doc, -"abs(number) -> number\n\ -\n\ -Return the absolute value of the argument."); +/*[clinic input] +all as builtin_all + + iterable: 'O' + / + +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(builtin_all__doc__, +"all(module, iterable)\n" +"Return True if bool(x) is True for all values x in the iterable.\n" +"\n" +"If the iterable is empty, return True."); + +#define BUILTIN_ALL_METHODDEF \ + {"all", (PyCFunction)builtin_all, METH_O, builtin_all__doc__}, static PyObject * -builtin_all(PyObject *self, PyObject *v) +builtin_all(PyModuleDef *module, PyObject *iterable) +/*[clinic end generated code: checksum=0a79c5483de04809e4176eb5ca46685c2443e657]*/ { PyObject *it, *item; PyObject *(*iternext)(PyObject *); int cmp; - it = PyObject_GetIter(v); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; iternext = *Py_TYPE(it)->tp_iternext; @@ -277,20 +311,35 @@ Py_RETURN_TRUE; } -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."); +/*[clinic input] +any as builtin_any + + iterable: 'O' + / + +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(builtin_any__doc__, +"any(module, iterable)\n" +"Return True if bool(x) is True for any x in the iterable.\n" +"\n" +"If the iterable is empty, return False."); + +#define BUILTIN_ANY_METHODDEF \ + {"any", (PyCFunction)builtin_any, METH_O, builtin_any__doc__}, static PyObject * -builtin_any(PyObject *self, PyObject *v) +builtin_any(PyModuleDef *module, PyObject *iterable) +/*[clinic end generated code: checksum=401a08de3cf8403ea725fa4d65c847f3a5053342]*/ { PyObject *it, *item; PyObject *(*iternext)(PyObject *); int cmp; - it = PyObject_GetIter(v); + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; iternext = *Py_TYPE(it)->tp_iternext; @@ -320,56 +369,99 @@ Py_RETURN_FALSE; } -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."); +/*[clinic input] +ascii as builtin_ascii + + obj: 'O' + / + +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(builtin_ascii__doc__, +"ascii(module, obj)\n" +"Return an ASCII-only representation of an object.\n" +"\n" +"As repr(), return a string containing a printable representation of an\n" +"object, but escape the non-ASCII characters in the string returned by\n" +"repr() using \\x, \\u or \\U escapes. This generates a string similar\n" +"to that returned by repr() in Python 2."); + +#define BUILTIN_ASCII_METHODDEF \ + {"ascii", (PyCFunction)builtin_ascii, METH_O, builtin_ascii__doc__}, static PyObject * -builtin_ascii(PyObject *self, PyObject *v) +builtin_ascii(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=21f0cea0778ae15ce90089eb1051c46067a3de07]*/ { - return PyObject_ASCII(v); + return PyObject_ASCII(obj); } -PyDoc_STRVAR(ascii_doc, -"ascii(object) -> string\n\ -\n\ -As repr(), return a string containing a printable representation of an\n\ -object, but escape the non-ASCII characters in the string returned by\n\ -repr() using \\x, \\u or \\U escapes. This generates a string similar\n\ -to that returned by repr() in Python 2."); - + +/*[clinic input] +bin as builtin_bin + + number: 'O' + / + +Return the binary representation of an integer. + + >>> bin(2796202) + '0b1010101010101010101010' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_bin__doc__, +"bin(module, number)\n" +"Return the binary representation of an integer.\n" +"\n" +" >>> bin(2796202)\n" +" \'0b1010101010101010101010\'"); + +#define BUILTIN_BIN_METHODDEF \ + {"bin", (PyCFunction)builtin_bin, METH_O, builtin_bin__doc__}, static PyObject * -builtin_bin(PyObject *self, PyObject *v) +builtin_bin(PyModuleDef *module, PyObject *number) +/*[clinic end generated code: checksum=fdfb9aca6f7bd21ce19110503416ba2d2b38feab]*/ { - return PyNumber_ToBase(v, 2); + return PyNumber_ToBase(number, 2); } -PyDoc_STRVAR(bin_doc, -"bin(number) -> string\n\ -\n\ -Return the binary representation of an integer.\n\ -\n\ - >>> bin(2796202)\n\ - '0b1010101010101010101010'\n\ -"); - + +/*[clinic input] +callable as builtin_callable + + obj: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_callable__doc__, +"callable(module, obj)\n" +"Return whether the object is callable (i.e., some kind of function).\n" +"\n" +"Note that classes are callable, as are instances of classes with a\n" +"__call__() method."); + +#define BUILTIN_CALLABLE_METHODDEF \ + {"callable", (PyCFunction)builtin_callable, METH_O, builtin_callable__doc__}, static PyObject * -builtin_callable(PyObject *self, PyObject *v) +builtin_callable(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=007ebacf39f1bbca5e227e2aa09e2f3556e10a54]*/ { - return PyBool_FromLong((long)PyCallable_Check(v)); + return PyBool_FromLong((long)PyCallable_Check(obj)); } -PyDoc_STRVAR(callable_doc, -"callable(object) -> bool\n\ -\n\ -Return whether the object is callable (i.e., some kind of function).\n\ -Note that classes are callable, as are instances of classes with a\n\ -__call__() method."); - typedef struct { PyObject_HEAD @@ -524,39 +616,101 @@ }; +/*[clinic input] +format as builtin_format + + value: 'O' + format_spec: unicode(c_default="NULL") = '' + / + +Return value.__format__(format_spec) + +format_spec defaults to the empty string +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_format__doc__, +"format(module, value, format_spec=\'\')\n" +"Return value.__format__(format_spec)\n" +"\n" +"format_spec defaults to the empty string"); + +#define BUILTIN_FORMAT_METHODDEF \ + {"format", (PyCFunction)builtin_format, METH_VARARGS, builtin_format__doc__}, + static PyObject * -builtin_format(PyObject *self, PyObject *args) +builtin_format_impl(PyModuleDef *module, PyObject *value, PyObject *format_spec); + +static PyObject * +builtin_format(PyModuleDef *module, PyObject *args) { + PyObject *return_value = NULL; PyObject *value; PyObject *format_spec = NULL; - if (!PyArg_ParseTuple(args, "O|U:format", &value, &format_spec)) - return NULL; - + if (!PyArg_ParseTuple(args, + "O|U:format", + &value, &format_spec)) + goto exit; + return_value = builtin_format_impl(module, value, format_spec); + +exit: + return return_value; +} + +static PyObject * +builtin_format_impl(PyModuleDef *module, PyObject *value, PyObject *format_spec) +/*[clinic end generated code: checksum=153e39f2edf84b28ff17bbb62d16de545920797e]*/ +{ return PyObject_Format(value, format_spec); } -PyDoc_STRVAR(format_doc, -"format(value[, format_spec]) -> string\n\ -\n\ -Returns value.__format__(format_spec)\n\ -format_spec defaults to \"\""); +/*[clinic input] +chr as builtin_chr + + i: 'i' + / + +Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff. + + >>> chr(12491) + 'ニ' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_chr__doc__, +"chr(module, i)\n" +"Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.\n" +"\n" +" >>> chr(12491)\n" +" \'ニ\'"); + +#define BUILTIN_CHR_METHODDEF \ + {"chr", (PyCFunction)builtin_chr, METH_VARARGS, builtin_chr__doc__}, static PyObject * -builtin_chr(PyObject *self, PyObject *args) +builtin_chr_impl(PyModuleDef *module, int i); + +static PyObject * +builtin_chr(PyModuleDef *module, PyObject *args) { - int x; - - if (!PyArg_ParseTuple(args, "i:chr", &x)) - return NULL; - - return PyUnicode_FromOrdinal(x); + PyObject *return_value = NULL; + int i; + + if (!PyArg_ParseTuple(args, + "i:chr", + &i)) + goto exit; + return_value = builtin_chr_impl(module, i); + +exit: + return return_value; } -PyDoc_STRVAR(chr_doc, -"chr(i) -> Unicode character\n\ -\n\ -Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff."); +static PyObject * +builtin_chr_impl(PyModuleDef *module, int i) +/*[clinic end generated code: checksum=3975cf2991166ab7f3322b9e557937d3a2906a2d]*/ +{ + return PyUnicode_FromOrdinal(i); +} static char * @@ -589,34 +743,87 @@ return str; } +/*[clinic input] +compile as builtin_compile + + source: 'O' + filename: object(converter="PyUnicode_FSDecoder") + mode: 's' + flags: 'i' = 0 + dont_inherit: 'i' = 0 + optimize: 'i' = -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 non-zero, stops the compilation inheriting +the effects of any future statements in effect in the code calling +compile; if absent or zero these statements do influence the compilation, +in addition to any features explicitly specified. +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_compile__doc__, +"compile(module, source, filename, mode, flags=0, dont_inherit=0, optimize=-1)\n" +"Compile source into a code object that can be executed by exec() or eval().\n" +"\n" +"The source code may represent a Python module, statement or expression.\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."); + +#define BUILTIN_COMPILE_METHODDEF \ + {"compile", (PyCFunction)builtin_compile, METH_VARARGS|METH_KEYWORDS, builtin_compile__doc__}, + static PyObject * -builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) +builtin_compile_impl(PyModuleDef *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, int optimize); + +static PyObject * +builtin_compile(PyModuleDef *module, PyObject *args, PyObject *kwargs) +{ + PyObject *return_value = NULL; + static char *_keywords[] = {"source", "filename", "mode", "flags", "dont_inherit", "optimize", NULL}; + PyObject *source; + PyObject *filename; + const char *mode; + int flags = 0; + int dont_inherit = 0; + int optimize = -1; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "OO&s|iii:compile", _keywords, + &source, PyUnicode_FSDecoder, &filename, &mode, &flags, &dont_inherit, &optimize)) + goto exit; + return_value = builtin_compile_impl(module, source, filename, mode, flags, dont_inherit, optimize); + +exit: + return return_value; +} + +static PyObject * +builtin_compile_impl(PyModuleDef *module, PyObject *source, PyObject *filename, const char *mode, int flags, int dont_inherit, int optimize) +/*[clinic end generated code: checksum=a1d9dae93c8ae0044425dc7abb6f7fbabcc960f8]*/ { char *str; - PyObject *filename; - char *startstr; - int mode = -1; - int dont_inherit = 0; - int supplied_flags = 0; - int optimize = -1; + int compile_mode = -1; int is_ast; PyCompilerFlags cf; - PyObject *cmd; - static char *kwlist[] = {"source", "filename", "mode", "flags", - "dont_inherit", "optimize", NULL}; int start[] = {Py_file_input, Py_eval_input, Py_single_input}; PyObject *result; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&s|iii:compile", kwlist, - &cmd, - PyUnicode_FSDecoder, &filename, - &startstr, &supplied_flags, - &dont_inherit, &optimize)) - return NULL; - - cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8; - - if (supplied_flags & + cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; + + if (flags & ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) { PyErr_SetString(PyExc_ValueError, @@ -635,25 +842,25 @@ PyEval_MergeCompilerFlags(&cf); } - if (strcmp(startstr, "exec") == 0) - mode = 0; - else if (strcmp(startstr, "eval") == 0) - mode = 1; - else if (strcmp(startstr, "single") == 0) - mode = 2; + 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 { PyErr_SetString(PyExc_ValueError, - "compile() arg 3 must be 'exec', 'eval' or 'single'"); + "compile() mode must be 'exec', 'eval' or 'single'"); goto error; } - is_ast = PyAST_Check(cmd); + is_ast = PyAST_Check(source); if (is_ast == -1) goto error; if (is_ast) { - if (supplied_flags & PyCF_ONLY_AST) { - Py_INCREF(cmd); - result = cmd; + if (flags & PyCF_ONLY_AST) { + Py_INCREF(source); + result = source; } else { PyArena *arena; @@ -662,7 +869,7 @@ arena = PyArena_New(); if (arena == NULL) goto error; - mod = PyAST_obj2mod(cmd, arena, mode); + mod = PyAST_obj2mod(source, arena, compile_mode); if (mod == NULL) { PyArena_Free(arena); goto error; @@ -678,11 +885,11 @@ goto finally; } - str = source_as_string(cmd, "compile", "string, bytes or AST", &cf); + str = source_as_string(source, "compile", "string, bytes or AST", &cf); if (str == NULL) goto error; - result = Py_CompileStringObject(str, filename, start[mode], &cf, optimize); + result = Py_CompileStringObject(str, filename, start[compile_mode], &cf, optimize); goto finally; error: @@ -692,21 +899,7 @@ return result; } -PyDoc_STRVAR(compile_doc, -"compile(source, filename, mode[, flags[, dont_inherit]]) -> code object\n\ -\n\ -Compile the source (a Python module, statement or expression)\n\ -into a code object that can be executed by exec() 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."); - +/* AC 3.5: cannot convert yet, as needs PEP 457 group support */ static PyObject * builtin_dir(PyObject *self, PyObject *args) { @@ -731,32 +924,110 @@ " 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: 'O' + y: 'O' + / + +Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_divmod__doc__, +"divmod(module, x, y)\n" +"Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x."); + +#define BUILTIN_DIVMOD_METHODDEF \ + {"divmod", (PyCFunction)builtin_divmod, METH_VARARGS, builtin_divmod__doc__}, + static PyObject * -builtin_divmod(PyObject *self, PyObject *args) +builtin_divmod_impl(PyModuleDef *module, PyObject *x, PyObject *y); + +static PyObject * +builtin_divmod(PyModuleDef *module, PyObject *args) { - PyObject *v, *w; - - if (!PyArg_UnpackTuple(args, "divmod", 2, 2, &v, &w)) - return NULL; - return PyNumber_Divmod(v, w); + PyObject *return_value = NULL; + PyObject *x; + PyObject *y; + + if (!PyArg_UnpackTuple(args, "divmod", + 2, 2, + &x, &y)) + goto exit; + return_value = builtin_divmod_impl(module, x, y); + +exit: + return return_value; } -PyDoc_STRVAR(divmod_doc, -"divmod(x, y) -> (div, mod)\n\ -\n\ -Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x."); - - static PyObject * -builtin_eval(PyObject *self, PyObject *args) +builtin_divmod_impl(PyModuleDef *module, PyObject *x, PyObject *y) +/*[clinic end generated code: checksum=11304f91a0832c6df4fc55f703c0c54777f12ac0]*/ { - PyObject *cmd, *result, *tmp = NULL; - PyObject *globals = Py_None, *locals = Py_None; + return PyNumber_Divmod(x, y); +} + + +/*[clinic input] +eval as builtin_eval + + source: 'O' + globals: 'O' = None + locals: 'O' = None + / + +Evaluate the given source in the context of globals and locals. + +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]*/ + +PyDoc_STRVAR(builtin_eval__doc__, +"eval(module, source, globals=None, locals=None)\n" +"Evaluate the given source in the context of globals and locals.\n" +"\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."); + +#define BUILTIN_EVAL_METHODDEF \ + {"eval", (PyCFunction)builtin_eval, METH_VARARGS, builtin_eval__doc__}, + +static PyObject * +builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyObject *locals); + +static PyObject * +builtin_eval(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *source; + PyObject *globals = Py_None; + PyObject *locals = Py_None; + + if (!PyArg_UnpackTuple(args, "eval", + 1, 3, + &source, &globals, &locals)) + goto exit; + return_value = builtin_eval_impl(module, source, globals, locals); + +exit: + return return_value; +} + +static PyObject * +builtin_eval_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyObject *locals) +/*[clinic end generated code: checksum=f01e5814b7067bb188e278d61d578a29b1372104]*/ +{ + PyObject *result, *tmp = NULL; 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; @@ -791,17 +1062,17 @@ return NULL; } - if (PyCode_Check(cmd)) { - if (PyCode_GetNumFree((PyCodeObject *)cmd) > 0) { + if (PyCode_Check(source)) { + if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { PyErr_SetString(PyExc_TypeError, "code object passed to eval() may not contain free variables"); return NULL; } - return PyEval_EvalCode(cmd, globals, locals); + return PyEval_EvalCode(source, globals, locals); } cf.cf_flags = PyCF_SOURCE_IS_UTF8; - str = source_as_string(cmd, "eval", "string, bytes or code", &cf); + str = source_as_string(source, "eval", "string, bytes or code", &cf); if (str == NULL) return NULL; @@ -814,24 +1085,62 @@ return result; } -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"); +/*[clinic input] +exec as builtin_exec + + source: 'O' + globals: 'O' = None + locals: 'O' = None + / + +Execute the given source in the context of globals and locals. + +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]*/ + +PyDoc_STRVAR(builtin_exec__doc__, +"exec(module, source, globals=None, locals=None)\n" +"Execute the given source in the context of globals and locals.\n" +"\n" +"The source may be a string representing one or more Python statements\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."); + +#define BUILTIN_EXEC_METHODDEF \ + {"exec", (PyCFunction)builtin_exec, METH_VARARGS, builtin_exec__doc__}, static PyObject * -builtin_exec(PyObject *self, PyObject *args) +builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyObject *locals); + +static PyObject * +builtin_exec(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *source; + PyObject *globals = Py_None; + PyObject *locals = Py_None; + + if (!PyArg_UnpackTuple(args, "exec", + 1, 3, + &source, &globals, &locals)) + goto exit; + return_value = builtin_exec_impl(module, source, globals, locals); + +exit: + return return_value; +} + +static PyObject * +builtin_exec_impl(PyModuleDef *module, PyObject *source, PyObject *globals, PyObject *locals) +/*[clinic end generated code: checksum=5a3ed2aebab33673c0793e9b5432d55a8f148e2f]*/ { PyObject *v; - PyObject *prog, *globals = Py_None, *locals = Py_None; - - if (!PyArg_UnpackTuple(args, "exec", 1, 3, &prog, &globals, &locals)) - return NULL; if (globals == Py_None) { globals = PyEval_GetGlobals(); @@ -850,13 +1159,13 @@ locals = globals; if (!PyDict_Check(globals)) { - PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.100s", + 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, - "arg 3 must be a mapping or None, not %.100s", + "locals must be a mapping or None, not %.100s", locals->ob_type->tp_name); return NULL; } @@ -866,21 +1175,21 @@ return NULL; } - if (PyCode_Check(prog)) { - if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) { + if (PyCode_Check(source)) { + if (PyCode_GetNumFree((PyCodeObject *)source) > 0) { PyErr_SetString(PyExc_TypeError, "code object passed to exec() may not " "contain free variables"); return NULL; } - v = PyEval_EvalCode(prog, globals, locals); + v = PyEval_EvalCode(source, globals, locals); } else { char *str; PyCompilerFlags cf; cf.cf_flags = PyCF_SOURCE_IS_UTF8; - str = source_as_string(prog, "exec", - "string, bytes or code", &cf); + str = source_as_string(source, "exec", + "string, bytes or code", &cf); if (str == NULL) return NULL; if (PyEval_MergeCompilerFlags(&cf)) @@ -895,15 +1204,8 @@ Py_RETURN_NONE; } -PyDoc_STRVAR(exec_doc, -"exec(object[, globals[, locals]])\n\ -\n\ -Read and execute code from an object, which can be a string or a code\n\ -object.\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 3.5: cannot convert yet, as needs PEP 457 group support */ static PyObject * builtin_getattr(PyObject *self, PyObject *args) { @@ -937,8 +1239,37 @@ 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]*/ + +PyDoc_STRVAR(builtin_globals__doc__, +"globals(module)\n" +"Return the dictionary containing the current scope\'s global variables.\n" +"\n" +"NOTE: Updates to this dictionary *will* affect name lookups in the current\n" +"global scope and vice-versa."); + +#define BUILTIN_GLOBALS_METHODDEF \ + {"globals", (PyCFunction)builtin_globals, METH_NOARGS, builtin_globals__doc__}, + static PyObject * -builtin_globals(PyObject *self) +builtin_globals_impl(PyModuleDef *module); + +static PyObject * +builtin_globals(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return builtin_globals_impl(module); +} + +static PyObject * +builtin_globals_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=b94881f0eefac48d4237f9495b6f468679ff3c27]*/ { PyObject *d; @@ -947,26 +1278,60 @@ 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: 'O' + name: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_hasattr__doc__, +"hasattr(module, obj, name)\n" +"Return whether the object has an attribute with the given name.\n" +"\n" +"This is done by calling getattr(obj, name) and catching AttributeError."); + +#define BUILTIN_HASATTR_METHODDEF \ + {"hasattr", (PyCFunction)builtin_hasattr, METH_VARARGS, builtin_hasattr__doc__}, static PyObject * -builtin_hasattr(PyObject *self, PyObject *args) +builtin_hasattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name); + +static PyObject * +builtin_hasattr(PyModuleDef *module, PyObject *args) +{ + PyObject *return_value = NULL; + PyObject *obj; + PyObject *name; + + if (!PyArg_UnpackTuple(args, "hasattr", + 2, 2, + &obj, &name)) + goto exit; + return_value = builtin_hasattr_impl(module, obj, name); + +exit: + return return_value; +} + +static PyObject * +builtin_hasattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name) +/*[clinic end generated code: checksum=69da37b153e7628d7972a2fcac8647a8f4fb3a7e]*/ { PyObject *v; - PyObject *name; - - if (!PyArg_UnpackTuple(args, "hasattr", 2, 2, &v, &name)) - return NULL; + if (!PyUnicode_Check(name)) { PyErr_SetString(PyExc_TypeError, "hasattr(): attribute name must be string"); return NULL; } - v = PyObject_GetAttr(v, name); + v = PyObject_GetAttr(obj, name); if (v == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); @@ -978,25 +1343,36 @@ Py_RETURN_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 AttributeError.)"); - + +/*[clinic input] +id as builtin_id + + obj: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_id__doc__, +"id(module, obj)\n" +"Return the identity of an object.\n" +"\n" +"This is guaranteed to be unique among simultaneously existing objects.\n" +"(CPython uses the object\'s memory address.)"); + +#define BUILTIN_ID_METHODDEF \ + {"id", (PyCFunction)builtin_id, METH_O, builtin_id__doc__}, static PyObject * -builtin_id(PyObject *self, PyObject *v) +builtin_id(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=7ccae76e9f305ded2826736fb0ef04e975ca16ad]*/ { - return PyLong_FromVoidPtr(v); + return PyLong_FromVoidPtr(obj); } -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 ************************************************************/ @@ -1169,6 +1545,8 @@ PyObject_GC_Del, /* tp_free */ }; + +/* AC 3.5: cannot convert yet, as needs PEP 457 group support */ static PyObject * builtin_next(PyObject *self, PyObject *args) { @@ -1210,83 +1588,178 @@ is exhausted, it is returned instead of raising StopIteration."); +/*[clinic input] +setattr as builtin_setattr + + obj: 'O' + name: 'O' + value: 'O' + / + +Sets the named attribute on the given object to the specified value. + +setattr(x, 'y', v) is equivalent to ``x.y = v'' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_setattr__doc__, +"setattr(module, obj, name, value)\n" +"Sets the named attribute on the given object to the specified value.\n" +"\n" +"setattr(x, \'y\', v) is equivalent to ``x.y = v\'\'"); + +#define BUILTIN_SETATTR_METHODDEF \ + {"setattr", (PyCFunction)builtin_setattr, METH_VARARGS, builtin_setattr__doc__}, + static PyObject * -builtin_setattr(PyObject *self, PyObject *args) +builtin_setattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name, PyObject *value); + +static PyObject * +builtin_setattr(PyModuleDef *module, PyObject *args) { - PyObject *v; + PyObject *return_value = NULL; + PyObject *obj; PyObject *name; PyObject *value; - if (!PyArg_UnpackTuple(args, "setattr", 3, 3, &v, &name, &value)) - return NULL; - if (PyObject_SetAttr(v, name, value) != 0) + if (!PyArg_UnpackTuple(args, "setattr", + 3, 3, + &obj, &name, &value)) + goto exit; + return_value = builtin_setattr_impl(module, obj, name, value); + +exit: + return return_value; +} + +static PyObject * +builtin_setattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name, PyObject *value) +/*[clinic end generated code: checksum=8e789a6e4984809f6b811318fa11a69879a814fa]*/ +{ + if (PyObject_SetAttr(obj, name, value) != 0) return NULL; Py_INCREF(Py_None); return Py_None; } -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''."); - + +/*[clinic input] +delattr as builtin_delattr + + obj: 'O' + name: 'O' + / + +Deletes the named attribute from the given object. + +delattr(x, 'y') is equivalent to ``del x.y'' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_delattr__doc__, +"delattr(module, obj, name)\n" +"Deletes the named attribute from the given object.\n" +"\n" +"delattr(x, \'y\') is equivalent to ``del x.y\'\'"); + +#define BUILTIN_DELATTR_METHODDEF \ + {"delattr", (PyCFunction)builtin_delattr, METH_VARARGS, builtin_delattr__doc__}, static PyObject * -builtin_delattr(PyObject *self, PyObject *args) +builtin_delattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name); + +static PyObject * +builtin_delattr(PyModuleDef *module, PyObject *args) { - PyObject *v; + PyObject *return_value = NULL; + PyObject *obj; PyObject *name; - if (!PyArg_UnpackTuple(args, "delattr", 2, 2, &v, &name)) - return NULL; - if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) + if (!PyArg_UnpackTuple(args, "delattr", + 2, 2, + &obj, &name)) + goto exit; + return_value = builtin_delattr_impl(module, obj, name); + +exit: + return return_value; +} + +static PyObject * +builtin_delattr_impl(PyModuleDef *module, PyObject *obj, PyObject *name) +/*[clinic end generated code: checksum=998aa8ec61fcc74dda16d3d1647c0fc03ead016c]*/ +{ + if (PyObject_SetAttr(obj, 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] +hash as builtin_hash + + obj: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_hash__doc__, +"hash(module, obj)\n" +"Return the hash value for the given object.\n" +"\n" +"Two objects that compare equal must also have the same hash value, but the\n" +"reverse is not necessarily true."); + +#define BUILTIN_HASH_METHODDEF \ + {"hash", (PyCFunction)builtin_hash, METH_O, builtin_hash__doc__}, static PyObject * -builtin_hash(PyObject *self, PyObject *v) +builtin_hash(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=a63d93a735a44517befb768f50035988c9c0e295]*/ { Py_hash_t x; - x = PyObject_Hash(v); + x = PyObject_Hash(obj); if (x == -1) return NULL; return PyLong_FromSsize_t(x); } -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."); - + +/*[clinic input] +hex as builtin_hex + + number: 'O' + / + +Return the hexadecimal representation of an integer. + + >>> hex(12648430) + '0xc0ffee' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_hex__doc__, +"hex(module, number)\n" +"Return the hexadecimal representation of an integer.\n" +"\n" +" >>> hex(12648430)\n" +" \'0xc0ffee\'"); + +#define BUILTIN_HEX_METHODDEF \ + {"hex", (PyCFunction)builtin_hex, METH_O, builtin_hex__doc__}, static PyObject * -builtin_hex(PyObject *self, PyObject *v) +builtin_hex(PyModuleDef *module, PyObject *number) +/*[clinic end generated code: checksum=17a33f618764748dc946cd81831150f74c617cdf]*/ { - return PyNumber_ToBase(v, 16); + return PyNumber_ToBase(number, 16); } -PyDoc_STRVAR(hex_doc, -"hex(number) -> string\n\ -\n\ -Return the hexadecimal representation of an integer.\n\ -\n\ - >>> hex(3735928559)\n\ - '0xdeadbeef'\n\ -"); - - + +/* AC 3.5: cannot convert yet, as needs PEP 457 group support */ static PyObject * builtin_iter(PyObject *self, PyObject *args) { @@ -1313,25 +1786,68 @@ In the second form, the callable is called until it returns the sentinel."); +/*[clinic input] +len as builtin_len + + obj: 'O' + / + +Return the number of items in a container. +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_len__doc__, +"len(module, obj)\n" +"Return the number of items in a container."); + +#define BUILTIN_LEN_METHODDEF \ + {"len", (PyCFunction)builtin_len, METH_O, builtin_len__doc__}, + static PyObject * -builtin_len(PyObject *self, PyObject *v) +builtin_len(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=766ffc44554392e98a2494da1d097cc393fef059]*/ { Py_ssize_t res; - res = PyObject_Size(v); + res = PyObject_Size(obj); if (res < 0 && PyErr_Occurred()) return NULL; return PyLong_FromSsize_t(res); } -PyDoc_STRVAR(len_doc, -"len(module, object)\n\ -\n\ -Return the number of items of a sequence or mapping."); - + +/*[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]*/ + +PyDoc_STRVAR(builtin_locals__doc__, +"locals(module)\n" +"Return a dictionary containing the current scope\'s local variables.\n" +"\n" +"NOTE: Whether or not updates to this dictionary will affect name lookups in\n" +"the local scope and vice-versa is *implementation dependent* and not\n" +"covered by any backwards compatibility guarantees."); + +#define BUILTIN_LOCALS_METHODDEF \ + {"locals", (PyCFunction)builtin_locals, METH_NOARGS, builtin_locals__doc__}, static PyObject * -builtin_locals(PyObject *self) +builtin_locals_impl(PyModuleDef *module); + +static PyObject * +builtin_locals(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return builtin_locals_impl(module); +} + +static PyObject * +builtin_locals_impl(PyModuleDef *module) +/*[clinic end generated code: checksum=d497d687f13b95f4f142fe333cebc37bd5653daa]*/ { PyObject *d; @@ -1340,11 +1856,6 @@ 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) @@ -1447,6 +1958,7 @@ return NULL; } +/* AC 3.4: waiting for *args support */ static PyObject * builtin_min(PyObject *self, PyObject *args, PyObject *kwds) { @@ -1461,6 +1973,7 @@ With two or more arguments, return the smallest argument."); +/* AC 3.4: waiting for *args support */ static PyObject * builtin_max(PyObject *self, PyObject *args, PyObject *kwds) { @@ -1475,56 +1988,93 @@ With two or more arguments, return the largest argument."); +/*[clinic input] +oct as builtin_oct + + number: 'O' + / + +Return the octal representation of an integer. + + >>> oct(342391) + '0o1234567' +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_oct__doc__, +"oct(module, number)\n" +"Return the octal representation of an integer.\n" +"\n" +" >>> oct(342391)\n" +" \'0o1234567\'"); + +#define BUILTIN_OCT_METHODDEF \ + {"oct", (PyCFunction)builtin_oct, METH_O, builtin_oct__doc__}, + static PyObject * -builtin_oct(PyObject *self, PyObject *v) +builtin_oct(PyModuleDef *module, PyObject *number) +/*[clinic end generated code: checksum=b49e004107f3f1f5a3757f623b446b7c85a185d4]*/ { - return PyNumber_ToBase(v, 8); + return PyNumber_ToBase(number, 8); } -PyDoc_STRVAR(oct_doc, -"oct(number) -> string\n\ -\n\ -Return the octal representation of an integer.\n\ -\n\ - >>> oct(342391)\n\ - '0o1234567'\n\ -"); - + +/*[clinic input] +ord as builtin_ord + + c: 'O' + / + +Return the Unicode code point for a one-character string. + + >>> ord('ニ') + 12491 +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_ord__doc__, +"ord(module, c)\n" +"Return the Unicode code point for a one-character string.\n" +"\n" +" >>> ord(\'ニ\')\n" +" 12491"); + +#define BUILTIN_ORD_METHODDEF \ + {"ord", (PyCFunction)builtin_ord, METH_O, builtin_ord__doc__}, static PyObject * -builtin_ord(PyObject *self, PyObject* obj) +builtin_ord(PyModuleDef *module, PyObject *c) +/*[clinic end generated code: checksum=57243e36338afbe1c8ac86ca4d9a479dacc2723e]*/ { long ord; Py_ssize_t size; - if (PyBytes_Check(obj)) { - size = PyBytes_GET_SIZE(obj); + if (PyBytes_Check(c)) { + size = PyBytes_GET_SIZE(c); if (size == 1) { - ord = (long)((unsigned char)*PyBytes_AS_STRING(obj)); + ord = (long)((unsigned char)*PyBytes_AS_STRING(c)); return PyLong_FromLong(ord); } } - else if (PyUnicode_Check(obj)) { - if (PyUnicode_READY(obj) == -1) + else if (PyUnicode_Check(c)) { + if (PyUnicode_READY(c) == -1) return NULL; - size = PyUnicode_GET_LENGTH(obj); + size = PyUnicode_GET_LENGTH(c); if (size == 1) { - ord = (long)PyUnicode_READ_CHAR(obj, 0); + ord = (long)PyUnicode_READ_CHAR(c, 0); return PyLong_FromLong(ord); } } - else if (PyByteArray_Check(obj)) { + else if (PyByteArray_Check(c)) { /* XXX Hopefully this is temporary */ - size = PyByteArray_GET_SIZE(obj); + size = PyByteArray_GET_SIZE(c); if (size == 1) { - ord = (long)((unsigned char)*PyByteArray_AS_STRING(obj)); + ord = (long)((unsigned char)*PyByteArray_AS_STRING(c)); return PyLong_FromLong(ord); } } else { PyErr_Format(PyExc_TypeError, "ord() expected string of length 1, but " \ - "%.200s found", obj->ob_type->tp_name); + "%.200s found", c->ob_type->tp_name); return NULL; } @@ -1535,31 +2085,61 @@ return NULL; } -PyDoc_VAR(ord_doc) = PyDoc_STR( -"ord(c) -> integer\n\ -\n\ -Return the integer ordinal of a one-character string." -); - + +/*[clinic input] +pow as builtin_pow + + x: 'O' + y: 'O' + z: 'O' = None + / + +Equivalent to x**y (with two arguments) or x**y % z (with three 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]*/ + +PyDoc_STRVAR(builtin_pow__doc__, +"pow(module, x, y, z=None)\n" +"Equivalent to x**y (with two arguments) or x**y % z (with three arguments)\n" +"\n" +"Some types, such as ints, are able to use a more efficient algorithm when\n" +"invoked using the three argument form."); + +#define BUILTIN_POW_METHODDEF \ + {"pow", (PyCFunction)builtin_pow, METH_VARARGS, builtin_pow__doc__}, static PyObject * -builtin_pow(PyObject *self, PyObject *args) +builtin_pow_impl(PyModuleDef *module, PyObject *x, PyObject *y, PyObject *z); + +static PyObject * +builtin_pow(PyModuleDef *module, PyObject *args) { - PyObject *v, *w, *z = Py_None; - - if (!PyArg_UnpackTuple(args, "pow", 2, 3, &v, &w, &z)) - return NULL; - return PyNumber_Power(v, w, z); + PyObject *return_value = NULL; + PyObject *x; + PyObject *y; + PyObject *z = Py_None; + + if (!PyArg_UnpackTuple(args, "pow", + 2, 3, + &x, &y, &z)) + goto exit; + return_value = builtin_pow_impl(module, x, y, z); + +exit: + return return_value; } -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 ints)."); - - - +static PyObject * +builtin_pow_impl(PyModuleDef *module, PyObject *x, PyObject *y, PyObject *z) +/*[clinic end generated code: checksum=c71b27c1b436a109f8f6a3c2499e874563a1b2e7]*/ +{ + return PyNumber_Power(x, y, z); +} + + +/* AC 3.4: waiting for *args support */ static PyObject * builtin_print(PyObject *self, PyObject *args, PyObject *kwds) { @@ -1655,10 +2235,57 @@ flush: whether to forcibly flush the stream."); +/*[clinic input] +input as builtin_input + + prompt: object(c_default="NULL") = None + / + +Read a string from standard input. The trailing newline is stripped. + +The prompt string, if given, is printed to standard output without a +trailing newline before reading input. + +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]*/ + +PyDoc_STRVAR(builtin_input__doc__, +"input(module, prompt=None)\n" +"Read a string from standard input. The trailing newline is stripped.\n" +"\n" +"The prompt string, if given, is printed to standard output without a\n" +"trailing newline before reading input.\n" +"\n" +"If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.\n" +"On *nix systems, readline is used if available."); + +#define BUILTIN_INPUT_METHODDEF \ + {"input", (PyCFunction)builtin_input, METH_VARARGS, builtin_input__doc__}, + static PyObject * -builtin_input(PyObject *self, PyObject *args) +builtin_input_impl(PyModuleDef *module, PyObject *prompt); + +static PyObject * +builtin_input(PyModuleDef *module, PyObject *args) { - PyObject *promptarg = NULL; + PyObject *return_value = NULL; + PyObject *prompt = NULL; + + if (!PyArg_UnpackTuple(args, "input", + 0, 1, + &prompt)) + goto exit; + return_value = builtin_input_impl(module, prompt); + +exit: + return return_value; +} + +static PyObject * +builtin_input_impl(PyModuleDef *module, PyObject *prompt) +/*[clinic end generated code: checksum=3f775e7863a36231126299ad6e21adc6d3ed23d5]*/ +{ PyObject *fin = _PySys_GetObjectId(&PyId_stdin); PyObject *fout = _PySys_GetObjectId(&PyId_stdout); PyObject *ferr = _PySys_GetObjectId(&PyId_stderr); @@ -1666,10 +2293,6 @@ long fd; int tty; - /* Parse arguments */ - if (!PyArg_UnpackTuple(args, "input", 0, 1, &promptarg)) - return NULL; - /* Check that stdin/out/err are intact */ if (fin == NULL || fin == Py_None) { PyErr_SetString(PyExc_RuntimeError, @@ -1725,7 +2348,7 @@ /* If we're interactive, use (GNU) readline */ if (tty) { PyObject *po = NULL; - char *prompt; + char *promptstr; char *s = NULL; PyObject *stdin_encoding = NULL, *stdin_errors = NULL; PyObject *stdout_encoding = NULL, *stdout_errors = NULL; @@ -1748,7 +2371,7 @@ PyErr_Clear(); else Py_DECREF(tmp); - if (promptarg != NULL) { + if (prompt != NULL) { /* We have a prompt, encode it as stdout would */ char *stdout_encoding_str, *stdout_errors_str; PyObject *stringpo; @@ -1760,7 +2383,7 @@ stdout_errors_str = _PyUnicode_AsString(stdout_errors); if (!stdout_encoding_str || !stdout_errors_str) goto _readline_errors; - stringpo = PyObject_Str(promptarg); + stringpo = PyObject_Str(prompt); if (stringpo == NULL) goto _readline_errors; po = PyUnicode_AsEncodedString(stringpo, @@ -1770,15 +2393,15 @@ Py_CLEAR(stringpo); if (po == NULL) goto _readline_errors; - prompt = PyBytes_AsString(po); - if (prompt == NULL) + promptstr = PyBytes_AsString(po); + if (promptstr == NULL) goto _readline_errors; } else { po = NULL; - prompt = ""; + promptstr = ""; } - s = PyOS_Readline(stdin, stdout, prompt); + s = PyOS_Readline(stdin, stdout, promptstr); if (s == NULL) { PyErr_CheckSignals(); if (!PyErr_Occurred()) @@ -1820,8 +2443,8 @@ } /* Fallback if we're not interactive */ - if (promptarg != NULL) { - if (PyFile_WriteObject(promptarg, fout, Py_PRINT_RAW) != 0) + if (prompt != NULL) { + if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0) return NULL; } tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); @@ -1832,28 +2455,36 @@ return PyFile_GetLine(fin, -1); } -PyDoc_STRVAR(input_doc, -"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: 'O' + / + +Return the canonical string representation of the object. + +For many object types, including most builtins, eval(repr(obj)) == obj. +[clinic start generated code]*/ + +PyDoc_STRVAR(builtin_repr__doc__, +"repr(module, obj)\n" +"Return the canonical string representation of the object.\n" +"\n" +"For many object types, including most builtins, eval(repr(obj)) == obj."); + +#define BUILTIN_REPR_METHODDEF \ + {"repr", (PyCFunction)builtin_repr, METH_O, builtin_repr__doc__}, static PyObject * -builtin_repr(PyObject *self, PyObject *v) +builtin_repr(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: checksum=57e932c0a500860943102bae7dae67cc9248c2c1]*/ { - return PyObject_Repr(v); + return PyObject_Repr(obj); } -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."); - - + +/* AC 3.5: Needs optional groups or a semantic change to accept None */ static PyObject * builtin_round(PyObject *self, PyObject *args, PyObject *kwds) { @@ -1895,6 +2526,7 @@ same type as the number. ndigits may be negative."); +/*AC 3.4: Needs support for False as an int default */ static PyObject * builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) { @@ -1939,6 +2571,7 @@ PyDoc_STRVAR(sorted_doc, "sorted(iterable, key=None, reverse=False) --> new sorted list"); +/* AC 3.5: needs optional groups */ static PyObject * builtin_vars(PyObject *self, PyObject *args) { @@ -1970,17 +2603,61 @@ Without arguments, equivalent to locals().\n\ With an argument, equivalent to object.__dict__."); -static PyObject* -builtin_sum(PyObject *self, PyObject *args) + +/* AC: this is a bit dubious, relying on "= 0" to give us NULL in C... */ +/*[clinic input] +sum as builtin_sum + + iterable: 'O' + start: 'O' = 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]*/ + +PyDoc_STRVAR(builtin_sum__doc__, +"sum(module, iterable, start=0)\n" +"Return the sum of a \'start\' value (default: 0) plus an iterable of numbers\n" +"\n" +"When the iterable is empty, return the start value.\n" +"This function is intended specifically for use with numeric values and may\n" +"reject non-numeric types."); + +#define BUILTIN_SUM_METHODDEF \ + {"sum", (PyCFunction)builtin_sum, METH_VARARGS, builtin_sum__doc__}, + +static PyObject * +builtin_sum_impl(PyModuleDef *module, PyObject *iterable, PyObject *start); + +static PyObject * +builtin_sum(PyModuleDef *module, PyObject *args) { - PyObject *seq; - PyObject *result = NULL; + PyObject *return_value = NULL; + PyObject *iterable; + PyObject *start = 0; + + if (!PyArg_UnpackTuple(args, "sum", + 1, 2, + &iterable, &start)) + goto exit; + return_value = builtin_sum_impl(module, iterable, start); + +exit: + return return_value; +} + +static PyObject * +builtin_sum_impl(PyModuleDef *module, PyObject *iterable, PyObject *start) +/*[clinic end generated code: checksum=b2036a5a24323afeb9e350c37e797daf7c015ff8]*/ +{ + PyObject *result = start; PyObject *temp, *item, *iter; - if (!PyArg_UnpackTuple(args, "sum", 1, 2, &seq, &result)) - return NULL; - - iter = PyObject_GetIter(seq); + iter = PyObject_GetIter(iterable); if (iter == NULL) return NULL; @@ -2010,7 +2687,6 @@ Py_DECREF(iter); return NULL; } - Py_INCREF(result); } @@ -2136,50 +2812,117 @@ return result; } -PyDoc_STRVAR(sum_doc, -"sum(iterable[, start]) -> value\n\ -\n\ -Return the sum of an iterable of numbers (NOT strings) plus the value\n\ -of parameter 'start' (which defaults to 0). When the iterable is\n\ -empty, return start."); - + +/*[clinic input] +isinstance as builtin_isinstance + + obj: 'O' + class_or_tuple: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_isinstance__doc__, +"isinstance(module, obj, class_or_tuple)\n" +"Return whether an object is an instance of a class or of a subclass thereof.\n" +"\n" +"A tuple, as in ``isinstance(x, (A, B, ...))``, may be given as the target to\n" +"check against. This is equivalent to ``isinstance(x, A) or isinstance(x, B)\n" +"or ...`` etc."); + +#define BUILTIN_ISINSTANCE_METHODDEF \ + {"isinstance", (PyCFunction)builtin_isinstance, METH_VARARGS, builtin_isinstance__doc__}, static PyObject * -builtin_isinstance(PyObject *self, PyObject *args) +builtin_isinstance_impl(PyModuleDef *module, PyObject *obj, PyObject *class_or_tuple); + +static PyObject * +builtin_isinstance(PyModuleDef *module, PyObject *args) { - PyObject *inst; - PyObject *cls; + PyObject *return_value = NULL; + PyObject *obj; + PyObject *class_or_tuple; + + if (!PyArg_UnpackTuple(args, "isinstance", + 2, 2, + &obj, &class_or_tuple)) + goto exit; + return_value = builtin_isinstance_impl(module, obj, class_or_tuple); + +exit: + return return_value; +} + +static PyObject * +builtin_isinstance_impl(PyModuleDef *module, PyObject *obj, PyObject *class_or_tuple) +/*[clinic end generated code: checksum=e25c52d8e6ad8fc077001fc7e82beaf0d19ff298]*/ +{ int retval; - if (!PyArg_UnpackTuple(args, "isinstance", 2, 2, &inst, &cls)) - return NULL; - - retval = PyObject_IsInstance(inst, cls); + retval = PyObject_IsInstance(obj, class_or_tuple); 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: 'O' + class_or_tuple: 'O' + / + +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]*/ + +PyDoc_STRVAR(builtin_issubclass__doc__, +"issubclass(module, cls, class_or_tuple)\n" +"Return whether \'cls\' is a derived from another class or is the same class.\n" +"\n" +"A tuple, as in ``issubclass(x, (A, B, ...))``, may be given as the target to\n" +"check against. This is equivalent to ``issubclass(x, A) or issubclass(x, B)\n" +"or ...`` etc."); + +#define BUILTIN_ISSUBCLASS_METHODDEF \ + {"issubclass", (PyCFunction)builtin_issubclass, METH_VARARGS, builtin_issubclass__doc__}, static PyObject * -builtin_issubclass(PyObject *self, PyObject *args) +builtin_issubclass_impl(PyModuleDef *module, PyObject *cls, PyObject *class_or_tuple); + +static PyObject * +builtin_issubclass(PyModuleDef *module, PyObject *args) { - PyObject *derived; + PyObject *return_value = NULL; PyObject *cls; + PyObject *class_or_tuple; + + if (!PyArg_UnpackTuple(args, "issubclass", + 2, 2, + &cls, &class_or_tuple)) + goto exit; + return_value = builtin_issubclass_impl(module, cls, class_or_tuple); + +exit: + return return_value; +} + +static PyObject * +builtin_issubclass_impl(PyModuleDef *module, PyObject *cls, PyObject *class_or_tuple) +/*[clinic end generated code: checksum=77fdd00bb55c91be9a2878539d7cbda2bdc48ade]*/ +{ int retval; - if (!PyArg_UnpackTuple(args, "issubclass", 2, 2, &derived, &cls)) - return NULL; - - retval = PyObject_IsSubclass(derived, cls); + retval = PyObject_IsSubclass(cls, class_or_tuple); if (retval < 0) return NULL; return PyBool_FromLong(retval); @@ -2386,44 +3129,44 @@ {"__build_class__", (PyCFunction)builtin___build_class__, METH_VARARGS | METH_KEYWORDS, build_class_doc}, {"__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}, - {"ascii", builtin_ascii, METH_O, ascii_doc}, - {"bin", builtin_bin, METH_O, bin_doc}, - {"callable", builtin_callable, METH_O, callable_doc}, - {"chr", builtin_chr, METH_VARARGS, chr_doc}, - {"compile", (PyCFunction)builtin_compile, METH_VARARGS | METH_KEYWORDS, compile_doc}, - {"delattr", builtin_delattr, METH_VARARGS, delattr_doc}, + BUILTIN_ABS_METHODDEF + BUILTIN_ALL_METHODDEF + BUILTIN_ANY_METHODDEF + BUILTIN_ASCII_METHODDEF + BUILTIN_BIN_METHODDEF + BUILTIN_CALLABLE_METHODDEF + BUILTIN_CHR_METHODDEF + BUILTIN_COMPILE_METHODDEF + BUILTIN_DELATTR_METHODDEF {"dir", builtin_dir, METH_VARARGS, dir_doc}, - {"divmod", builtin_divmod, METH_VARARGS, divmod_doc}, - {"eval", builtin_eval, METH_VARARGS, eval_doc}, - {"exec", builtin_exec, METH_VARARGS, exec_doc}, - {"format", builtin_format, METH_VARARGS, format_doc}, + BUILTIN_DIVMOD_METHODDEF + BUILTIN_EVAL_METHODDEF + BUILTIN_EXEC_METHODDEF + BUILTIN_FORMAT_METHODDEF {"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}, - {"isinstance", builtin_isinstance, METH_VARARGS, isinstance_doc}, - {"issubclass", builtin_issubclass, METH_VARARGS, issubclass_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", builtin_iter, METH_VARARGS, iter_doc}, - {"len", builtin_len, METH_O, len_doc}, - {"locals", (PyCFunction)builtin_locals, METH_NOARGS, locals_doc}, + BUILTIN_LEN_METHODDEF + BUILTIN_LOCALS_METHODDEF {"max", (PyCFunction)builtin_max, METH_VARARGS | METH_KEYWORDS, max_doc}, {"min", (PyCFunction)builtin_min, METH_VARARGS | METH_KEYWORDS, min_doc}, {"next", (PyCFunction)builtin_next, METH_VARARGS, next_doc}, - {"oct", builtin_oct, METH_O, oct_doc}, - {"ord", builtin_ord, METH_O, ord_doc}, - {"pow", builtin_pow, METH_VARARGS, pow_doc}, + BUILTIN_OCT_METHODDEF + BUILTIN_ORD_METHODDEF + BUILTIN_POW_METHODDEF {"print", (PyCFunction)builtin_print, METH_VARARGS | METH_KEYWORDS, print_doc}, - {"repr", builtin_repr, METH_O, repr_doc}, + BUILTIN_REPR_METHODDEF {"round", (PyCFunction)builtin_round, METH_VARARGS | METH_KEYWORDS, round_doc}, - {"setattr", builtin_setattr, METH_VARARGS, setattr_doc}, + BUILTIN_SETATTR_METHODDEF {"sorted", (PyCFunction)builtin_sorted, METH_VARARGS | METH_KEYWORDS, sorted_doc}, - {"sum", builtin_sum, METH_VARARGS, sum_doc}, + BUILTIN_SUM_METHODDEF {"vars", builtin_vars, METH_VARARGS, vars_doc}, {NULL, NULL}, };