From 13fa908e8289366a4a523e676c301ec1dc18347f Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Thu, 2 Dec 2010 16:13:25 +0000 Subject: [PATCH] - When ``colander.null`` was unpickled, the reference created during unpickling was *not* a reference to the singleton but rather a new instance of the ``colander._null`` class. This was unintentional, because lots of code checks for ``if x is colander.null``, which will fail across pickling and unpickling. Now the reference created when ``colander.null`` is pickled is unpickled as the singleton itself. --- CHANGES.txt | 6 ++++++ colander/__init__.py | 3 +++ colander/tests.py | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 3f9447e..694d8f9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,12 @@ Changes Next release ------------ +- When ``colander.null`` was unpickled, the reference created during + unpickling was *not* a reference to the singleton but rather a new instance + of the ``colander._null`` class. This was unintentional, because lots of + code checks for ``if x is colander.null``, which will fail across pickling + and unpickling. Now the reference created when ``colander.null`` is + pickled is unpickled as the singleton itself. 0.9 (2010-11-28) ----------------- diff --git a/colander/__init__.py b/colander/__init__.py index 4d88226..be6901d 100644 --- a/colander/__init__.py +++ b/colander/__init__.py @@ -18,6 +18,9 @@ class _null(object): def __repr__(self): return '' + def __reduce__(self): + return 'null' # when unpickled, refers to "null" below (singleton) + null = _null() def interpolate(msgs): diff --git a/colander/tests.py b/colander/tests.py index 070c0ac..22d1f5f 100644 --- a/colander/tests.py +++ b/colander/tests.py @@ -1854,6 +1854,11 @@ class Test_null(unittest.TestCase): from colander import null self.assertEqual(repr(null), '') + def test_pickling(self): + from colander import null + import cPickle + self.failUnless(cPickle.loads(cPickle.dumps(null)) is null) + class Dummy(object): pass