Skip to content

Commit f390192

Browse files
Update whichmodule tests in cloudpickle to work with numpy 2.2. (#546)
1) test_non_module_object_passing_whichmodule_test was not testing what it was supposed to test, because after some refactoring passing "name" to "_whichmodule" became mandatory for the module lookup to occur. Changed the test to pass "func" name. 2) test_importing_multiprocessing_does_not_impact_whichmodule was no longer testing what it was supposed to test after the numpy 2.2 release which caused the numpy symbols to have a module attached, and therefore the module lookup was no longer occuring. Changed the test to avoid dependency on numpy, instead testing with our own function. Co-authored-by: Olivier Grisel <[email protected]>
1 parent 7468d72 commit f390192

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

tests/cloudpickle_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ def __getattr__(self, name):
14941494
try:
14951495
sys.modules["NonModuleObject"] = non_module_object
14961496

1497-
func_module_name = _whichmodule(func, None)
1497+
func_module_name = _whichmodule(func, "func")
14981498
assert func_module_name != "NonModuleObject"
14991499
assert func_module_name is None
15001500

@@ -1506,13 +1506,16 @@ def __getattr__(self, name):
15061506

15071507
def test_importing_multiprocessing_does_not_impact_whichmodule(self):
15081508
# non-regression test for #528
1509-
pytest.importorskip("numpy")
15101509
script = textwrap.dedent("""
15111510
import multiprocessing
15121511
import cloudpickle
1513-
from numpy import exp
1512+
from cloudpickle.cloudpickle import dumps
15141513
1515-
print(cloudpickle.cloudpickle._whichmodule(exp, exp.__name__))
1514+
# Trigger a loop during the execution of whichmodule() by
1515+
# explicitly setting the function's module to None
1516+
dumps.__module__ = None
1517+
1518+
print(cloudpickle.cloudpickle._whichmodule(dumps, dumps.__name__))
15161519
""")
15171520
script_path = Path(self.tmpdir) / "whichmodule_and_multiprocessing.py"
15181521
with open(script_path, mode="w") as f:
@@ -1524,12 +1527,9 @@ def test_importing_multiprocessing_does_not_impact_whichmodule(self):
15241527
stderr=subprocess.STDOUT,
15251528
)
15261529
out, _ = proc.communicate()
1527-
self.assertEqual(proc.wait(), 0)
1528-
assert out.strip() in (
1529-
b"numpy.core._multiarray_umath", # numpy 1
1530-
b"numpy._core._multiarray_umath", # older numpy 2
1531-
b"numpy", # more recent numpy 2
1532-
)
1530+
self.assertEqual(proc.wait(), 0, msg="Stdout: " + str(out))
1531+
self.assertEqual(out.strip(), b"cloudpickle.cloudpickle")
1532+
15331533

15341534
def test_unrelated_faulty_module(self):
15351535
# Check that pickling a dynamically defined function or class does not

0 commit comments

Comments
 (0)