Commit 91cf882b authored by Brett Cannon's avatar Brett Cannon

Refactor source and bytecode file loaders in importlib so that there

are source-only and source/bytecode loaders.
parent 0515619d
to do to do
///// /////
* Refactor source/bytecode finder/loader code such that bytecode support is a
subclass of source support (makes it nicer for VMs that don't use CPython
bytecode).
+ PyLoader (for ABC)
- load_module for source only
- get_code for source only
+ PyFileLoader(PyLoader)
- get_data
- source_mtime
- source_path
+PyPycLoader (PyLoader, for ABC)
- load_module for source and bytecode
- get_code for source and bytecode
+ PyPycFileLoader(PyPycLoader, PyFileLoader)
- bytecode_path
- write_bytecode
* Implement PEP 302 protocol for loaders (should just be a matter of testing). * Implement PEP 302 protocol for loaders (should just be a matter of testing).
+ Source/bytecode. + Source/bytecode.
...@@ -42,7 +17,6 @@ to do ...@@ -42,7 +17,6 @@ to do
* load_module * load_module
- (?) Importer(Finder, Loader)
- ResourceLoader(Loader) - ResourceLoader(Loader)
* get_data * get_data
...@@ -89,6 +63,8 @@ to do ...@@ -89,6 +63,8 @@ to do
* Add leading underscores to all objects in importlib._bootstrap that are not * Add leading underscores to all objects in importlib._bootstrap that are not
publicly exposed. publicly exposed.
* Reorder importlib/_bootstrap.py so definitions are not in inverted order.
* Make sure that there is documentation *somewhere* fully explaining the * Make sure that there is documentation *somewhere* fully explaining the
semantics of import that can be referenced from the package's documentation semantics of import that can be referenced from the package's documentation
(even if it is in the package documentation itself, although it might be best (even if it is in the package documentation itself, although it might be best
......
This diff is collapsed.
...@@ -19,7 +19,7 @@ class SimpleTest(unittest.TestCase): ...@@ -19,7 +19,7 @@ class SimpleTest(unittest.TestCase):
# [basic] # [basic]
def test_module(self): def test_module(self):
with source_util.create_modules('_temp') as mapping: with source_util.create_modules('_temp') as mapping:
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
module = loader.load_module('_temp') module = loader.load_module('_temp')
self.assert_('_temp' in sys.modules) self.assert_('_temp' in sys.modules)
check = {'__name__': '_temp', '__file__': mapping['_temp'], check = {'__name__': '_temp', '__file__': mapping['_temp'],
...@@ -29,7 +29,7 @@ class SimpleTest(unittest.TestCase): ...@@ -29,7 +29,7 @@ class SimpleTest(unittest.TestCase):
def test_package(self): def test_package(self):
with source_util.create_modules('_pkg.__init__') as mapping: with source_util.create_modules('_pkg.__init__') as mapping:
loader = importlib._PyFileLoader('_pkg', mapping['_pkg.__init__'], loader = importlib.PyPycFileLoader('_pkg', mapping['_pkg.__init__'],
True) True)
module = loader.load_module('_pkg') module = loader.load_module('_pkg')
self.assert_('_pkg' in sys.modules) self.assert_('_pkg' in sys.modules)
...@@ -42,7 +42,7 @@ class SimpleTest(unittest.TestCase): ...@@ -42,7 +42,7 @@ class SimpleTest(unittest.TestCase):
def test_lacking_parent(self): def test_lacking_parent(self):
with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
loader = importlib._PyFileLoader('_pkg.mod', mapping['_pkg.mod'], loader = importlib.PyPycFileLoader('_pkg.mod', mapping['_pkg.mod'],
False) False)
module = loader.load_module('_pkg.mod') module = loader.load_module('_pkg.mod')
self.assert_('_pkg.mod' in sys.modules) self.assert_('_pkg.mod' in sys.modules)
...@@ -57,7 +57,7 @@ class SimpleTest(unittest.TestCase): ...@@ -57,7 +57,7 @@ class SimpleTest(unittest.TestCase):
def test_module_reuse(self): def test_module_reuse(self):
with source_util.create_modules('_temp') as mapping: with source_util.create_modules('_temp') as mapping:
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
module = loader.load_module('_temp') module = loader.load_module('_temp')
module_id = id(module) module_id = id(module)
module_dict_id = id(module.__dict__) module_dict_id = id(module.__dict__)
...@@ -87,7 +87,7 @@ class SimpleTest(unittest.TestCase): ...@@ -87,7 +87,7 @@ class SimpleTest(unittest.TestCase):
setattr(orig_module, attr, value) setattr(orig_module, attr, value)
with open(mapping[name], 'w') as file: with open(mapping[name], 'w') as file:
file.write('+++ bad syntax +++') file.write('+++ bad syntax +++')
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
self.assertRaises(SyntaxError, loader.load_module, name) self.assertRaises(SyntaxError, loader.load_module, name)
for attr in attributes: for attr in attributes:
self.assertEqual(getattr(orig_module, attr), value) self.assertEqual(getattr(orig_module, attr), value)
...@@ -97,7 +97,7 @@ class SimpleTest(unittest.TestCase): ...@@ -97,7 +97,7 @@ class SimpleTest(unittest.TestCase):
with source_util.create_modules('_temp') as mapping: with source_util.create_modules('_temp') as mapping:
with open(mapping['_temp'], 'w') as file: with open(mapping['_temp'], 'w') as file:
file.write('=') file.write('=')
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
self.assertRaises(SyntaxError, loader.load_module, '_temp') self.assertRaises(SyntaxError, loader.load_module, '_temp')
self.assert_('_temp' not in sys.modules) self.assert_('_temp' not in sys.modules)
...@@ -112,7 +112,7 @@ class DontWriteBytecodeTest(unittest.TestCase): ...@@ -112,7 +112,7 @@ class DontWriteBytecodeTest(unittest.TestCase):
@source_util.writes_bytecode @source_util.writes_bytecode
def run_test(self, assertion): def run_test(self, assertion):
with source_util.create_modules('_temp') as mapping: with source_util.create_modules('_temp') as mapping:
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
loader.load_module('_temp') loader.load_module('_temp')
bytecode_path = source_util.bytecode_path(mapping['_temp']) bytecode_path = source_util.bytecode_path(mapping['_temp'])
assertion(bytecode_path) assertion(bytecode_path)
...@@ -144,7 +144,7 @@ class BadDataTest(unittest.TestCase): ...@@ -144,7 +144,7 @@ class BadDataTest(unittest.TestCase):
with open(bytecode_path, 'r+b') as file: with open(bytecode_path, 'r+b') as file:
file.seek(0) file.seek(0)
file.write(b'\x00\x00\x00\x00') file.write(b'\x00\x00\x00\x00')
loader = importlib._PyFileLoader('_temp', mapping['_temp'], False) loader = importlib.PyPycFileLoader('_temp', mapping['_temp'], False)
self.assertRaises(ImportError, loader.load_module, '_temp') self.assertRaises(ImportError, loader.load_module, '_temp')
self.assert_('_temp' not in sys.modules) self.assert_('_temp' not in sys.modules)
...@@ -159,7 +159,7 @@ class SourceBytecodeInteraction(unittest.TestCase): ...@@ -159,7 +159,7 @@ class SourceBytecodeInteraction(unittest.TestCase):
""" """
def import_(self, file, module, *, pkg=False): def import_(self, file, module, *, pkg=False):
loader = importlib._PyFileLoader(module, file, pkg) loader = importlib.PyPycFileLoader(module, file, pkg)
return loader.load_module(module) return loader.load_module(module)
def run_test(self, test, *create, pkg=False): def run_test(self, test, *create, pkg=False):
...@@ -171,7 +171,7 @@ class SourceBytecodeInteraction(unittest.TestCase): ...@@ -171,7 +171,7 @@ class SourceBytecodeInteraction(unittest.TestCase):
import_name = test.rsplit('.', 1)[0] import_name = test.rsplit('.', 1)[0]
else: else:
import_name = test import_name = test
loader = importlib._PyFileLoader(import_name, mapping[test], pkg) loader = importlib.PyPycFileLoader(import_name, mapping[test], pkg)
# Because some platforms only have a granularity to the second for # Because some platforms only have a granularity to the second for
# atime you can't check the physical files. Instead just make it an # atime you can't check the physical files. Instead just make it an
# exception trigger if source was read. # exception trigger if source was read.
...@@ -212,7 +212,7 @@ class BadBytecodeTest(unittest.TestCase): ...@@ -212,7 +212,7 @@ class BadBytecodeTest(unittest.TestCase):
""" """
def import_(self, file, module_name): def import_(self, file, module_name):
loader = importlib._PyFileLoader(module_name, file, False) loader = importlib.PyPycFileLoader(module_name, file, False)
module = loader.load_module(module_name) module = loader.load_module(module_name)
self.assert_(module_name in sys.modules) self.assert_(module_name in sys.modules)
......
...@@ -35,7 +35,7 @@ class EncodingTest(unittest.TestCase): ...@@ -35,7 +35,7 @@ class EncodingTest(unittest.TestCase):
with source_util.create_modules(self.module_name) as mapping: with source_util.create_modules(self.module_name) as mapping:
with open(mapping[self.module_name], 'wb')as file: with open(mapping[self.module_name], 'wb')as file:
file.write(source) file.write(source)
loader = importlib._PyFileLoader(self.module_name, loader = importlib.PyPycFileLoader(self.module_name,
mapping[self.module_name], False) mapping[self.module_name], False)
return loader.load_module(self.module_name) return loader.load_module(self.module_name)
...@@ -96,7 +96,7 @@ class LineEndingTest(unittest.TestCase): ...@@ -96,7 +96,7 @@ class LineEndingTest(unittest.TestCase):
with source_util.create_modules(module_name) as mapping: with source_util.create_modules(module_name) as mapping:
with open(mapping[module_name], 'wb') as file: with open(mapping[module_name], 'wb') as file:
file.write(source) file.write(source)
loader = importlib._PyFileLoader(module_name, mapping[module_name], loader = importlib.PyPycFileLoader(module_name, mapping[module_name],
False) False)
return loader.load_module(module_name) return loader.load_module(module_name)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment