Problem with module variables / module deallocation . . .

  • Thread starter alejandro david weil
  • Start date
A

alejandro david weil

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?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top