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

Skip to content

Commit f87e04d

Browse files
committed
Finish properly hiding importlib implementation code.
1 parent e9103d2 commit f87e04d

12 files changed

Lines changed: 43 additions & 55 deletions

Lib/importlib/NOTES

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ to do
77

88
+ Expose function to see if a frozen module is a package.
99

10-
* Remove ``import *`` from importlib.__init__.
11-
12-
* Remove __all__ from importlib._bootstrap.
13-
14-
* Add leading underscores to all objects in importlib._bootstrap that are not
15-
publicly exposed.
16-
17-
* Reorder importlib/_bootstrap.py so definitions are not in inverted order.
18-
1910
* Make sure that there is documentation *somewhere* fully explaining the
2011
semantics of import that can be referenced from the package's documentation
2112
(even if it is in the package documentation itself, although it might be best

Lib/importlib/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,3 @@ def import_module(name, package=None):
136136
break
137137
level += 1
138138
return _bootstrap._gcd_import(name[level:], package, level)
139-
140-
141-
# XXX This should go away once the public API is done.
142-
from ._bootstrap import *

Lib/importlib/_bootstrap.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def get_code(self, fullname):
387387
return code_object
388388

389389

390-
class PyFileLoader(PyLoader):
390+
class _PyFileLoader(PyLoader):
391391

392392
"""Load a Python source file."""
393393

@@ -452,7 +452,7 @@ def is_package(self, fullname):
452452
return self._is_pkg
453453

454454

455-
class PyPycFileLoader(PyPycLoader, PyFileLoader):
455+
class _PyPycFileLoader(PyPycLoader, _PyFileLoader):
456456

457457
"""Load a module from a source or bytecode file."""
458458

@@ -626,7 +626,7 @@ def find_module(self, fullname, path=None):
626626
return None
627627

628628

629-
class FileFinder:
629+
class _FileFinder:
630630

631631
"""Base class for file finders.
632632
@@ -685,12 +685,12 @@ def find_module(self, fullname, path=None):
685685
return None
686686

687687

688-
class PyFileFinder(FileFinder):
688+
class _PyFileFinder(_FileFinder):
689689

690690
"""Importer for source/bytecode files."""
691691

692692
_possible_package = True
693-
_loader = PyFileLoader
693+
_loader = _PyFileLoader
694694

695695
def __init__(self, path_entry):
696696
# Lack of imp during class creation means _suffixes is set here.
@@ -700,11 +700,11 @@ def __init__(self, path_entry):
700700
super().__init__(path_entry)
701701

702702

703-
class PyPycFileFinder(PyFileFinder):
703+
class _PyPycFileFinder(_PyFileFinder):
704704

705705
"""Finder for source and bytecode files."""
706706

707-
_loader = PyPycFileLoader
707+
_loader = _PyPycFileLoader
708708

709709
def __init__(self, path_entry):
710710
super().__init__(path_entry)
@@ -713,7 +713,7 @@ def __init__(self, path_entry):
713713

714714

715715

716-
class ExtensionFileFinder(FileFinder):
716+
class _ExtensionFileFinder(_FileFinder):
717717

718718
"""Importer for extension files."""
719719

@@ -750,7 +750,7 @@ def path_hook(entry):
750750
return path_hook
751751

752752

753-
_DEFAULT_PATH_HOOK = _chained_path_hook(ExtensionFileFinder, PyPycFileFinder)
753+
_DEFAULT_PATH_HOOK = _chained_path_hook(_ExtensionFileFinder, _PyPycFileFinder)
754754

755755
class _DefaultPathFinder(PathFinder):
756756

@@ -902,8 +902,3 @@ def _import(name, globals={}, locals={}, fromlist=[], level=0):
902902
except ImportError:
903903
pass
904904
return module
905-
906-
907-
# XXX Eventually replace with a proper __all__ value (i.e., don't expose os
908-
# replacements but do expose _ExtensionFileLoader, etc. for testing).
909-
__all__ = [obj for obj in globals().keys() if not obj.startswith('__')]

Lib/importlib/test/extension/test_case_sensitivity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from test import support
33
import unittest
4-
import importlib
4+
from importlib import _bootstrap
55
from .. import util
66
from . import util as ext_util
77

@@ -13,7 +13,7 @@ def find_module(self):
1313
good_name = ext_util.NAME
1414
bad_name = good_name.upper()
1515
assert good_name != bad_name
16-
finder = importlib.ExtensionFileFinder(ext_util.PATH)
16+
finder = _bootstrap._ExtensionFileFinder(ext_util.PATH)
1717
return finder.find_module(bad_name)
1818

1919
def test_case_sensitive(self):

Lib/importlib/test/extension/test_finder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import importlib
1+
from importlib import _bootstrap
22
from .. import abc
33
from . import util
44

@@ -9,7 +9,7 @@ class FinderTests(abc.FinderTests):
99
"""Test the finder for extension modules."""
1010

1111
def find_module(self, fullname):
12-
importer = importlib.ExtensionFileFinder(util.PATH)
12+
importer = _bootstrap._ExtensionFileFinder(util.PATH)
1313
return importer.find_module(fullname)
1414

1515
def test_module(self):

Lib/importlib/test/extension/test_loader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import importlib
1+
from importlib import _bootstrap
22
from . import util as ext_util
33
from .. import abc
44
from .. import util
@@ -12,7 +12,7 @@ class LoaderTests(abc.LoaderTests):
1212
"""Test load_module() for extension modules."""
1313

1414
def load_module(self, fullname):
15-
loader = importlib._ExtensionFileLoader(ext_util.NAME,
15+
loader = _bootstrap._ExtensionFileLoader(ext_util.NAME,
1616
ext_util.FILEPATH, False)
1717
return loader.load_module(fullname)
1818

@@ -25,7 +25,7 @@ def test_module(self):
2525
self.assertEqual(getattr(module, attr), value)
2626
self.assert_(ext_util.NAME in sys.modules)
2727
self.assert_(isinstance(module.__loader__,
28-
importlib._ExtensionFileLoader))
28+
_bootstrap._ExtensionFileLoader))
2929

3030
def test_package(self):
3131
# Extensions are not found in packages.

Lib/importlib/test/extension/test_path_hook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import importlib
1+
from importlib import _bootstrap
22
from . import util
33

44
import collections
@@ -14,7 +14,7 @@ class PathHookTests(unittest.TestCase):
1414
# XXX Should it only work for directories containing an extension module?
1515

1616
def hook(self, entry):
17-
return importlib.ExtensionFileFinder(entry)
17+
return _bootstrap._ExtensionFileFinder(entry)
1818

1919
def test_success(self):
2020
# Path hook should handle a directory where a known extension module

Lib/importlib/test/source/test_case_sensitivity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Test case-sensitivity (PEP 235)."""
2-
import importlib
2+
from importlib import _bootstrap
33
from .. import util
44
from . import util as source_util
55
import os
@@ -19,7 +19,7 @@ class CaseSensitivityTest(unittest.TestCase):
1919
assert name != name.lower()
2020

2121
def find(self, path):
22-
finder = importlib.PyPycFileFinder(path)
22+
finder = _bootstrap._PyPycFileFinder(path)
2323
return finder.find_module(self.name)
2424

2525
def sensitivity_test(self):

Lib/importlib/test/source/test_file_loader.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
from importlib import _bootstrap
23
from .. import abc
34
from . import util as source_util
45

@@ -19,7 +20,8 @@ class SimpleTest(unittest.TestCase):
1920
# [basic]
2021
def test_module(self):
2122
with source_util.create_modules('_temp') as mapping:
22-
loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
23+
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
24+
False)
2325
module = loader.load_module('_temp')
2426
self.assert_('_temp' in sys.modules)
2527
check = {'__name__': '_temp', '__file__': mapping['_temp'],
@@ -29,8 +31,9 @@ def test_module(self):
2931

3032
def test_package(self):
3133
with source_util.create_modules('_pkg.__init__') as mapping:
32-
loader = importlib.PyPycFileLoader('_pkg', mapping['_pkg.__init__'],
33-
True)
34+
loader = _bootstrap._PyPycFileLoader('_pkg',
35+
mapping['_pkg.__init__'],
36+
True)
3437
module = loader.load_module('_pkg')
3538
self.assert_('_pkg' in sys.modules)
3639
check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
@@ -42,8 +45,8 @@ def test_package(self):
4245

4346
def test_lacking_parent(self):
4447
with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
45-
loader = importlib.PyPycFileLoader('_pkg.mod', mapping['_pkg.mod'],
46-
False)
48+
loader = _bootstrap._PyPycFileLoader('_pkg.mod',
49+
mapping['_pkg.mod'], False)
4750
module = loader.load_module('_pkg.mod')
4851
self.assert_('_pkg.mod' in sys.modules)
4952
check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
@@ -57,7 +60,8 @@ def fake_mtime(self, fxn):
5760

5861
def test_module_reuse(self):
5962
with source_util.create_modules('_temp') as mapping:
60-
loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
63+
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
64+
False)
6165
module = loader.load_module('_temp')
6266
module_id = id(module)
6367
module_dict_id = id(module.__dict__)
@@ -87,7 +91,8 @@ def test_state_after_failure(self):
8791
setattr(orig_module, attr, value)
8892
with open(mapping[name], 'w') as file:
8993
file.write('+++ bad syntax +++')
90-
loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
94+
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
95+
False)
9196
self.assertRaises(SyntaxError, loader.load_module, name)
9297
for attr in attributes:
9398
self.assertEqual(getattr(orig_module, attr), value)
@@ -97,7 +102,8 @@ def test_bad_syntax(self):
97102
with source_util.create_modules('_temp') as mapping:
98103
with open(mapping['_temp'], 'w') as file:
99104
file.write('=')
100-
loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
105+
loader = _bootstrap._PyPycFileLoader('_temp', mapping['_temp'],
106+
False)
101107
self.assertRaises(SyntaxError, loader.load_module, '_temp')
102108
self.assert_('_temp' not in sys.modules)
103109

@@ -116,7 +122,7 @@ class BadBytecodeTest(unittest.TestCase):
116122
"""
117123

118124
def import_(self, file, module_name):
119-
loader = importlib.PyPycFileLoader(module_name, file, False)
125+
loader = _bootstrap._PyPycFileLoader(module_name, file, False)
120126
module = loader.load_module(module_name)
121127
self.assert_(module_name in sys.modules)
122128

Lib/importlib/test/source/test_finder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import importlib
1+
from importlib import _bootstrap
22
from .. import abc
33
from . import util as source_util
44
import os
@@ -32,7 +32,7 @@ class FinderTests(abc.FinderTests):
3232
"""
3333

3434
def import_(self, root, module):
35-
finder = importlib.PyPycFileFinder(root)
35+
finder = _bootstrap._PyPycFileFinder(root)
3636
return finder.find_module(module)
3737

3838
def run_test(self, test, create=None, *, compile_=None, unlink=None):

0 commit comments

Comments
 (0)