def test_python2(): py2_prog = ('import cPickle as pickle\n' 'print(pickle.dumps([42, "bytes", u"unicode", ' 'b"more bytes", u"more unicode"], 2))') try: buf = subprocess.check_output(['python', '-c', py2_prog]) except FileNotFoundError: pytest.skip('failed to execute python2') assert safe_unpickle(buf, encoding='latin1') == \ [ 42, 'bytes', 'unicode', 'more bytes', 'more unicode']
def test_composite(): elems = ('a string', b'bytes', 42, 23.222) # Tuple buf = pickle.dumps(elems, PICKLE_PROTOCOL) safe_unpickle(buf) # Dict buf = pickle.dumps(dict(x for x in enumerate(elems)), PICKLE_PROTOCOL) safe_unpickle(buf) # List buf = pickle.dumps(list(elems), PICKLE_PROTOCOL) safe_unpickle(buf) # Set buf = pickle.dumps(set(elems), PICKLE_PROTOCOL) safe_unpickle(buf) # Frozenset buf = pickle.dumps(frozenset(elems), PICKLE_PROTOCOL) safe_unpickle(buf)
def test_elementary(): for obj in ('a string', b'bytes', 42, 23.222, bytearray(b'ascii rules')): buf = pickle.dumps(obj, PICKLE_PROTOCOL) safe_unpickle(buf)