variables bound in moudules are None when module is not completely imported

chrysn at chrysn at
Tue Feb 24 09:27:19 EST 2009

when a module being loaded is interrupted by an exception, code blocks
that have the module's scope in their environment will later evaluate
variables bound to that module to None, instead of preserving that scope
(because it is still referenced somewhere) or raising a NameError (for
which i see no reason, but which would still be better than silently
returning None).

this can happen, for example, when one tries to join different programs
in one main loop by having the modules first hook up to events and then
terminating the subprograms by replacing the call to the main loop with
an exception raiser.

attached, there is a tarball with two similar examples, one
demonstrating that with functions instead of modules (this behaves as
expected; ./function/), the other showing that when a module's block is
terminated, the remaining variables are set to None (./module/); both
can be run by calling `python` in the respective directory.

* is this a bug in the pythin implementation? (i've tested it both with
  2.5 and with 3.0b2)
* if yes, is it identical to [1748015]? (i suppose it is, but lacking
  experience with pdb i am not sure)
* can / will this be fixed?
* is there a workaround?
    * especially, is there a workaround that works w/o rewriting the
      modules that raise the exceptions? (otherwise, wrapping all the
      stuff called in the __name__=="__main__" wrapper into a main()
      function and then calling that would trivially solve that)

answers to any of these questions would be very much appreciated



To use raw power is to make yourself infinitely vulnerable to greater powers.
  -- Bene Gesserit axiom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: module_none.tar.bz2
Type: application/octet-stream
Size: 1191 bytes
Desc: not available
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: Digital signature
URL: <>

More information about the Python-list mailing list