Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit275a6e7

Browse files
committed
Only replace importers not from six (fixes#874)
This avoids the breakage seen in#874. Changes to six won't trigger areload, however not breaking six is more important.The code now also only provides some functions of the underlingfinder/importer also does.
1 parente38c478 commit275a6e7

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

‎bpython/curtsiesfrontend/repl.py‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -212,50 +212,51 @@ def __init__(self, watcher, loader):
212212
self.watcher=watcher
213213
self.loader=loader
214214

215-
defload_module(self,name):
215+
def__getattr__(self,name):
216+
ifname=="create_module"andhasattr(self.loader,name):
217+
returnself._create_module
218+
ifname=="load_module"andhasattr(self.loader,name):
219+
returnself._load_module
220+
returngetattr(self.loader,name)
221+
222+
def_create_module(self,spec):
223+
spec=self.loader.create_module(spec)
224+
ifgetattr(spec,"origin",None)isnotNoneandspec.origin!="builtin":
225+
self.watcher.track_module(spec.origin)
226+
returnspec
227+
228+
def_load_module(self,name):
216229
module=self.loader.load_module(name)
217230
ifhasattr(module,"__file__"):
218231
self.watcher.track_module(module.__file__)
219232
returnmodule
220233

221234

222235
classImportFinder:
223-
def__init__(self,watcher,old_meta_path):
236+
def__init__(self,finder,watcher):
224237
self.watcher=watcher
225-
self.old_meta_path=old_meta_path
226-
227-
deffind_distributions(self,context):
228-
forfinderinself.old_meta_path:
229-
distribution_finder=getattr(finder,"find_distributions",None)
230-
ifdistribution_finderisnotNone:
231-
loader=finder.find_distributions(context)
232-
ifloaderisnotNone:
233-
returnloader
234-
235-
returnNone
236-
237-
deffind_spec(self,fullname,path,target=None):
238-
forfinderinself.old_meta_path:
239-
# Consider the finder only if it implements find_spec
240-
ifgetattr(finder,"find_spec",None)isNone:
241-
continue
242-
# Attempt to find the spec
243-
spec=finder.find_spec(fullname,path,target)
244-
ifspecisnotNone:
245-
ifgetattr(spec,"__loader__",None)isnotNone:
246-
# Patch the loader to enable reloading
247-
spec.__loader__=ImportLoader(
248-
self.watcher,spec.__loader__
249-
)
250-
returnspec
251-
252-
deffind_module(self,fullname,path=None):
253-
forfinderinself.old_meta_path:
254-
loader=finder.find_module(fullname,path)
255-
ifloaderisnotNone:
256-
returnImportLoader(self.watcher,loader)
257-
258-
returnNone
238+
self.finder=finder
239+
240+
def__getattr__(self,name):
241+
ifname=="find_spec"andhasattr(self.finder,name):
242+
returnself._find_spec
243+
ifname=="find_module"andhasattr(self.finder,name):
244+
returnself._find_module
245+
returngetattr(self.finder,name)
246+
247+
def_find_spec(self,fullname,path,target=None):
248+
# Attempt to find the spec
249+
spec=self.finder.find_spec(fullname,path,target)
250+
ifspecisnotNone:
251+
ifgetattr(spec,"__loader__",None)isnotNone:
252+
# Patch the loader to enable reloading
253+
spec.__loader__=ImportLoader(self.watcher,spec.__loader__)
254+
returnspec
255+
256+
def_find_module(self,fullname,path=None):
257+
loader=self.finder.find_module(fullname,path)
258+
ifloaderisnotNone:
259+
returnImportLoader(self.watcher,loader)
259260

260261

261262
classBaseRepl(BpythonRepl):
@@ -531,7 +532,17 @@ def __enter__(self):
531532

532533
self.orig_meta_path=sys.meta_path
533534
ifself.watcher:
534-
sys.meta_path= [ImportFinder(self.watcher,self.orig_meta_path)]
535+
meta_path= []
536+
forfinderinsys.meta_path:
537+
# All elements get wrapped in ImportFinder instances execepted for instances of
538+
# _SixMetaPathImporter (from six). When importing six, it will check if the importer
539+
# is already part of sys.meta_path and will remove instances. We do not want to
540+
# break this feature (see also #874).
541+
iftype(finder).__name__=="_SixMetaPathImporter":
542+
meta_path.append(finder)
543+
else:
544+
meta_path.append(ImportFinder(finder,self.watcher))
545+
sys.meta_path=meta_path
535546

536547
sitefix.monkeypatch_quit()
537548
returnself

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp