sys.modules and __main__ obscureness

F

Fuzzyman

Hello all,

I am messing with namespaces, so that code I exec thinks it is
executing in the __main__ module.

I have the following code :

import imp
import sys

# can't call the module '__main__' or 'new_module' returns the real one
module = imp.new_module('_')
namespace = module.__dict__
namespace['__name__'] = '__main__'

# next put things into the names
# e.g. :
namespace['variable'] = 3

sys.modules['__main__'] = module

print module
print namespace
import __main__
print __main__.__dict__['variable']

This code behaves differently when entered into an interactive
interpreter session. When run as a program you will see that module and
namespace have both become None !!

However the code works, but to get access to the namespace again I have
to import __main__. I just wondered why ?


All the best,


Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
K

K.S.Sreeram

Fuzzyman said:
This code behaves differently when entered into an interactive
interpreter session. When run as a program you will see that module and
namespace have both become None !!

Thats because the reference count to the current '__main__' module goes
to 0, and the module object gets deleted along with its globals 'module'
and 'namespace'. The code keeps working because the codeobject is
retained by the interpreter.

To make it work, simply retain a reference to the existing module,
before overwriting with the new module.

sys.blahblah = sys.modules['__main__']
sys.modules['__main__'] = module

[sreeram;]


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEuo1Mrgn0plK5qqURArxlAKCY/AnOz2W0hg408wJ6q4PhbtvoPwCePbTn
EC3vRrcBbaYtSfNdoZy3wVg=
=bzrH
-----END PGP SIGNATURE-----
 
F

Fuzzyman

K.S.Sreeram said:
Fuzzyman said:
This code behaves differently when entered into an interactive
interpreter session. When run as a program you will see that module and
namespace have both become None !!

Thats because the reference count to the current '__main__' module goes
to 0, and the module object gets deleted along with its globals 'module'
and 'namespace'. The code keeps working because the codeobject is
retained by the interpreter.

To make it work, simply retain a reference to the existing module,
before overwriting with the new module.

sys.blahblah = sys.modules['__main__']
sys.modules['__main__'] = module

[sreeram;]

That was a quick response. :)

Thanks very much.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
 
K

K.S.Sreeram

Fuzzyman said:
That was a quick response. :)

Thanks very much.

Sigh.. When I'm drowning in arcane win32 c++ crap, I tend to jump on
anything interesting on python-list. It feels like a breath of fresh air!

[sreeram;]


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEuqUIrgn0plK5qqURAnoTAJ9aulkTgHFZZZbYbsvyKelcLP4C3wCdGwUS
WezD9nYW6AsoU/8ZPTU0SDA=
=1r+I
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top