Skip to content

Commit

Permalink
Update whichmodule tests in cloudpickle to work with numpy 2.2. (#546)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
petrmitrichev and ogrisel authored Jan 14, 2025
1 parent 7468d72 commit f390192
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ def __getattr__(self, name):
try:
sys.modules["NonModuleObject"] = non_module_object

func_module_name = _whichmodule(func, None)
func_module_name = _whichmodule(func, "func")
assert func_module_name != "NonModuleObject"
assert func_module_name is None

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

def test_importing_multiprocessing_does_not_impact_whichmodule(self):
# non-regression test for #528
pytest.importorskip("numpy")
script = textwrap.dedent("""
import multiprocessing
import cloudpickle
from numpy import exp
from cloudpickle.cloudpickle import dumps
print(cloudpickle.cloudpickle._whichmodule(exp, exp.__name__))
# Trigger a loop during the execution of whichmodule() by
# explicitly setting the function's module to None
dumps.__module__ = None
print(cloudpickle.cloudpickle._whichmodule(dumps, dumps.__name__))
""")
script_path = Path(self.tmpdir) / "whichmodule_and_multiprocessing.py"
with open(script_path, mode="w") as f:
Expand All @@ -1524,12 +1527,9 @@ def test_importing_multiprocessing_does_not_impact_whichmodule(self):
stderr=subprocess.STDOUT,
)
out, _ = proc.communicate()
self.assertEqual(proc.wait(), 0)
assert out.strip() in (
b"numpy.core._multiarray_umath", # numpy 1
b"numpy._core._multiarray_umath", # older numpy 2
b"numpy", # more recent numpy 2
)
self.assertEqual(proc.wait(), 0, msg="Stdout: " + str(out))
self.assertEqual(out.strip(), b"cloudpickle.cloudpickle")


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

0 comments on commit f390192

Please sign in to comment.