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

Delta Between Two Patch Sets: Lib/importlib/__init__.py

Issue 19413: Reload semantics changed unexpectedly in Python 3.3
Left Patch Set: Created 6 years, 4 months ago
Right Patch Set: Created 6 years, 3 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/importlib/_bootstrap.py ('k') | Lib/test/test_importlib/test_api.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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)
121 if not hasattr(module, '__loader__'): 122 module.__loader__ = _bootstrap._find_module(name, None)
122 module.__loader__ = _bootstrap._find_module(module.__name__, None)
123 module.__loader__.load_module(name) 123 module.__loader__.load_module(name)
124 # The module may have replaced itself in sys.modules! 124 # The module may have replaced itself in sys.modules!
125 return sys.modules[module.__name__] 125 return sys.modules[module.__name__]
126 finally: 126 finally:
127 try: 127 try:
128 del _RELOADING[name] 128 del _RELOADING[name]
129 except KeyError: 129 except KeyError:
130 pass 130 pass
LEFTRIGHT

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