()
| 537 | assert_array_equal(data, np.ones(2)) |
| 538 | |
| 539 | def 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 | |
| 586 | def test_pickle_disallow(tmpdir): |
nothing calls this directly
no test coverage detected