MCPcopy Create free account
hub / github.com/numpy/numpy / test_pickle_python2_python3

Function test_pickle_python2_python3

numpy/lib/tests/test_format.py:539–583  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

537 assert_array_equal(data, np.ones(2))
538
539def test_pickle_python2_python3():
540 # Test that loading object arrays saved on Python 2 works both on
541 # Python 2 and Python 3 and vice versa
542 data_dir = os.path.join(os.path.dirname(__file__), 'data')
543
544 expected = np.array([None, range, '\u512a\u826f',
545 b'\xe4\xb8\x8d\xe8\x89\xaf'],
546 dtype=object)
547
548 for fname in ['py2-objarr.npy', 'py2-objarr.npz',
549 'py3-objarr.npy', 'py3-objarr.npz']:
550 path = os.path.join(data_dir, fname)
551
552 for encoding in ['bytes', 'latin1']:
553 data_f = np.load(path, allow_pickle=True, encoding=encoding)
554 if fname.endswith('.npz'):
555 data = data_f['x']
556 data_f.close()
557 else:
558 data = data_f
559
560 if encoding == 'latin1' and fname.startswith('py2'):
561 assert_(isinstance(data[3], str))
562 assert_array_equal(data[:-1], expected[:-1])
563 # mojibake occurs
564 assert_array_equal(data[-1].encode(encoding), expected[-1])
565 else:
566 assert_(isinstance(data[3], bytes))
567 assert_array_equal(data, expected)
568
569 if fname.startswith('py2'):
570 if fname.endswith('.npz'):
571 data = np.load(path, allow_pickle=True)
572 assert_raises(UnicodeError, data.__getitem__, 'x')
573 data.close()
574 data = np.load(path, allow_pickle=True, fix_imports=False,
575 encoding='latin1')
576 assert_raises(ImportError, data.__getitem__, 'x')
577 data.close()
578 else:
579 assert_raises(UnicodeError, np.load, path,
580 allow_pickle=True)
581 assert_raises(ImportError, np.load, path,
582 allow_pickle=True, fix_imports=False,
583 encoding='latin1')
584
585
586def test_pickle_disallow(tmpdir):

Callers

nothing calls this directly

Calls 8

assert_Function · 0.90
assert_array_equalFunction · 0.90
assert_raisesFunction · 0.90
endswithMethod · 0.80
startswithMethod · 0.80
encodeMethod · 0.80
joinMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected