From: Serhiy Storchaka Date: Wed, 25 Nov 2015 13:53:19 +0000 (+0200) Subject: Issue #24731: Fixed crash on converting objects with special methods X-Git-Tag: v3.6.0a1~986 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb6e4a0b31402f2e242f8f4be9a26e07d3dcc1b7;p=python Issue #24731: Fixed crash on converting objects with special methods __bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly. --- bb6e4a0b31402f2e242f8f4be9a26e07d3dcc1b7 diff --cc Misc/NEWS index 56a4d2d841,99e962be28..3f22a20c41 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -10,6 -10,49 +10,10 @@@ Release date: XXXX-XX-X Core and Builtins ----------------- + - Issue #24731: Fixed crash on converting objects with special methods + __bytes__, __trunc__, and __float__ returning instances of subclasses of + bytes, int, and float to subclasses of bytes, int, and float correspondingly. + -Library -------- - -- Issue #25725: Fixed a reference leak in pickle.loads() when unpickling - invalid data including tuple instructions. - -- Issue #25663: In the Readline completer, avoid listing duplicate global - names, and search the global namespace before searching builtins. - -- Issue #25688: Fixed file leak in ElementTree.iterparse() raising an error. - -- Issue #23914: Fixed SystemError raised by unpickler on broken pickle data. - -- Issue #25691: Fixed crash on deleting ElementTree.Element attributes. - -- Issue #25624: ZipFile now always writes a ZIP_STORED header for directory - entries. Patch by Dingyuan Wang. - - -What's New in Python 3.5.1 final? -================================= - -Release date: 2015-12-06 - -Core and Builtins ------------------ - -Library -------- - - -What's New in Python 3.5.1 release candidate 1? -=============================================== - -Release date: 2015-11-22 - -Core and Builtins ------------------ - - Issue #25630: Fix a possible segfault during argument parsing in functions that accept filesystem paths. diff --cc Objects/bytesobject.c index 6a9e06231c,8e098494de..cbdac86aec --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@@ -3523,40 -3386,9 +3523,40 @@@ _PyBytes_FromIterator(PyObject *x return NULL; } +PyObject * +PyBytes_FromObject(PyObject *x) +{ + if (x == NULL) { + PyErr_BadInternalCall(); + return NULL; + } + + if (PyBytes_CheckExact(x)) { + Py_INCREF(x); + return x; + } + + /* Use the modern buffer interface */ + if (PyObject_CheckBuffer(x)) + return _PyBytes_FromBuffer(x); + + if (PyList_CheckExact(x)) + return _PyBytes_FromList(x); + + if (PyTuple_CheckExact(x)) + return _PyBytes_FromTuple(x); + + if (PyUnicode_Check(x)) { + PyErr_SetString(PyExc_TypeError, + "cannot convert unicode object to bytes"); + return NULL; + } + + return _PyBytes_FromIterator(x); +} + static PyObject * - str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) + bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { PyObject *tmp, *pnew; Py_ssize_t n;