8000 TST, BUG: Re-raise MemoryError exception in test_large_zip's process … · numpy/numpy@9765987 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9765987

Browse files
authored
TST, BUG: Re-raise MemoryError exception in test_large_zip's process (gh-16890)
Since #15893, test_large_zip's actual test is run in a child process, so when this test raises a MemoryError exception, the exception is lost and the @requires_memory decorator can't catch it to return an xfail. This commit uses a boolean variable in shared memory to flag if the exception was raised, and in that case, re-raise it in the parent process. Fixes #16889
1 parent 6ef5ec3 commit 9765987

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

numpy/lib/tests/test_io.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from io import BytesIO, StringIO
1414
from datetime import datetime
1515
import locale
16-
from multiprocessing import Process
16+
from multiprocessing import Process, Value
17+
from ctypes import c_bool
1718

1819
import numpy as np
1920
import numpy.ma as ma
@@ -574,16 +575,29 @@ def test_unicode_and_bytes_fmt(self, fmt, iotype):
574575
@pytest.mark.slow
575576
@requires_memory(free_bytes=7e9)
576577
def test_large_zip(self):
577-
def check_large_zip():
578-
# The test takes at least 6GB of memory, writes a file larger than 4GB
579-
test_data = np.asarray([np.random.rand(np.random.randint(50,100),4)
580-
for i in range(800000)], dtype=object)
581-
with tempdir() as tmpdir:
582-
np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data)
578+
def check_large_zip(memoryerror_raised):
579+
memoryerror_raised.value = False
580+
try:
581+
# The test takes at least 6GB of memory, writes a file larger
582+
# than 4GB
583+
test_data = np.asarray([np.random.rand(
584+
np.random.randint(50,100),4)
585+
for i in range(800000)], dtype=object)
586+
with tempdir() as tmpdir:
587+
np.savez(os.path.join(tmpdir, 'test.npz'),
588+
test_data=test_data)
589+
except MemoryError:
590+
memoryerror_raised.value = True
591+
raise
583592
# run in a subprocess to ensure memory is released on PyPy, see gh-15775
584-
p = Process(target=check_large_zip)
593+
# Use an object in shared memory to re-raise the MemoryError exception
594+
# in our process if needed, see gh-16889
595+
memoryerror_raised = Value(c_bool)
596+
p = Process(target=check_large_zip, args=(memoryerror_raised,))
585597
p.start()
586598
p.join()
599+
if memoryerror_raised.value:
600+
raise MemoryError("Child process raised a MemoryError exception")
587601
assert p.exitcode == 0
588602

589603
class LoadTxtBase:

0 commit comments

Comments
 (0)
0