Kaydet (Commit) 3f50cdc0 authored tarafından Guido van Rossum's avatar Guido van Rossum

Get rid of the "bozo" __getstate__ that was inserted when __slots__

was used.  This simplifies some logic in copy_reg.py (used by
pickling).  It also broke a test, but this was rewritten to test the
new feature. :-)
üst 0c10015a
...@@ -58,6 +58,9 @@ def _reduce(self): ...@@ -58,6 +58,9 @@ def _reduce(self):
try: try:
getstate = self.__getstate__ getstate = self.__getstate__
except AttributeError: except AttributeError:
if getattr(self, "__slots__", None):
raise TypeError("a class that defines __slots__ without "
"defining __getstate__ cannot be pickled")
try: try:
dict = self.__dict__ dict = self.__dict__
except AttributeError: except AttributeError:
...@@ -83,16 +86,8 @@ def _better_reduce(obj): ...@@ -83,16 +86,8 @@ def _better_reduce(obj):
args = () args = ()
getstate = getattr(obj, "__getstate__", None) getstate = getattr(obj, "__getstate__", None)
if getstate: if getstate:
try: state = getstate()
state = getstate() else:
except TypeError, err:
# XXX Catch generic exception caused by __slots__
if str(err) != ("a class that defines __slots__ "
"without defining __getstate__ "
"cannot be pickled"):
raise # Not that specific exception
getstate = None
if not getstate:
state = getattr(obj, "__dict__", None) state = getattr(obj, "__dict__", None)
names = _slotnames(cls) names = _slotnames(cls)
if names: if names:
......
...@@ -2835,7 +2835,7 @@ def pickleslots(): ...@@ -2835,7 +2835,7 @@ def pickleslots():
pass pass
else: else:
raise TestFailed, "should fail: cPickle D instance - %s" % base raise TestFailed, "should fail: cPickle D instance - %s" % base
# Give C a __getstate__ and __setstate__ # Give C a nice generic __getstate__ and __setstate__
class C(base): class C(base):
__slots__ = ['a'] __slots__ = ['a']
def __getstate__(self): def __getstate__(self):
...@@ -2843,10 +2843,12 @@ def pickleslots(): ...@@ -2843,10 +2843,12 @@ def pickleslots():
d = self.__dict__.copy() d = self.__dict__.copy()
except AttributeError: except AttributeError:
d = {} d = {}
try: for cls in self.__class__.__mro__:
d['a'] = self.a for sn in cls.__dict__.get('__slots__', ()):
except AttributeError: try:
pass d[sn] = getattr(self, sn)
except AttributeError:
pass
return d return d
def __setstate__(self, d): def __setstate__(self, d):
for k, v in d.items(): for k, v in d.items():
...@@ -2871,21 +2873,18 @@ def pickleslots(): ...@@ -2871,21 +2873,18 @@ def pickleslots():
vereq(y.a + y.b, 142) vereq(y.a + y.b, 142)
y = cPickle.loads(cPickle.dumps(x)) y = cPickle.loads(cPickle.dumps(x))
vereq(y.a + y.b, 142) vereq(y.a + y.b, 142)
# But a subclass that adds a slot should not work # A subclass that adds a slot should also work
class E(C): class E(C):
__slots__ = ['b'] __slots__ = ['b']
try: x = E()
pickle.dumps(E()) x.a = 42
except TypeError: x.b = "foo"
pass y = pickle.loads(pickle.dumps(x))
else: vereq(y.a, x.a)
raise TestFailed, "should fail: pickle E instance - %s" % base vereq(y.b, x.b)
try: y = cPickle.loads(cPickle.dumps(x))
cPickle.dumps(E()) vereq(y.a, x.a)
except TypeError: vereq(y.b, x.b)
pass
else:
raise TestFailed, "should fail: cPickle E instance - %s" % base
def copies(): def copies():
if verbose: print "Testing copy.copy() and copy.deepcopy()..." if verbose: print "Testing copy.copy() and copy.deepcopy()..."
......
...@@ -1468,21 +1468,6 @@ static PyGetSetDef subtype_getsets_weakref_only[] = { ...@@ -1468,21 +1468,6 @@ static PyGetSetDef subtype_getsets_weakref_only[] = {
{0} {0}
}; };
/* bozo: __getstate__ that raises TypeError */
static PyObject *
bozo_func(PyObject *self, PyObject *args)
{
PyErr_SetString(PyExc_TypeError,
"a class that defines __slots__ without "
"defining __getstate__ cannot be pickled");
return NULL;
}
static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS};
static PyObject *bozo_obj = NULL;
static int static int
valid_identifier(PyObject *s) valid_identifier(PyObject *s)
{ {
...@@ -1740,23 +1725,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) ...@@ -1740,23 +1725,6 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
Py_DECREF(slots); Py_DECREF(slots);
slots = newslots; slots = newslots;
/* See if *this* class defines __getstate__ */
if (PyDict_GetItemString(dict, "__getstate__") == NULL) {
/* If not, provide a bozo that raises TypeError */
if (bozo_obj == NULL) {
bozo_obj = PyCFunction_New(&bozo_ml, NULL);
if (bozo_obj == NULL)
goto bad_slots;
}
if (PyDict_SetItemString(dict,
"__getstate__",
bozo_obj) < 0)
{
Py_DECREF(bozo_obj);
goto bad_slots;
}
}
/* Secondary bases may provide weakrefs or dict */ /* Secondary bases may provide weakrefs or dict */
if (nbases > 1 && if (nbases > 1 &&
((may_add_dict && !add_dict) || ((may_add_dict && !add_dict) ||
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment