Problem with module variables / module deallocation . . .

alejandro david weil aweil at mail.ru
Tue May 11 23:26:36 EDT 2004


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.






More information about the Python-list mailing list