Commit 0c6ccff9 authored by Guido van Rossum's avatar Guido van Rossum

An example of action-at-a-distance: fix the problems I had in test_io.py

without touching io.py or test_io.py.  The cause of the failure was that
bytes objects didn't pickle right.  As a stop-gap measure, I'm providing
bytes pickling via copy_reg.  Eventually, we should use a more efficient
protocol, e.g. __reduce_ex__ or __getstate__/__setstate__.
parent 406eb5e9
...@@ -23,18 +23,12 @@ def constructor(object): ...@@ -23,18 +23,12 @@ def constructor(object):
if not callable(object): if not callable(object):
raise TypeError("constructors must be callable") raise TypeError("constructors must be callable")
# Example: provide pickling support for complex numbers. # Example: provide pickling support for bytes objects.
try: def _pickle_bytes(b):
complex return bytes, (str(b),)
except NameError:
pass
else:
def pickle_complex(c):
return complex, (c.real, c.imag)
pickle(complex, pickle_complex, complex) pickle(bytes, _pickle_bytes)
# Support for pickling new-style objects # Support for pickling new-style objects
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
import os import os
import re import re
import sys import sys
import pickle
import cPickle
import tempfile import tempfile
import unittest import unittest
import test.test_support import test.test_support
...@@ -633,6 +635,14 @@ class BytesTest(unittest.TestCase): ...@@ -633,6 +635,14 @@ class BytesTest(unittest.TestCase):
self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi'))
self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b''))
def test_pickling(self):
for pm in pickle, cPickle:
for proto in range(pm.HIGHEST_PROTOCOL):
for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0":
ps = pm.dumps(b, proto)
q = pm.loads(ps)
self.assertEqual(b, q)
# Optimizations: # Optimizations:
# __iter__? (optimization) # __iter__? (optimization)
# __reversed__? (optimization) # __reversed__? (optimization)
...@@ -641,8 +651,6 @@ class BytesTest(unittest.TestCase): ...@@ -641,8 +651,6 @@ class BytesTest(unittest.TestCase):
# lstrip, rstrip, strip?? (currently un-pepped) # lstrip, rstrip, strip?? (currently un-pepped)
# join # join
# XXX pickle and marshal support?
# There are tests in string_tests.py that are more # There are tests in string_tests.py that are more
# comprehensive for things like split, partition, etc. # comprehensive for things like split, partition, etc.
# Unfortunately they are all bundled with tests that # Unfortunately they are all bundled with tests that
......
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