From f3901923f89b4c25f6d4438792c7d9efa19dc429 Mon Sep 17 00:00:00 2001 From: petrmitrichev <35372680+petrmitrichev@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:35:38 +0100 Subject: [PATCH] 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 --- tests/cloudpickle_test.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/cloudpickle_test.py b/tests/cloudpickle_test.py index ecea27ae..00467e35 100644 --- a/tests/cloudpickle_test.py +++ b/tests/cloudpickle_test.py @@ -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 @@ -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: @@ -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