Commit 5c0a6de7 authored by Guido van Rossum's avatar Guido van Rossum

Use Glyph's trick to ensure that __globals__ is set properly.

parent 55b4a4a1
...@@ -55,22 +55,22 @@ def xreload(mod): ...@@ -55,22 +55,22 @@ def xreload(mod):
finally: finally:
if stream: if stream:
stream.close() stream.close()
# Execute the code in a temporary namespace; if this fails, no changes # Execute the code. We copy the module dict to a temporary; then
tmpns = {} # clear the module dict; then execute the new code in the module
exec(code, tmpns) # 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 # Now we get to the hard part
oldnames = set(modns) oldnames = set(tmpns)
newnames = set(tmpns) newnames = set(modns)
# Add newly introduced names # Update attributes in place
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
for name in oldnames & newnames: for name in oldnames & newnames:
modns[name] = _update(modns[name], tmpns[name]) modns[name] = _update(tmpns[name], modns[name])
# Done! # Done!
return mod return mod
......
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