Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50793b4

Browse files
committed
Issue #18055: Move to importlib from imp for IDLE.
1 parent b101435 commit 50793b4

2 files changed

Lines changed: 21 additions & 38 deletions

File tree

Lib/idlelib/EditorWindow.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import imp
21
import importlib
2+
import importlib.abc
33
import os
44
import re
55
import string
@@ -35,34 +35,6 @@ def _sphinx_version():
3535
release += '%s%s' % (level[0], serial)
3636
return release
3737

38-
def _find_module(fullname, path=None):
39-
"""Version of imp.find_module() that handles hierarchical module names"""
40-
41-
file = None
42-
for tgt in fullname.split('.'):
43-
if file is not None:
44-
file.close() # close intermediate files
45-
(file, filename, descr) = imp.find_module(tgt, path)
46-
if descr[2] == imp.PY_SOURCE:
47-
break # find but not load the source file
48-
module = imp.load_module(tgt, file, filename, descr)
49-
try:
50-
path = module.__path__
51-
except AttributeError:
52-
raise ImportError('No source for module ' + module.__name__)
53-
if descr[2] != imp.PY_SOURCE:
54-
# If all of the above fails and didn't raise an exception,fallback
55-
# to a straight import which can find __init__.py in a package.
56-
m = __import__(fullname)
57-
try:
58-
filename = m.__file__
59-
except AttributeError:
60-
pass
61-
else:
62-
file = None
63-
descr = os.path.splitext(filename)[1], None, imp.PY_SOURCE
64-
return file, filename, descr
65-
6638

6739
class HelpDialog(object):
6840

@@ -687,20 +659,29 @@ def open_module(self, event=None):
687659
return
688660
# XXX Ought to insert current file's directory in front of path
689661
try:
690-
(f, file, (suffix, mode, type)) = _find_module(name)
691-
except (NameError, ImportError) as msg:
662+
loader = importlib.find_loader(name)
663+
except (ValueError, ImportError) as msg:
692664
tkMessageBox.showerror("Import error", str(msg), parent=self.text)
693665
return
694-
if type != imp.PY_SOURCE:
695-
tkMessageBox.showerror("Unsupported type",
696-
"%s is not a source module" % name, parent=self.text)
666+
if loader is None:
667+
tkMessageBox.showerror("Import error", "module not found",
668+
parent=self.text)
669+
return
670+
if not isinstance(loader, importlib.abc.SourceLoader):
671+
tkMessageBox.showerror("Import error", "not a source-based module",
672+
parent=self.text)
673+
return
674+
try:
675+
file_path = loader.get_filename(name)
676+
except AttributeError:
677+
tkMessageBox.showerror("Import error",
678+
"loader does not support get_filename",
679+
parent=self.text)
697680
return
698-
if f:
699-
f.close()
700681
if self.flist:
701-
self.flist.open(file)
682+
self.flist.open(file_path)
702683
else:
703-
self.io.loadfile(file)
684+
self.io.loadfile(file_path)
704685

705686
def open_class_browser(self, event=None):
706687
filename = self.io.filename

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Library
7171
IDLE
7272
----
7373

74+
- Issue #18055: Move IDLE off of imp and on to importlib.
75+
7476
- Issue #15392: Create a unittest framework for IDLE.
7577
Initial patch by Rajagopalasarma Jayakrishnan.
7678

0 commit comments

Comments
 (0)