[Python-3000-checkins] r53915 - python/branches/p3yk/Lib/xreload.py
guido.van.rossum
python-3000-checkins at python.org
Sun Feb 25 22:22:22 CET 2007
Author: guido.van.rossum
Date: Sun Feb 25 22:22:21 2007
New Revision: 53915
Modified:
python/branches/p3yk/Lib/xreload.py
Log:
Use Glyph's trick to ensure that __globals__ is set properly.
Modified: python/branches/p3yk/Lib/xreload.py
==============================================================================
--- python/branches/p3yk/Lib/xreload.py (original)
+++ python/branches/p3yk/Lib/xreload.py Sun Feb 25 22:22:21 2007
@@ -55,22 +55,22 @@
finally:
if stream:
stream.close()
- # Execute the code in a temporary namespace; if this fails, no changes
- tmpns = {}
- exec(code, tmpns)
+ # Execute the code. We copy the module dict to a temporary; then
+ # clear the module dict; then execute the new code in the module
+ # dict; then swap things back and around. This trick (due to
+ # Glyph Lefkowitz) ensures that the (readonly) __globals__
+ # attribute of methods and functions is set to the correct dict
+ # object.
+ tmpns = modns.copy()
+ modns.clear()
+ modns["__name__"] = tmpns["__name__"]
+ exec(code, modns)
# Now we get to the hard part
- oldnames = set(modns)
- newnames = set(tmpns)
- # Add newly introduced names
- for name in newnames - oldnames:
- modns[name] = tmpns[name]
- # Delete names that are no longer current
- # XXX What to do about renamed objects?
- for name in oldnames - newnames - {"__name__"}:
- del modns[name]
- # Now update the rest in place
+ oldnames = set(tmpns)
+ newnames = set(modns)
+ # Update attributes in place
for name in oldnames & newnames:
- modns[name] = _update(modns[name], tmpns[name])
+ modns[name] = _update(tmpns[name], modns[name])
# Done!
return mod
More information about the Python-3000-checkins
mailing list