Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem with module variables / module deallocation . . .

Thread Tools

Problem with module variables / module deallocation . . .

alejandro david weil
Posts: n/a

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

If we have a module like:

--- --------------->8------
testvar2 = 15

def testfunc():
print testvar2
--- --------------->8------

And if we make:

--- --------------->8------
import sys
import mod

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

or :

--- --------------->8------
import sys

def testloco():
import mod
return mod.testfunc

f = testloco()
del sys.modules['mod']
print 'after module dereferencing..'
--- --------------->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:

--- --------------->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..'
--- --------------->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.

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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
std.textio, readline and memory deallocation Nicolas Matringe VHDL 9 09-04-2006 12:15 PM
memory allocation/deallocation question BigBrian C++ 12 04-07-2005 12:32 AM
Vector pointer and deallocation Andy Chang C++ 7 01-05-2005 03:52 PM
Stack Memory Deallocation Problem C++ 2 12-18-2004 02:35 PM
Automatic memory deallocation by std::string ?? qazmlp C++ 1 03-07-2004 02:00 PM