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

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

Issue 19413: Reload semantics changed unexpectedly in Python 3.3
Patch Set: Created 6 years, 4 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
« no previous file with comments | « Lib/importlib/_bootstrap.py ('k') | Lib/test/test_importlib/test_api.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """A pure Python implementation of import.""" 1 """A pure Python implementation of import."""
2 __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload'] 2 __all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload']
3 3
4 # Bootstrap help ##################################################### 4 # Bootstrap help #####################################################
5 5
6 # Until bootstrapping is complete, DO NOT import any modules that attempt 6 # Until bootstrapping is complete, DO NOT import any modules that attempt
7 # to import importlib._bootstrap (directly or indirectly). Since this 7 # to import importlib._bootstrap (directly or indirectly). Since this
8 # partially initialised package would be present in sys.modules, those 8 # partially initialised package would be present in sys.modules, those
9 # modules would get an uninitialised copy of the source version, instead 9 # modules would get an uninitialised copy of the source version, instead
10 # of a fully initialised version (either the frozen one or the one 10 # of a fully initialised version (either the frozen one or the one
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 _RELOADING = {} 98 _RELOADING = {}
99 99
100 100
101 def reload(module): 101 def reload(module):
102 """Reload the module and return it. 102 """Reload the module and return it.
103 103
104 The module must have been successfully imported before. 104 The module must have been successfully imported before.
105 105
106 """ 106 """
107 # XXX Is this check wrong?
107 if not module or not isinstance(module, types.ModuleType): 108 if not module or not isinstance(module, types.ModuleType):
108 raise TypeError("reload() argument must be module") 109 raise TypeError("reload() argument must be module")
109 name = module.__name__ 110 name = module.__name__
110 if name not in sys.modules: 111 if sys.modules.get(name) is not module:
111 msg = "module {} not in sys.modules" 112 msg = "module {} not in sys.modules"
112 raise ImportError(msg.format(name), name=name) 113 raise ImportError(msg.format(name), name=name)
113 if name in _RELOADING: 114 if name in _RELOADING:
114 return _RELOADING[name] 115 return _RELOADING[name]
115 _RELOADING[name] = module 116 _RELOADING[name] = module
116 try: 117 try:
117 parent_name = name.rpartition('.')[0] 118 parent_name = name.rpartition('.')[0]
118 if parent_name and parent_name not in sys.modules: 119 if parent_name and parent_name not in sys.modules:
119 msg = "parent {!r} not in sys.modules" 120 msg = "parent {!r} not in sys.modules"
120 raise ImportError(msg.format(parent_name), name=parent_name) 121 raise ImportError(msg.format(parent_name), name=parent_name)
122 module.__loader__ = _bootstrap._find_module(name, None)
121 module.__loader__.load_module(name) 123 module.__loader__.load_module(name)
122 # The module may have replaced itself in sys.modules! 124 # The module may have replaced itself in sys.modules!
123 return sys.modules[module.__name__] 125 return sys.modules[module.__name__]
124 finally: 126 finally:
125 try: 127 try:
126 del _RELOADING[name] 128 del _RELOADING[name]
127 except KeyError: 129 except KeyError:
128 pass 130 pass
OLDNEW
« no previous file with comments | « Lib/importlib/_bootstrap.py ('k') | Lib/test/test_importlib/test_api.py » ('j') | no next file with comments »

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