The Voidspace Techie Blog

Gravatar Keep an extra reference yourself where, exactly? How do you ensure that the garbage collector won't detect the reference as cyclic and clean the script up anyway, given enough time?


Gravatar Hmm... only in the sense that *all* references are cyclic when it comes down to it.

Adding 'import __main__' works, even for long running processes.

I guess that even though the module object is garbage collected, the other objects referenced inside the module aren't. So I think that this a specific bug.

I need this in order to be able to exec ordinary unittest scripts unmodified. unittest imports '__main__' and pulls the tests in from that, so I need to be able to replace __main__ in sys.modules.


Gravatar FWIW I get a NameError: name 'x' is not defined when I try this code on Python 2.4.4 and 2.5 on Linux.


Gravatar Hmm... with Python 2.5 on Linux I get a NameError even when keeping a reference around. It looks like the grabage collection is a bit more aggressive on Linux.


Gravatar At least for me with Python 2.4 on Linux, I get different results depending on whether I am doing this at an interpreter prompt or in a function (or at the top level of a file). At the prompt, I get a NameError; in a function, I get None.

(The reasons behind the difference are interesting but do not really fit in this margin, so I wrote a page about it here.)


Gravatar Ahh yes. I forgot that I'd seen different results when run from the interpreter. Thanks for the explanation on your page as well.


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan