Technique for safely reloading dynamically generated module
Terry Reedy
tjreedy at udel.edu
Thu Jul 21 13:32:36 EDT 2016
On 7/21/2016 12:07 PM, Malcolm Greene wrote:
> I assume that one downside to the exec() approach is that there is no
> persistent namespace for this code's functions and classes, eg. after
> the exec() completes, its namespace disappears and is not available to
> code that follows?
Objects only disappear when no references are left. So it is up to you
whether you drop the namespace and use a new one for the next exec call
or whether you use the same namespace over and over. IDLE's run module
does the latter. Simplified, IDLE's Shell simulates the interactive
interpreter by sending user input to this code in the run module.
main = fake_main() # with __name__ = '__main__', __builtins__ = ...
while True:
try:
code = user_input()
exec(code, main)
except BaseException as e:
handle_exception(e)
--
Terry Jan Reedy
More information about the Python-list
mailing list