Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111727

Re: Technique for safely reloading dynamically generated module

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Technique for safely reloading dynamically generated module
Date 2016-07-21 13:32 -0400
Message-ID <mailman.36.1469122373.22221.python-list@python.org> (permalink)
References <1469112141.379402.672830825.05659985@webmail.messagingengine.com> <nmqqd2$lcd$1@ger.gmane.org> <1469117265.403838.672932729.2B34F311@webmail.messagingengine.com> <nmr0vl$5e5$1@ger.gmane.org>

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Technique for safely reloading dynamically generated module Terry Reedy <tjreedy@udel.edu> - 2016-07-21 13:32 -0400

csiph-web