look up __getnewargs__ and __getnewargs_ex__ on the object type (#16251)

This commit is contained in:
Benjamin Peterson 2014-02-16 13:49:16 -05:00
parent 1bcfbd5107
commit e686c5c3e8
3 changed files with 23 additions and 12 deletions

View File

@ -4701,6 +4701,20 @@ class PicklingTests(unittest.TestCase):
for proto in protocols: for proto in protocols:
self._check_reduce(proto, obj, listitems=list(obj)) self._check_reduce(proto, obj, listitems=list(obj))
def test_special_method_lookup(self):
protocols = range(pickle.HIGHEST_PROTOCOL + 1)
class Picky:
def __getstate__(self):
return {}
def __getattr__(self, attr):
if attr in ("__getnewargs__", "__getnewargs_ex__"):
raise AssertionError(attr)
return None
for protocol in protocols:
state = {} if protocol >= 2 else None
self._check_reduce(protocol, Picky(), state=state)
def _assert_is_copy(self, obj, objcopy, msg=None): def _assert_is_copy(self, obj, objcopy, msg=None):
"""Utility method to verify if two objects are copies of each others. """Utility method to verify if two objects are copies of each others.
""" """

View File

@ -10,6 +10,9 @@ Release date: 2014-02-23
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #20261: In pickle, lookup __getnewargs__ and __getnewargs_ex__ on the
type of the object.
- Issue #20619: Give the AST nodes of keyword-only arguments a column and line - Issue #20619: Give the AST nodes of keyword-only arguments a column and line
number. number.

View File

@ -3719,7 +3719,7 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
/* We first attempt to fetch the arguments for __new__ by calling /* We first attempt to fetch the arguments for __new__ by calling
__getnewargs_ex__ on the object. */ __getnewargs_ex__ on the object. */
getnewargs_ex = _PyObject_GetAttrId(obj, &PyId___getnewargs_ex__); getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__);
if (getnewargs_ex != NULL) { if (getnewargs_ex != NULL) {
PyObject *newargs = PyObject_CallObject(getnewargs_ex, NULL); PyObject *newargs = PyObject_CallObject(getnewargs_ex, NULL);
Py_DECREF(getnewargs_ex); Py_DECREF(getnewargs_ex);
@ -3766,16 +3766,13 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
return -1; return -1;
} }
return 0; return 0;
} else { } else if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { return -1;
return -1;
}
PyErr_Clear();
} }
/* The object does not have __getnewargs_ex__ so we fallback on using /* The object does not have __getnewargs_ex__ so we fallback on using
__getnewargs__ instead. */ __getnewargs__ instead. */
getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__); getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__);
if (getnewargs != NULL) { if (getnewargs != NULL) {
*args = PyObject_CallObject(getnewargs, NULL); *args = PyObject_CallObject(getnewargs, NULL);
Py_DECREF(getnewargs); Py_DECREF(getnewargs);
@ -3791,11 +3788,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
} }
*kwargs = NULL; *kwargs = NULL;
return 0; return 0;
} else { } else if (PyErr_Occurred()) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { return -1;
return -1;
}
PyErr_Clear();
} }
/* The object does not have __getnewargs_ex__ and __getnewargs__. This may /* The object does not have __getnewargs_ex__ and __getnewargs__. This may