OK, this is really the last one tonight!

NEWFALSE and NEWTRUE.
This commit is contained in:
Guido van Rossum 2003-01-28 04:25:27 +00:00
parent 025bc2fe6c
commit 7d97d31a1b
2 changed files with 19 additions and 1 deletions

View File

@ -356,6 +356,9 @@ class Pickler:
dispatch[NoneType] = save_none dispatch[NoneType] = save_none
def save_bool(self, object): def save_bool(self, object):
if self.proto >= 2:
self.write(object and NEWTRUE or NEWFALSE)
else:
self.write(object and TRUE or FALSE) self.write(object and TRUE or FALSE)
dispatch[bool] = save_bool dispatch[bool] = save_bool
@ -760,6 +763,14 @@ class Unpickler:
self.append(None) self.append(None)
dispatch[NONE] = load_none dispatch[NONE] = load_none
def load_false(self):
self.append(False)
dispatch[NEWFALSE] = load_false
def load_true(self):
self.append(True)
dispatch[NEWTRUE] = load_true
def load_int(self): def load_int(self):
data = self.readline() data = self.readline()
if data == FALSE[1:]: if data == FALSE[1:]:

View File

@ -293,6 +293,13 @@ class AbstractPickleTests(unittest.TestCase):
y = self.loads(s) y = self.loads(s)
self.assertEqual(x, y, (proto, x, s, y)) self.assertEqual(x, y, (proto, x, s, y))
def test_singletons(self):
for proto in 0, 1, 2:
for x in None, False, True:
s = self.dumps(x, proto)
y = self.loads(s)
self.assert_(x is y, (proto, x, s, y))
class AbstractPickleModuleTests(unittest.TestCase): class AbstractPickleModuleTests(unittest.TestCase):
def test_dump_closed_file(self): def test_dump_closed_file(self):