Skip to content

Commit ec30d97

Browse files
authored
Merge pull request #481 from minrk/deprecated-imp
remove use of deprecated imp module
2 parents d27436a + d476bda commit ec30d97

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

ipyparallel/client/view.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
from __future__ import absolute_import
55
from __future__ import print_function
66

7-
import imp
8-
import threading
7+
import inspect
98
import warnings
109
from contextlib import contextmanager
1110

1211
from decorator import decorator
1312
from IPython import get_ipython
1413
from ipython_genutils.py3compat import iteritems
15-
from ipython_genutils.py3compat import PY3
1614
from ipython_genutils.py3compat import string_types
1715
from traitlets import Any
1816
from traitlets import Bool
@@ -433,6 +431,10 @@ def sync_imports(self, local=True, quiet=False):
433431
modules = set()
434432
results = []
435433

434+
# get the calling frame
435+
# that's two steps up due to `@contextmanager`
436+
context_frame = inspect.getouterframes(inspect.currentframe())[2].frame
437+
436438
@util.interactive
437439
def remote_import(name, fromlist, level):
438440
"""the function to be passed to apply, that actually performs the import
@@ -456,17 +458,17 @@ def view_import(name, globals={}, locals={}, fromlist=[], level=0):
456458
save_import = builtin_mod.__import__
457459
builtin_mod.__import__ = local_import
458460

459-
if imp.lock_held():
460-
# this is a side-effect import, don't do it remotely, or even
461-
# ignore the local effects
461+
import_frame = inspect.getouterframes(inspect.currentframe())[1].frame
462+
if import_frame is not context_frame:
463+
# only forward imports from the context frame,
464+
# not secondary imports
465+
# TODO: does this ever happen, or is the above `__import__` enough?
462466
return local_import(name, globals, locals, fromlist, level)
463467

464-
imp.acquire_lock()
465468
if local:
466469
mod = local_import(name, globals, locals, fromlist, level)
467470
else:
468471
raise NotImplementedError("remote-only imports not yet implemented")
469-
imp.release_lock()
470472

471473
key = name + ':' + ','.join(fromlist or [])
472474
if level <= 0 and key not in modules:

0 commit comments

Comments
 (0)