By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,400 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,400 IT Pros & Developers. It's quick & easy.

Problem with module variables / module deallocation . . .

P: n/a
Hello!

I got the next problem and didn't see any reference to the behaviour
that produces it, look:

If we have a module like:

--- mod.py --------------->8------
testvar2 = 15

def testfunc():
print testvar2
--- mod.py --------------->8------

And if we make:

--- test1.py --------------->8------
import sys
import mod

f = mod.testfunc
f()
del mod
del sys.modules['mod']
print 'after module dereferencing..'
f()
--- test1.py --------------->8------

or :

--- test2.py --------------->8------
import sys

def testloco():
import mod
return mod.testfunc

f = testloco()
f()
del sys.modules['mod']
print 'after module dereferencing..'
f()
--- test2.py --------------->8------

The last f() prints None.

YES, i know, that usually you are not
removing imports you made.

I'm getting that problem when using
pyhton's encodings/codecs modules.
C codecs module, keeps a reference to
a function in encodings pakage called
search_function, and after getting that
function, the module is auto deallocated
(except from sys.modules).
The application where we are using it,
embeeds python and after my module ends,
it "cleans up" sys.modules dictionary (not in
a clean way it seems), and I can't continue
using encodings later, because, when I
make 'something'.encode('some encoding'),
it searchs using that registered search function
that has problems accessing (invalid) data.

The problem is like this:

--- enc_test.py --------------->8------
import sys

def testencoding():
status = name in sys.modules.keys()
print 'is encodings loaded? ', status
return status

def get_sf():
import encodings
return encodings.search_function
f = get_sf()
del sys.modules['encodings'] #this line is articial, but..
print 'after module dereferencing..'
'bye'.encode('chau')
--- enc_test.py --------------->8------
One way to workaround this problem is that
codecs keeps a reference to encodings module.
There's no way that the function keeps a reference to
the module, to prevent the module's variable destruction?

Well. That's all.
david

PS: Is this the right mailing list for this kind of posts?
--
+ There is no dark side of the moon really. Matter of fact it's all dark.

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.