mirror of
https://github.com/python/cpython.git
synced 2025-10-17 20:28:43 +00:00
Introducing __reduce_ex__, which is called with a protocol number argument
if it exists in preference over __reduce__. Now Tim can go implement this in cPickle.c.
This commit is contained in:
parent
2b0643a95d
commit
c53f009f94
4 changed files with 36 additions and 19 deletions
|
@ -109,6 +109,17 @@ def _better_reduce(obj):
|
||||||
dictitems = obj.iteritems()
|
dictitems = obj.iteritems()
|
||||||
return __newobj__, (cls,) + args, state, listitems, dictitems
|
return __newobj__, (cls,) + args, state, listitems, dictitems
|
||||||
|
|
||||||
|
# Extended reduce:
|
||||||
|
|
||||||
|
def _reduce_ex(obj, proto=0):
|
||||||
|
obj_reduce = getattr(obj, "__reduce__", None)
|
||||||
|
if obj_reduce and obj.__class__.__reduce__ is not object.__reduce__:
|
||||||
|
return obj_reduce()
|
||||||
|
elif proto < 2:
|
||||||
|
return _reduce(obj)
|
||||||
|
else:
|
||||||
|
return _better_reduce(obj)
|
||||||
|
|
||||||
def _slotnames(cls):
|
def _slotnames(cls):
|
||||||
"""Return a list of slot names for a given class.
|
"""Return a list of slot names for a given class.
|
||||||
|
|
||||||
|
|
|
@ -304,21 +304,20 @@ class Pickler:
|
||||||
|
|
||||||
# Check copy_reg.dispatch_table
|
# Check copy_reg.dispatch_table
|
||||||
reduce = dispatch_table.get(t)
|
reduce = dispatch_table.get(t)
|
||||||
if not reduce:
|
if reduce:
|
||||||
# Check for a __reduce__ method.
|
rv = reduce(obj)
|
||||||
# Subtle: get the unbound method from the class, so that
|
else:
|
||||||
# protocol 2 can override the default __reduce__ that all
|
# Check for a __reduce_ex__ method, fall back to __reduce__
|
||||||
# classes inherit from object. This has the added
|
reduce = getattr(obj, "__reduce_ex__", None)
|
||||||
# advantage that the call always has the form reduce(obj)
|
if reduce:
|
||||||
reduce = getattr(t, "__reduce__", None)
|
rv = reduce(self.proto)
|
||||||
if self.proto >= 2:
|
else:
|
||||||
# Protocol 2 can do better than the default __reduce__
|
reduce = getattr(obj, "__reduce__", None)
|
||||||
if reduce is object.__reduce__:
|
if reduce:
|
||||||
reduce = _better_reduce
|
rv = reduce()
|
||||||
if not reduce:
|
else:
|
||||||
raise PicklingError("Can't pickle %r object: %r" %
|
raise PicklingError("Can't pickle %r object: %r" %
|
||||||
(t.__name__, obj))
|
(t.__name__, obj))
|
||||||
rv = reduce(obj)
|
|
||||||
|
|
||||||
# Check for string returned by reduce(), meaning "save as global"
|
# Check for string returned by reduce(), meaning "save as global"
|
||||||
if type(rv) is StringType:
|
if type(rv) is StringType:
|
||||||
|
|
|
@ -210,6 +210,7 @@ Instead, you can get the same information from the list type:
|
||||||
'__ne__',
|
'__ne__',
|
||||||
'__new__',
|
'__new__',
|
||||||
'__reduce__',
|
'__reduce__',
|
||||||
|
'__reduce_ex__',
|
||||||
'__repr__',
|
'__repr__',
|
||||||
'__rmul__',
|
'__rmul__',
|
||||||
'__setattr__',
|
'__setattr__',
|
||||||
|
|
|
@ -2441,11 +2441,15 @@ static PyGetSetDef object_getsets[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
object_reduce(PyObject *self, PyObject *args)
|
object_reduce_ex(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
/* Call copy_reg._reduce(self) */
|
/* Call copy_reg._reduce_ex(self, proto) */
|
||||||
static PyObject *copy_reg_str;
|
static PyObject *copy_reg_str;
|
||||||
PyObject *copy_reg, *res;
|
PyObject *copy_reg, *res;
|
||||||
|
int proto = 0;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (!copy_reg_str) {
|
if (!copy_reg_str) {
|
||||||
copy_reg_str = PyString_InternFromString("copy_reg");
|
copy_reg_str = PyString_InternFromString("copy_reg");
|
||||||
|
@ -2455,13 +2459,15 @@ object_reduce(PyObject *self, PyObject *args)
|
||||||
copy_reg = PyImport_Import(copy_reg_str);
|
copy_reg = PyImport_Import(copy_reg_str);
|
||||||
if (!copy_reg)
|
if (!copy_reg)
|
||||||
return NULL;
|
return NULL;
|
||||||
res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
|
res = PyEval_CallMethod(copy_reg, "_reduce_ex", "(Oi)", self, proto);
|
||||||
Py_DECREF(copy_reg);
|
Py_DECREF(copy_reg);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef object_methods[] = {
|
static PyMethodDef object_methods[] = {
|
||||||
{"__reduce__", object_reduce, METH_NOARGS,
|
{"__reduce_ex__", object_reduce_ex, METH_VARARGS,
|
||||||
|
PyDoc_STR("helper for pickle")},
|
||||||
|
{"__reduce__", object_reduce_ex, METH_VARARGS,
|
||||||
PyDoc_STR("helper for pickle")},
|
PyDoc_STR("helper for pickle")},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue