Skip to content

Commit f111f7a

Browse files
Fix _whichmodule with multiprocessing (#529)
Co-authored-by: Olivier Grisel <[email protected]>
1 parent d003266 commit f111f7a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
dynamic functions and classes.
66
([PR #524](https://github.com/cloudpipe/cloudpickle/pull/524))
77

8+
- Fix a problem with the joint usage of cloudpickle's `_whichmodule` and
9+
`multiprocessing`.
10+
([PR #529](https://github.com/cloudpipe/cloudpickle/pull/529))
811

912
3.0.0
1013
=====

cloudpickle/cloudpickle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def _whichmodule(obj, name):
213213
# sys.modules
214214
if (
215215
module_name == "__main__"
216+
or module_name == "__mp_main__"
216217
or module is None
217218
or not isinstance(module, types.ModuleType)
218219
):

tests/cloudpickle_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pickle
3030

3131
import pytest
32+
from pathlib import Path
3233

3334
try:
3435
# try importing numpy and scipy. These are not hard dependencies and
@@ -1479,6 +1480,29 @@ def __getattr__(self, name):
14791480
finally:
14801481
sys.modules.pop("NonModuleObject")
14811482

1483+
def test_importing_multiprocessing_does_not_impact_whichmodule(self):
1484+
# non-regression test for #528
1485+
pytest.importorskip("numpy")
1486+
script = textwrap.dedent("""
1487+
import multiprocessing
1488+
import cloudpickle
1489+
from numpy import exp
1490+
1491+
print(cloudpickle.cloudpickle._whichmodule(exp, exp.__name__))
1492+
""")
1493+
script_path = Path(self.tmpdir) / "whichmodule_and_multiprocessing.py"
1494+
with open(script_path, mode="w") as f:
1495+
f.write(script)
1496+
1497+
proc = subprocess.Popen(
1498+
[sys.executable, str(script_path)],
1499+
stdout=subprocess.PIPE,
1500+
stderr=subprocess.STDOUT,
1501+
)
1502+
out, _ = proc.communicate()
1503+
self.assertEqual(proc.wait(), 0)
1504+
self.assertEqual(out, b"numpy.core._multiarray_umath\n")
1505+
14821506
def test_unrelated_faulty_module(self):
14831507
# Check that pickling a dynamically defined function or class does not
14841508
# fail when introspecting the currently loaded modules in sys.modules

0 commit comments

Comments
 (0)