From 58f210b76b9128791152d4bc9431434221439ca9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 18 Jun 2016 09:44:03 +0300 Subject: Issue #27342: Replaced some Py_XDECREFs with Py_DECREFs. Patch by Xiang Zhang. --- Python/bltinmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 0637a2ded9..7d35cdbf46 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2711,10 +2711,10 @@ _PyBuiltin_Init(void) SETBUILTIN("zip", &PyZip_Type); debug = PyBool_FromLong(Py_OptimizeFlag == 0); if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { - Py_XDECREF(debug); + Py_DECREF(debug); return NULL; } - Py_XDECREF(debug); + Py_DECREF(debug); return mod; #undef ADD_TO_ALL -- cgit v1.2.1 From 5b00c5b38cada10df9f78640f356476cc6e6dc7f Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 22 Aug 2016 23:21:55 +0200 Subject: Issue #27809: Use _PyObject_FastCallDict() Modify: * builtin_sorted() * classmethoddescr_call() * methoddescr_call() * wrapperdescr_call() --- Python/bltinmodule.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 220c92ddc5..1cdc0e2563 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2087,10 +2087,11 @@ PyDoc_STRVAR(builtin_sorted__doc__, static PyObject * builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) { - PyObject *newlist, *v, *seq, *keyfunc=NULL, *newargs; + PyObject *newlist, *v, *seq, *keyfunc=NULL, **newargs; PyObject *callable; static char *kwlist[] = {"iterable", "key", "reverse", 0}; int reverse; + int nargs; /* args 1-3 should match listsort in Objects/listobject.c */ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted", @@ -2107,15 +2108,9 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } - 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); + newargs = &PyTuple_GET_ITEM(args, 1); + nargs = PyTuple_GET_SIZE(args) - 1; + v = _PyObject_FastCallDict(callable, newargs, nargs, kwds); Py_DECREF(callable); if (v == NULL) { Py_DECREF(newlist); -- cgit v1.2.1 From 04ca689091773fa19c16bcacfccae2914e3fae56 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 22 Aug 2016 23:33:13 +0200 Subject: Issue #27809: Use _PyObject_FastCallDict() Modify: * init_subclass() * builtin___build_class__() Fix also a bug in init_subclass(): check for super() failure. --- Python/bltinmodule.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 1cdc0e2563..b22867eb07 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -155,16 +155,8 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) } } else { - PyObject *pargs = PyTuple_Pack(2, name, bases); - if (pargs == NULL) { - Py_DECREF(prep); - Py_DECREF(meta); - Py_XDECREF(mkw); - Py_DECREF(bases); - return NULL; - } - ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw); - Py_DECREF(pargs); + PyObject *pargs[2] = {name, bases}; + ns = _PyObject_FastCallDict(prep, pargs, 2, mkw); Py_DECREF(prep); } if (ns == NULL) { -- cgit v1.2.1 From d86599444bdef5e88d1154b487ea1f011181ae30 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 23 Aug 2016 01:34:35 +0200 Subject: Issue #27809: builtin___build_class__() uses fast call --- Python/bltinmodule.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index b22867eb07..00a85b5741 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -169,12 +169,8 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) NULL, 0, NULL, 0, NULL, 0, NULL, PyFunction_GET_CLOSURE(func)); if (cell != NULL) { - PyObject *margs; - margs = PyTuple_Pack(3, name, bases, ns); - if (margs != NULL) { - cls = PyEval_CallObjectWithKeywords(meta, margs, mkw); - Py_DECREF(margs); - } + PyObject *margs[3] = {name, bases, ns}; + cls = _PyObject_FastCallDict(meta, margs, 3, mkw); if (cls != NULL && PyCell_Check(cell)) PyCell_Set(cell, cls); Py_DECREF(cell); -- cgit v1.2.1 From 518c04ccd4d3b1d2b85d8ba7e62534cac0a2bbb7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 23 Aug 2016 17:56:06 +0200 Subject: Issue #27809: map_next() uses fast call Use a small stack allocated in the C stack for up to 5 iterator functions, otherwise allocates a stack on the heap memory. --- Python/bltinmodule.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 00a85b5741..a77dfe84c1 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1156,27 +1156,43 @@ map_traverse(mapobject *lz, visitproc visit, void *arg) static PyObject * map_next(mapobject *lz) { - PyObject *val; - PyObject *argtuple; - PyObject *result; - Py_ssize_t numargs, i; + PyObject *small_stack[5]; + PyObject **stack; + Py_ssize_t niters, nargs, i; + PyObject *result = NULL; - numargs = PyTuple_GET_SIZE(lz->iters); - argtuple = PyTuple_New(numargs); - if (argtuple == NULL) - return NULL; + 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(PyObject*)); + if (stack == NULL) { + PyErr_NoMemory(); + return NULL; + } + } - for (i=0 ; iiters, i); - val = Py_TYPE(it)->tp_iternext(it); + PyObject *val = Py_TYPE(it)->tp_iternext(it); if (val == NULL) { - Py_DECREF(argtuple); - return NULL; + goto exit; } - PyTuple_SET_ITEM(argtuple, i, val); + stack[i] = val; + nargs++; + } + + result = _PyObject_FastCall(lz->func, stack, nargs); + +exit: + for (i=0; i < nargs; i++) { + Py_DECREF(stack[i]); + } + if (stack != small_stack) { + PyMem_Free(stack); } - result = PyObject_Call(lz->func, argtuple, NULL); - Py_DECREF(argtuple); return result; } -- cgit v1.2.1 From cbc99d213fb9a8a647041680e08ac19deb51daf7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 24 Aug 2016 00:54:47 +0200 Subject: Backed out changeset 70f88b097f60 (map_next) --- Python/bltinmodule.c | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index a77dfe84c1..00a85b5741 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1156,43 +1156,27 @@ map_traverse(mapobject *lz, visitproc visit, void *arg) static PyObject * map_next(mapobject *lz) { - PyObject *small_stack[5]; - PyObject **stack; - Py_ssize_t niters, nargs, i; - PyObject *result = NULL; + PyObject *val; + PyObject *argtuple; + PyObject *result; + Py_ssize_t numargs, i; - 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(PyObject*)); - if (stack == NULL) { - PyErr_NoMemory(); - return NULL; - } - } + numargs = PyTuple_GET_SIZE(lz->iters); + argtuple = PyTuple_New(numargs); + if (argtuple == NULL) + return NULL; - nargs = 0; - for (i=0; i < niters; i++) { + for (i=0 ; iiters, i); - PyObject *val = Py_TYPE(it)->tp_iternext(it); + val = Py_TYPE(it)->tp_iternext(it); if (val == NULL) { - goto exit; + Py_DECREF(argtuple); + return NULL; } - stack[i] = val; - nargs++; - } - - result = _PyObject_FastCall(lz->func, stack, nargs); - -exit: - for (i=0; i < nargs; i++) { - Py_DECREF(stack[i]); - } - if (stack != small_stack) { - PyMem_Free(stack); + PyTuple_SET_ITEM(argtuple, i, val); } + result = PyObject_Call(lz->func, argtuple, NULL); + Py_DECREF(argtuple); return result; } -- cgit v1.2.1 From c499f0ba2c5dc311729af350a85666d1d136c8f0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 24 Aug 2016 01:45:13 +0200 Subject: Issue #27809: map_next() uses fast call Use a small stack allocated in the C stack for up to 5 iterator functions, otherwise allocates a stack on the heap memory. --- Python/bltinmodule.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 00a85b5741..121cbb7f3d 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1156,27 +1156,43 @@ map_traverse(mapobject *lz, visitproc visit, void *arg) static PyObject * map_next(mapobject *lz) { - PyObject *val; - PyObject *argtuple; - PyObject *result; - Py_ssize_t numargs, i; + PyObject *small_stack[5]; + PyObject **stack; + Py_ssize_t niters, nargs, i; + PyObject *result = NULL; - numargs = PyTuple_GET_SIZE(lz->iters); - argtuple = PyTuple_New(numargs); - if (argtuple == NULL) - return NULL; + 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(); + return NULL; + } + } - for (i=0 ; iiters, i); - val = Py_TYPE(it)->tp_iternext(it); + PyObject *val = Py_TYPE(it)->tp_iternext(it); if (val == NULL) { - Py_DECREF(argtuple); - return NULL; + goto exit; } - PyTuple_SET_ITEM(argtuple, i, val); + stack[i] = val; + nargs++; + } + + result = _PyObject_FastCall(lz->func, stack, nargs); + +exit: + for (i=0; i < nargs; i++) { + Py_DECREF(stack[i]); + } + if (stack != small_stack) { + PyMem_Free(stack); } - result = PyObject_Call(lz->func, argtuple, NULL); - Py_DECREF(argtuple); return result; } -- cgit v1.2.1 From c79e5123bcf2d1e514a0420e89c62fa334d57ba9 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 25 Aug 2016 00:04:09 +0200 Subject: Use Py_ssize_t type for number of arguments Issue #27848: use Py_ssize_t rather than C int for the number of function positional and keyword arguments. --- Python/bltinmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 121cbb7f3d..bf671903ed 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2095,7 +2095,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) PyObject *callable; static char *kwlist[] = {"iterable", "key", "reverse", 0}; int reverse; - int nargs; + Py_ssize_t nargs; /* args 1-3 should match listsort in Objects/listobject.c */ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi:sorted", -- cgit v1.2.1 From 823616bdc22856b5df918a443051ec6ec2e9073c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 5 Sep 2016 14:50:11 -0700 Subject: Issue #24254: Preserve class attribute definition order. --- Python/bltinmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index dc2daa8d1a..07d59caba1 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -145,7 +145,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) if (prep == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - ns = PyDict_New(); + ns = PyODict_New(); } else { Py_DECREF(meta); -- cgit v1.2.1 From 06d0337c7565e35432fe744713260e2ef3e8a545 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 5 Sep 2016 18:16:01 -0700 Subject: Avoid calling functions with an empty string as format string Directly pass NULL rather than an empty string. --- Python/bltinmodule.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 07d59caba1..be145609dd 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1784,7 +1784,7 @@ builtin_print(PyObject *self, PyObject *args, PyObject *kwds) if (do_flush == -1) return NULL; else if (do_flush) { - tmp = _PyObject_CallMethodId(file, &PyId_flush, ""); + tmp = _PyObject_CallMethodId(file, &PyId_flush, NULL); if (tmp == NULL) return NULL; else @@ -1850,7 +1850,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt) } /* First of all, flush stderr */ - tmp = _PyObject_CallMethodId(ferr, &PyId_flush, ""); + tmp = _PyObject_CallMethodId(ferr, &PyId_flush, NULL); if (tmp == NULL) PyErr_Clear(); else @@ -1859,7 +1859,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt) /* 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_CallMethodId(fin, &PyId_fileno, ""); + tmp = _PyObject_CallMethodId(fin, &PyId_fileno, NULL); if (tmp == NULL) { PyErr_Clear(); tty = 0; @@ -1872,7 +1872,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt) tty = fd == fileno(stdin) && isatty(fd); } if (tty) { - tmp = _PyObject_CallMethodId(fout, &PyId_fileno, ""); + tmp = _PyObject_CallMethodId(fout, &PyId_fileno, NULL); if (tmp == NULL) { PyErr_Clear(); tty = 0; @@ -1907,7 +1907,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt) stdin_errors_str = _PyUnicode_AsString(stdin_errors); if (!stdin_encoding_str || !stdin_errors_str) goto _readline_errors; - tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); + tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); if (tmp == NULL) PyErr_Clear(); else @@ -1987,7 +1987,7 @@ builtin_input_impl(PyObject *module, PyObject *prompt) if (PyFile_WriteObject(prompt, fout, Py_PRINT_RAW) != 0) return NULL; } - tmp = _PyObject_CallMethodId(fout, &PyId_flush, ""); + tmp = _PyObject_CallMethodId(fout, &PyId_flush, NULL); if (tmp == NULL) PyErr_Clear(); else -- cgit v1.2.1 From 9c9a45c1f0a264d3006971b8211d948d6883a05b Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Thu, 8 Sep 2016 10:35:16 -0700 Subject: Issue #27781: Change file system encoding on Windows to UTF-8 (PEP 529) --- Python/bltinmodule.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index be145609dd..252c0a7b89 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -21,16 +21,18 @@ Don't forget to modify PyUnicode_DecodeFSDefault() if you touch any of the values for Py_FileSystemDefaultEncoding! */ -#ifdef HAVE_MBCS -const char *Py_FileSystemDefaultEncoding = "mbcs"; +#if defined(__APPLE__) +const char *Py_FileSystemDefaultEncoding = "utf-8"; int Py_HasFileSystemDefaultEncoding = 1; -#elif defined(__APPLE__) +#elif defined(MS_WINDOWS) +/* may be changed by initfsencoding(), but should never be free()d */ const char *Py_FileSystemDefaultEncoding = "utf-8"; int Py_HasFileSystemDefaultEncoding = 1; #else const char *Py_FileSystemDefaultEncoding = NULL; /* set by initfsencoding() */ int Py_HasFileSystemDefaultEncoding = 0; #endif +const char *Py_FileSystemDefaultEncodeErrors = "surrogateescape"; _Py_IDENTIFIER(__builtins__); _Py_IDENTIFIER(__dict__); -- cgit v1.2.1 From 761928cf31626534ebb81a6326c61b42050ef388 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 8 Sep 2016 15:11:11 -0700 Subject: Issue #24254: Drop cls.__definition_order__. --- Python/bltinmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 252c0a7b89..da9ad55ce8 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -147,7 +147,7 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) if (prep == NULL) { if (PyErr_ExceptionMatches(PyExc_AttributeError)) { PyErr_Clear(); - ns = PyODict_New(); + ns = PyDict_New(); } else { Py_DECREF(meta); -- cgit v1.2.1 From 0780d6cb0e6e406d0030dee40156a6dd00066147 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Sun, 11 Sep 2016 14:45:49 +1000 Subject: Issue #23722: Initialize __class__ from type.__new__() The __class__ cell used by zero-argument super() is now initialized from type.__new__ rather than __build_class__, so class methods relying on that will now work correctly when called from metaclass methods during class creation. Patch by Martin Teichmann. --- Python/bltinmodule.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index da9ad55ce8..cee013f57b 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -54,7 +54,7 @@ _Py_IDENTIFIER(stderr); static PyObject * builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) { - PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *cell; + PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *none; PyObject *cls = NULL; Py_ssize_t nargs; int isclass = 0; /* initialize to prevent gcc warning */ @@ -167,15 +167,13 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) Py_DECREF(bases); return NULL; } - cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns, + none = 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 (none != NULL) { PyObject *margs[3] = {name, bases, ns}; cls = _PyObject_FastCallDict(meta, margs, 3, mkw); - if (cls != NULL && PyCell_Check(cell)) - PyCell_Set(cell, cls); - Py_DECREF(cell); + Py_DECREF(none); } Py_DECREF(ns); Py_DECREF(meta); -- cgit v1.2.1 From 57c0f2e61c8a5893c37e576a3a03ba5e57c1132c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 20 Nov 2016 09:13:07 +0200 Subject: Replaced outdated macros _PyUnicode_AsString and _PyUnicode_AsStringAndSize with PyUnicode_AsUTF8 and PyUnicode_AsUTF8AndSize. --- Python/bltinmodule.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index cee013f57b..a49cb9d103 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -1903,8 +1903,8 @@ builtin_input_impl(PyObject *module, PyObject *prompt) /* stdin is a text stream, so it must have an encoding. */ goto _readline_errors; - stdin_encoding_str = _PyUnicode_AsString(stdin_encoding); - stdin_errors_str = _PyUnicode_AsString(stdin_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_CallMethodId(fout, &PyId_flush, NULL); @@ -1920,8 +1920,8 @@ builtin_input_impl(PyObject *module, PyObject *prompt) stdout_errors = _PyObject_GetAttrId(fout, &PyId_errors); if (!stdout_encoding || !stdout_errors) goto _readline_errors; - stdout_encoding_str = _PyUnicode_AsString(stdout_encoding); - stdout_errors_str = _PyUnicode_AsString(stdout_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); -- cgit v1.2.1 From f1b02866add58317c7f9c6c813c282659981a147 Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Mon, 5 Dec 2016 16:47:55 +1000 Subject: Issue #23722: improve __classcell__ compatibility Handling zero-argument super() in __init_subclass__ and __set_name__ involved moving __class__ initialisation to type.__new__. This requires cooperation from custom metaclasses to ensure that the new __classcell__ entry is passed along appropriately. The initial implementation of that change resulted in abruptly broken zero-argument super() support in metaclasses that didn't adhere to the new requirements (such as Django's metaclass for Model definitions). The updated approach adopted here instead emits a deprecation warning for those cases, and makes them work the same way they did in Python 3.5. This patch also improves the related class machinery documentation to cover these details and to include more reader-friendly cross-references and index entries. --- Python/bltinmodule.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index a49cb9d103..69e5f08b0e 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -54,8 +54,8 @@ _Py_IDENTIFIER(stderr); static PyObject * builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) { - PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns, *none; - PyObject *cls = NULL; + PyObject *func, *name, *bases, *mkw, *meta, *winner, *prep, *ns; + PyObject *cls = NULL, *cell = NULL; Py_ssize_t nargs; int isclass = 0; /* initialize to prevent gcc warning */ @@ -167,14 +167,44 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds) Py_DECREF(bases); return NULL; } - none = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns, + cell = PyEval_EvalCodeEx(PyFunction_GET_CODE(func), PyFunction_GET_GLOBALS(func), ns, NULL, 0, NULL, 0, NULL, 0, NULL, PyFunction_GET_CLOSURE(func)); - if (none != NULL) { + if (cell != NULL) { PyObject *margs[3] = {name, bases, ns}; cls = _PyObject_FastCallDict(meta, margs, 3, mkw); - Py_DECREF(none); + if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) { + PyObject *cell_cls = PyCell_GET(cell); + if (cell_cls != cls) { + /* TODO: In 3.7, DeprecationWarning will become RuntimeError. + * At that point, cell_error won't be needed. + */ + int cell_error; + if (cell_cls == NULL) { + const char *msg = + "__class__ not set defining %.200R as %.200R. " + "Was __classcell__ propagated to type.__new__?"; + cell_error = PyErr_WarnFormat( + PyExc_DeprecationWarning, 1, msg, name, cls); + } else { + const char *msg = + "__class__ set to %.200R defining %.200R as %.200R"; + PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls); + cell_error = 1; + } + if (cell_error) { + Py_DECREF(cls); + cls = NULL; + goto error; + } else { + /* Fill in the cell, since type.__new__ didn't do it */ + PyCell_Set(cell, cls); + } + } + } } +error: + Py_XDECREF(cell); Py_DECREF(ns); Py_DECREF(meta); Py_XDECREF(mkw); -- cgit v1.2.1 From 660076761180bac82ff7467926a4b1d41a50945f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 20 Jan 2017 08:33:06 +0200 Subject: Issue #29327: Fixed a crash when pass the iterable keyword argument to sorted(). --- Python/bltinmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Python/bltinmodule.c') diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 69e5f08b0e..8acdfc3222 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2123,7 +2123,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) { PyObject *newlist, *v, *seq, *keyfunc=NULL, **newargs; PyObject *callable; - static char *kwlist[] = {"iterable", "key", "reverse", 0}; + static char *kwlist[] = {"", "key", "reverse", 0}; int reverse; Py_ssize_t nargs; @@ -2142,6 +2142,7 @@ builtin_sorted(PyObject *self, PyObject *args, PyObject *kwds) return NULL; } + assert(PyTuple_GET_SIZE(args) >= 1); newargs = &PyTuple_GET_ITEM(args, 1); nargs = PyTuple_GET_SIZE(args) - 1; v = _PyObject_FastCallDict(callable, newargs, nargs, kwds); -- cgit v1.2.1