()
| 557 | @pytest.mark.filterwarnings( |
| 558 | "ignore:.*align should be passed:numpy.exceptions.VisibleDeprecationWarning") |
| 559 | def test_pickle_python2_python3(): |
| 560 | # Test that loading object arrays saved on Python 2 works both on |
| 561 | # Python 2 and Python 3 and vice versa |
| 562 | data_dir = os.path.join(os.path.dirname(__file__), 'data') |
| 563 | |
| 564 | expected = np.array([None, range, '\u512a\u826f', |
| 565 | b'\xe4\xb8\x8d\xe8\x89\xaf'], |
| 566 | dtype=object) |
| 567 | |
| 568 | for fname in ['py2-np0-objarr.npy', 'py2-objarr.npy', 'py2-objarr.npz', |
| 569 | 'py3-objarr.npy', 'py3-objarr.npz']: |
| 570 | path = os.path.join(data_dir, fname) |
| 571 | |
| 572 | for encoding in ['bytes', 'latin1']: |
| 573 | data_f = np.load(path, allow_pickle=True, encoding=encoding) |
| 574 | if fname.endswith('.npz'): |
| 575 | data = data_f['x'] |
| 576 | data_f.close() |
| 577 | else: |
| 578 | data = data_f |
| 579 | |
| 580 | if encoding == 'latin1' and fname.startswith('py2'): |
| 581 | assert_(isinstance(data[3], str)) |
| 582 | assert_array_equal(data[:-1], expected[:-1]) |
| 583 | # mojibake occurs |
| 584 | assert_array_equal(data[-1].encode(encoding), expected[-1]) |
| 585 | else: |
| 586 | assert_(isinstance(data[3], bytes)) |
| 587 | assert_array_equal(data, expected) |
| 588 | |
| 589 | if fname.startswith('py2'): |
| 590 | if fname.endswith('.npz'): |
| 591 | data = np.load(path, allow_pickle=True) |
| 592 | assert_raises(UnicodeError, data.__getitem__, 'x') |
| 593 | data.close() |
| 594 | data = np.load(path, allow_pickle=True, fix_imports=False, |
| 595 | encoding='latin1') |
| 596 | assert_raises(ImportError, data.__getitem__, 'x') |
| 597 | data.close() |
| 598 | else: |
| 599 | assert_raises(UnicodeError, np.load, path, |
| 600 | allow_pickle=True) |
| 601 | assert_raises(ImportError, np.load, path, |
| 602 | allow_pickle=True, fix_imports=False, |
| 603 | encoding='latin1') |
| 604 | |
| 605 | |
| 606 | def test_pickle_disallow(tmpdir): |
nothing calls this directly
no test coverage detected
searching dependent graphs…