|
1 | | -import imp |
2 | 1 | import importlib |
| 2 | +import importlib.abc |
3 | 3 | import os |
4 | 4 | import re |
5 | 5 | import string |
@@ -35,34 +35,6 @@ def _sphinx_version(): |
35 | 35 | release += '%s%s' % (level[0], serial) |
36 | 36 | return release |
37 | 37 |
|
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 | | - |
66 | 38 |
|
67 | 39 | class HelpDialog(object): |
68 | 40 |
|
@@ -687,20 +659,29 @@ def open_module(self, event=None): |
687 | 659 | return |
688 | 660 | # XXX Ought to insert current file's directory in front of path |
689 | 661 | 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: |
692 | 664 | tkMessageBox.showerror("Import error", str(msg), parent=self.text) |
693 | 665 | 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) |
697 | 680 | return |
698 | | - if f: |
699 | | - f.close() |
700 | 681 | if self.flist: |
701 | | - self.flist.open(file) |
| 682 | + self.flist.open(file_path) |
702 | 683 | else: |
703 | | - self.io.loadfile(file) |
| 684 | + self.io.loadfile(file_path) |
704 | 685 |
|
705 | 686 | def open_class_browser(self, event=None): |
706 | 687 | filename = self.io.filename |
|
0 commit comments