[Python-checkins] r72223 - in python/trunk: Lib/pickle.py Lib/test/pickletester.py Misc/NEWS Modules/cPickle.c
antoine.pitrou
python-checkins at python.org
Sat May 2 23:13:24 CEST 2009
Author: antoine.pitrou
Date: Sat May 2 23:13:23 2009
New Revision: 72223
Log:
Isue #5084: unpickling now interns the attribute names of pickled objects,
saving memory and avoiding growth in size of subsequent pickles. Proposal
and original patch by Jake McGuire.
Modified:
python/trunk/Lib/pickle.py
python/trunk/Lib/test/pickletester.py
python/trunk/Misc/NEWS
python/trunk/Modules/cPickle.c
Modified: python/trunk/Lib/pickle.py
==============================================================================
--- python/trunk/Lib/pickle.py (original)
+++ python/trunk/Lib/pickle.py Sat May 2 23:13:23 2009
@@ -1221,7 +1221,15 @@
state, slotstate = state
if state:
try:
- inst.__dict__.update(state)
+ d = inst.__dict__
+ try:
+ for k, v in state.iteritems():
+ d[intern(k)] = v
+ # keys in state don't have to be strings
+ # don't blow up, but don't go out of our way
+ except TypeError:
+ d.update(state)
+
except RuntimeError:
# XXX In restricted execution, the instance's __dict__
# is not accessible. Use the old way of unpickling
Modified: python/trunk/Lib/test/pickletester.py
==============================================================================
--- python/trunk/Lib/test/pickletester.py (original)
+++ python/trunk/Lib/test/pickletester.py Sat May 2 23:13:23 2009
@@ -938,6 +938,20 @@
"Failed protocol %d: %r != %r"
% (proto, obj, loaded))
+ def test_attribute_name_interning(self):
+ # Test that attribute names of pickled objects are interned when
+ # unpickling.
+ for proto in protocols:
+ x = C()
+ x.foo = 42
+ x.bar = "hello"
+ s = self.dumps(x, proto)
+ y = self.loads(s)
+ x_keys = sorted(x.__dict__)
+ y_keys = sorted(y.__dict__)
+ for x_key, y_key in zip(x_keys, y_keys):
+ self.assertIs(x_key, y_key)
+
# Test classes for reduce_ex
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Sat May 2 23:13:23 2009
@@ -261,6 +261,10 @@
Library
-------
+- Issue #5084: unpickling now interns the attribute names of pickled objects,
+ saving memory and avoiding growth in size of subsequent pickles. Proposal
+ and original patch by Jake McGuire.
+
- Issue #3002: ``shutil.copyfile()`` and ``shutil.copytree()`` now raise an
error when a named pipe is encountered, rather than blocking infinitely.
Modified: python/trunk/Modules/cPickle.c
==============================================================================
--- python/trunk/Modules/cPickle.c (original)
+++ python/trunk/Modules/cPickle.c Sat May 2 23:13:23 2009
@@ -4473,8 +4473,16 @@
i = 0;
while (PyDict_Next(state, &i, &d_key, &d_value)) {
- if (PyObject_SetItem(dict, d_key, d_value) < 0)
+ /* normally the keys for instance attributes are
+ interned. we should try to do that here. */
+ Py_INCREF(d_key);
+ if (PyString_CheckExact(d_key))
+ PyString_InternInPlace(&d_key);
+ if (PyObject_SetItem(dict, d_key, d_value) < 0) {
+ Py_DECREF(d_key);
goto finally;
+ }
+ Py_DECREF(d_key);
}
Py_DECREF(dict);
}
More information about the Python-checkins
mailing list