Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81445

Re: unloading a module created with imp.new_module

Subject Re: unloading a module created with imp.new_module
From Patrick Stinson <patrickkidd@gmail.com>
Date 2014-11-23 01:10 -0900
References <C093BC09-3AB7-49DB-B160-265A34C6BCDA@gmail.com> <CALwzidnHV8smFHtvixtdhX-i94PWbwNKSRrR6HAfhn2HOKqxsw@mail.gmail.com> <CALwzid=9Zgu3svDP-mv8-1DrZkKkDcNTxy+D=obwGsOGzB5aZQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16233.1416816962.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

I am defining a single class with a destructor method that prints ‘__del__’, and running that source code string using exec with the module’s dict like so:

import rtmidi
import sys
import types
import time
import gc

s = """                                                                                       
class A:                                                                                      
    def __del__(self):                                                                        
        print('__del__')                                                                      
a = A()                                                                                       
"""

m = types.ModuleType('mine')
exec(s, m.__dict__)
print('deleting...')
m = None
print('done')

and the output is:

deleting...
done
__del__

I the “__del__" to come between “deleting…” and “done”. This is not being run from the interactive interpreter by via a .py file.

> On Nov 23, 2014, at 12:56 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 
> On Sun, Nov 23, 2014 at 2:48 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>> On Sat, Nov 22, 2014 at 11:49 PM, Patrick Stinson <patrickkidd@gmail.com> wrote:
>>> If I create a module with imp.new_module(name), how can I unload it so that all the references contained in it are set to zero and the module is deleted? deleting the reference that is returned doesn’t seem to do the job, and it’s not in sys.modules, so where is the dangling reference?
>> 
>> How are you determining that the module is not deleted?
> 
> From my testing, using Python 3.4:
> 
>>>> for i in range(5): imp.new_module('spam')
> ...
> <module 'spam'>
> <module 'spam'>
> <module 'spam'>
> <module 'spam'>
> <module 'spam'>
>>>> import gc, types
>>>> [m for m in gc.get_objects() if isinstance(m, types.ModuleType) and m.__name__ == 'spam']
> [<module 'spam'>]
>>>> 42
> 42
>>>> [m for m in gc.get_objects() if isinstance(m, types.ModuleType) and m.__name__ == 'spam']
> []
> 
> In this case one of the created modules was hanging around because it
> was still referenced by the special "_" variable of the interactive
> interpreter. Evaluating a new expression cleared that out and deleted
> the module. Maybe you're seeing the same thing.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: unloading a module created with imp.new_module Patrick Stinson <patrickkidd@gmail.com> - 2014-11-23 01:10 -0900

csiph-web