diff options
author | Alexandre Vassalotti <alexandre@peadrop.com> | 2013-11-30 21:24:13 (GMT) |
---|---|---|
committer | Alexandre Vassalotti <alexandre@peadrop.com> | 2013-11-30 21:24:13 (GMT) |
commit | 1d3a17332683bc38f8be956ac3dc9ea8fbba27af (patch) | |
tree | 5102c3544b9ff7f3b78e3eafdd4b2e78d8f19827 | |
parent | 72a01b29bf7ac17de81c294f4ee6e5cc7359c685 (diff) | |
download | cpython-1d3a17332683bc38f8be956ac3dc9ea8fbba27af.zip cpython-1d3a17332683bc38f8be956ac3dc9ea8fbba27af.tar.gz cpython-1d3a17332683bc38f8be956ac3dc9ea8fbba27af.tar.bz2 |
Issue #16231: Allow false values other than None to be used as persistent IDs.
-rw-r--r-- | Lib/pickle.py | 2 | ||||
-rw-r--r-- | Lib/test/pickletester.py | 40 | ||||
-rw-r--r-- | Misc/NEWS | 5 |
3 files changed, 28 insertions, 19 deletions
diff --git a/Lib/pickle.py b/Lib/pickle.py index 508e858..299de16 100644 --- a/Lib/pickle.py +++ b/Lib/pickle.py @@ -269,7 +269,7 @@ class Pickler: def save(self, obj): # Check for persistent id (defined by a subclass) pid = self.persistent_id(obj) - if pid: + if pid is not None: self.save_pers(pid) return diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py index 34cafcb..1599893 100644 --- a/Lib/test/pickletester.py +++ b/Lib/test/pickletester.py @@ -1152,30 +1152,34 @@ class AbstractPersistentPicklerTests(unittest.TestCase): if isinstance(object, int) and object % 2 == 0: self.id_count += 1 return str(object) + elif object == "test_false_value": + self.false_count += 1 + return "" else: return None def persistent_load(self, oid): - self.load_count += 1 - object = int(oid) - assert object % 2 == 0 - return object + if not oid: + self.load_false_count += 1 + return "test_false_value" + else: + self.load_count += 1 + object = int(oid) + assert object % 2 == 0 + return object def test_persistence(self): - self.id_count = 0 - self.load_count = 0 - L = range(10) - self.assertEqual(self.loads(self.dumps(L)), L) - self.assertEqual(self.id_count, 5) - self.assertEqual(self.load_count, 5) - - def test_bin_persistence(self): - self.id_count = 0 - self.load_count = 0 - L = range(10) - self.assertEqual(self.loads(self.dumps(L, 1)), L) - self.assertEqual(self.id_count, 5) - self.assertEqual(self.load_count, 5) + L = range(10) + ["test_false_value"] + for proto in protocols: + self.id_count = 0 + self.false_count = 0 + self.load_false_count = 0 + self.load_count = 0 + self.assertEqual(self.loads(self.dumps(L, proto)), L) + self.assertEqual(self.id_count, 5) + self.assertEqual(self.false_count, 1) + self.assertEqual(self.load_count, 5) + self.assertEqual(self.load_false_count, 1) class AbstractPicklerUnpicklerObjectTests(unittest.TestCase): @@ -15,6 +15,11 @@ Core and Builtins Library ------- +- Issue #16231: Fixed pickle.Pickler to only fallback to its default pickling + behaviour when Pickler.persistent_id returns None, but not for any other + false values. This allows false values other values other than None to be + used as persistent IDs. This behaviour is consistent with cPickle. + - Issue #11508: Fixed uuid.getnode() and uuid.uuid1() on environment with virtual interface. Original patch by Kent Frazier. |