|
1 | 1 | from __future__ import division, print_function, absolute_import
|
2 | 2 |
|
3 | 3 | from os import path
|
4 |
| - |
| 4 | +from warnings import catch_warnings |
5 | 5 |
|
6 | 6 | DATA_PATH = path.join(path.dirname(__file__), 'data')
|
7 | 7 |
|
@@ -408,12 +408,27 @@ def test_description(self):
|
408 | 408 |
|
409 | 409 |
|
410 | 410 | def test_null_pointer():
|
411 |
| - """ |
412 |
| - Regression test for null pointers. |
413 |
| - """ |
| 411 | + # Regression test for null pointers. |
414 | 412 | s = readsav(path.join(DATA_PATH, 'null_pointer.sav'), verbose=False)
|
415 | 413 | assert_identical(s.point, None)
|
416 | 414 | assert_identical(s.check, np.int16(5))
|
417 | 415 |
|
| 416 | + |
| 417 | +def test_invalid_pointer(): |
| 418 | + # Regression test for invalid pointers (gh-4613). |
| 419 | + |
| 420 | + # In some files in the wild, pointers can sometimes refer to a heap |
| 421 | + # variable that does not exist. In that case, we now gracefully fail for |
| 422 | + # that variable and replace the variable with None and emit a warning. |
| 423 | + # Since it's difficult to artificially produce such files, the file used |
| 424 | + # here has been edited to force the pointer reference to be invalid. |
| 425 | + with catch_warnings(record=True) as w: |
| 426 | + s = readsav(path.join(DATA_PATH, 'invalid_pointer.sav'), verbose=False) |
| 427 | + assert_(len(w) == 1) |
| 428 | + assert_(str(w[0].message) == ("Variable referenced by pointer not found in " |
| 429 | + "heap: variable will be set to None")) |
| 430 | + assert_identical(s['a'], np.array([None, None])) |
| 431 | + |
| 432 | + |
418 | 433 | if __name__ == "__main__":
|
419 | 434 | run_module_suite()
|
0 commit comments