Problem with module variables / module deallocation . . .

Discussion in 'Python' started by alejandro david weil, May 12, 2004.

  1. 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.
     
    alejandro david weil, May 12, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    772
    John Harrison
    Mar 7, 2004
  2. Replies:
    2
    Views:
    757
    Paavo Helde
    Dec 18, 2004
  3. Andy Chang

    Vector pointer and deallocation

    Andy Chang, Jan 4, 2005, in forum: C++
    Replies:
    7
    Views:
    8,237
    Ron Natalie
    Jan 5, 2005
  4. BigBrian
    Replies:
    12
    Views:
    604
    Pete Becker
    Apr 7, 2005
  5. Nicolas Matringe

    std.textio, readline and memory deallocation

    Nicolas Matringe, Sep 1, 2006, in forum: VHDL
    Replies:
    9
    Views:
    2,232
    Paul Uiterlinden
    Sep 4, 2006
Loading...

Share This Page