Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e686c5c

Browse files
committed
look up __getnewargs__ and __getnewargs_ex__ on the object type (#16251)
1 parent 1bcfbd5 commit e686c5c

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

Lib/test/test_descr.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,6 +4701,20 @@ class C16(list):
47014701
for proto in protocols:
47024702
self._check_reduce(proto, obj, listitems=list(obj))
47034703

4704+
def test_special_method_lookup(self):
4705+
protocols = range(pickle.HIGHEST_PROTOCOL + 1)
4706+
class Picky:
4707+
def __getstate__(self):
4708+
return {}
4709+
4710+
def __getattr__(self, attr):
4711+
if attr in ("__getnewargs__", "__getnewargs_ex__"):
4712+
raise AssertionError(attr)
4713+
return None
4714+
for protocol in protocols:
4715+
state = {} if protocol >= 2 else None
4716+
self._check_reduce(protocol, Picky(), state=state)
4717+
47044718
def _assert_is_copy(self, obj, objcopy, msg=None):
47054719
"""Utility method to verify if two objects are copies of each others.
47064720
"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Release date: 2014-02-23
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #20261: In pickle, lookup __getnewargs__ and __getnewargs_ex__ on the
14+
type of the object.
15+
1316
- Issue #20619: Give the AST nodes of keyword-only arguments a column and line
1417
number.
1518

Objects/typeobject.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3719,7 +3719,7 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
37193719

37203720
/* We first attempt to fetch the arguments for __new__ by calling
37213721
__getnewargs_ex__ on the object. */
3722-
getnewargs_ex = _PyObject_GetAttrId(obj, &PyId___getnewargs_ex__);
3722+
getnewargs_ex = _PyObject_LookupSpecial(obj, &PyId___getnewargs_ex__);
37233723
if (getnewargs_ex != NULL) {
37243724
PyObject *newargs = PyObject_CallObject(getnewargs_ex, NULL);
37253725
Py_DECREF(getnewargs_ex);
@@ -3766,16 +3766,13 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
37663766
return -1;
37673767
}
37683768
return 0;
3769-
} else {
3770-
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
3771-
return -1;
3772-
}
3773-
PyErr_Clear();
3769+
} else if (PyErr_Occurred()) {
3770+
return -1;
37743771
}
37753772

37763773
/* The object does not have __getnewargs_ex__ so we fallback on using
37773774
__getnewargs__ instead. */
3778-
getnewargs = _PyObject_GetAttrId(obj, &PyId___getnewargs__);
3775+
getnewargs = _PyObject_LookupSpecial(obj, &PyId___getnewargs__);
37793776
if (getnewargs != NULL) {
37803777
*args = PyObject_CallObject(getnewargs, NULL);
37813778
Py_DECREF(getnewargs);
@@ -3791,11 +3788,8 @@ _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
37913788
}
37923789
*kwargs = NULL;
37933790
return 0;
3794-
} else {
3795-
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
3796-
return -1;
3797-
}
3798-
PyErr_Clear();
3791+
} else if (PyErr_Occurred()) {
3792+
return -1;
37993793
}
38003794

38013795
/* The object does not have __getnewargs_ex__ and __getnewargs__. This may

0 commit comments

Comments
 (0)