Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(185731)

Side by Side Diff: Lib/importlib/_bootstrap.py

Issue 23014: Don't have importlib.abc.Loader.create_module() be optional
Patch Set: Created 4 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Core implementation of import. 1 """Core implementation of import.
2 2
3 This module is NOT meant to be directly imported! It has been designed such 3 This module is NOT meant to be directly imported! It has been designed such
4 that it can be bootstrapped into Python as the implementation of import. As 4 that it can be bootstrapped into Python as the implementation of import. As
5 such it requires the injection of specific modules and attributes in order to 5 such it requires the injection of specific modules and attributes in order to
6 work. One should use importlib as the public-facing version of this module. 6 work. One should use importlib as the public-facing version of this module.
7 7
8 """ 8 """
9 # 9 #
10 # IMPORTANT: Whenever making changes to this module, be sure to run 10 # IMPORTANT: Whenever making changes to this module, be sure to run
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after
1048 1048
1049 1049
1050 def module_from_spec(spec): 1050 def module_from_spec(spec):
1051 """Create a module based on the provided spec.""" 1051 """Create a module based on the provided spec."""
1052 # Typically loaders will not implement create_module(). 1052 # Typically loaders will not implement create_module().
1053 module = None 1053 module = None
1054 if hasattr(spec.loader, 'create_module'): 1054 if hasattr(spec.loader, 'create_module'):
1055 # If create_module() returns `None` then it means default 1055 # If create_module() returns `None` then it means default
1056 # module creation should be used. 1056 # module creation should be used.
1057 module = spec.loader.create_module(spec) 1057 module = spec.loader.create_module(spec)
1058 elif hasattr(spec.loader, 'exec_module'):
1059 _warnings.warn('starting in Python 3.6, loaders defining exec_module() '
1060 'must also define create_module()',
1061 DeprecationWarning)
berkerpeksag 2014/12/12 20:07:45 Shouldn't the stacklevel parameter be 2 here?
brett.cannon 2014/12/15 15:54:58 It's a bit tricky to get right. Yes, it should at
brett.cannon 2015/01/09 17:28:56 Done.
1058 if module is None: 1062 if module is None:
1059 module = _new_module(spec.name) 1063 module = _new_module(spec.name)
1060 _init_module_attrs(spec, module) 1064 _init_module_attrs(spec, module)
1061 return module 1065 return module
1062 1066
1063 1067
1064 def _module_repr_from_spec(spec): 1068 def _module_repr_from_spec(spec):
1065 """Return the repr to use for the module.""" 1069 """Return the repr to use for the module."""
1066 # We mostly replicate _module_repr() using the spec attributes. 1070 # We mostly replicate _module_repr() using the spec attributes.
1067 name = '?' if spec.name is None else spec.name 1071 name = '?' if spec.name is None else spec.name
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 return None 1294 return None
1291 1295
1292 @classmethod 1296 @classmethod
1293 def find_module(cls, fullname, path=None): 1297 def find_module(cls, fullname, path=None):
1294 """Find a frozen module. 1298 """Find a frozen module.
1295 1299
1296 This method is deprecated. Use find_spec() instead. 1300 This method is deprecated. Use find_spec() instead.
1297 1301
1298 """ 1302 """
1299 return cls if _imp.is_frozen(fullname) else None 1303 return cls if _imp.is_frozen(fullname) else None
1304
1305 @staticmethod
eric.snow 2014/12/16 03:42:16 Out of curiousity, what is the advantage of static
brett.cannon 2014/12/16 16:04:13 I don't explicitly remember him saying that, but f
brett.cannon 2015/01/09 17:28:56 Went ahead and made it a classmethod.
1306 def create_module(spec):
1307 """Use default semantics for module creation."""
1300 1308
1301 @staticmethod 1309 @staticmethod
1302 def exec_module(module): 1310 def exec_module(module):
1303 name = module.__spec__.name 1311 name = module.__spec__.name
1304 if not _imp.is_frozen(name): 1312 if not _imp.is_frozen(name):
1305 raise ImportError('{!r} is not a frozen module'.format(name), 1313 raise ImportError('{!r} is not a frozen module'.format(name),
1306 name=name) 1314 name=name)
1307 code = _call_with_frames_removed(_imp.get_frozen_object, name) 1315 code = _call_with_frames_removed(_imp.get_frozen_object, name)
1308 exec(code, module.__dict__) 1316 exec(code, module.__dict__)
1309 1317
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1403 """Base class of common code needed by both SourceLoader and 1411 """Base class of common code needed by both SourceLoader and
1404 SourcelessFileLoader.""" 1412 SourcelessFileLoader."""
1405 1413
1406 def is_package(self, fullname): 1414 def is_package(self, fullname):
1407 """Concrete implementation of InspectLoader.is_package by checking if 1415 """Concrete implementation of InspectLoader.is_package by checking if
1408 the path returned by get_filename has a filename of '__init__.py'.""" 1416 the path returned by get_filename has a filename of '__init__.py'."""
1409 filename = _path_split(self.get_filename(fullname))[1] 1417 filename = _path_split(self.get_filename(fullname))[1]
1410 filename_base = filename.rsplit('.', 1)[0] 1418 filename_base = filename.rsplit('.', 1)[0]
1411 tail_name = fullname.rpartition('.')[2] 1419 tail_name = fullname.rpartition('.')[2]
1412 return filename_base == '__init__' and tail_name != '__init__' 1420 return filename_base == '__init__' and tail_name != '__init__'
1421
1422 def create_module(self, spec):
1423 """Use default semantics for module creation."""
1413 1424
1414 def exec_module(self, module): 1425 def exec_module(self, module):
1415 """Execute the module.""" 1426 """Execute the module."""
1416 code = self.get_code(module.__name__) 1427 code = self.get_code(module.__name__)
1417 if code is None: 1428 if code is None:
1418 raise ImportError('cannot load module {!r} when get_code() ' 1429 raise ImportError('cannot load module {!r} when get_code() '
1419 'returns None'.format(module.__name__)) 1430 'returns None'.format(module.__name__))
1420 _call_with_frames_removed(exec, code, module.__dict__) 1431 _call_with_frames_removed(exec, code, module.__dict__)
1421 1432
1422 load_module = _load_module_shim 1433 load_module = _load_module_shim
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1763 return '<module {!r} (namespace)>'.format(module.__name__) 1774 return '<module {!r} (namespace)>'.format(module.__name__)
1764 1775
1765 def is_package(self, fullname): 1776 def is_package(self, fullname):
1766 return True 1777 return True
1767 1778
1768 def get_source(self, fullname): 1779 def get_source(self, fullname):
1769 return '' 1780 return ''
1770 1781
1771 def get_code(self, fullname): 1782 def get_code(self, fullname):
1772 return compile('', '<string>', 'exec', dont_inherit=True) 1783 return compile('', '<string>', 'exec', dont_inherit=True)
1784
1785 def create_module(self, spec):
1786 """Use default semantics for module creation."""
1773 1787
1774 def exec_module(self, module): 1788 def exec_module(self, module):
1775 pass 1789 pass
1776 1790
1777 def load_module(self, fullname): 1791 def load_module(self, fullname):
1778 """Load a namespace module. 1792 """Load a namespace module.
1779 1793
1780 This method is deprecated. Use exec_module() instead. 1794 This method is deprecated. Use exec_module() instead.
1781 1795
1782 """ 1796 """
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after
2404 def _install(sys_module, _imp_module): 2418 def _install(sys_module, _imp_module):
2405 """Install importlib as the implementation of import.""" 2419 """Install importlib as the implementation of import."""
2406 _setup(sys_module, _imp_module) 2420 _setup(sys_module, _imp_module)
2407 supported_loaders = _get_supported_file_loaders() 2421 supported_loaders = _get_supported_file_loaders()
2408 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)]) 2422 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
2409 sys.meta_path.append(BuiltinImporter) 2423 sys.meta_path.append(BuiltinImporter)
2410 sys.meta_path.append(FrozenImporter) 2424 sys.meta_path.append(FrozenImporter)
2411 if _os.__name__ == 'nt': 2425 if _os.__name__ == 'nt':
2412 sys.meta_path.append(WindowsRegistryFinder) 2426 sys.meta_path.append(WindowsRegistryFinder)
2413 sys.meta_path.append(PathFinder) 2427 sys.meta_path.append(PathFinder)
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+