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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!backfill.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <patrickkidd@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'output': 0.05; '"""': 0.07; 'interpreter.': 0.07; 'sys': 0.07; 'string': 0.09; '22,': 0.09; 'exec': 0.09; 'referenced': 0.09; 'subject:module': 0.09; 'testing,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '"_"': 0.16; '.py': 0.16; '23,': 0.16; '__del__': 0.16; 'a()': 0.16; 'determining': 0.16; 'dict': 0.16; 'evaluating': 0.16; 'subject:created': 0.16; 'unload': 0.16; 'wrote:on': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'patrick': 0.19; '>>>': 0.22; 'import': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'returned': 0.30; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'prints': 0.31; 'subject:skip:i 10': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'run': 0.32; 'url:python': 0.33; 'running': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'subject:with': 0.35; 'created': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'done': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'url:org': 0.36; 'being': 0.38; 'message-id:@gmail.com': 0.38; 'nov': 0.38; 'pm,': 0.38; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'deleting': 0.60; 'expression': 0.60; 'ian': 0.60; 'skip:t 30': 0.61; 'new': 0.61; "you're": 0.61; 'header:Message-Id:1': 0.63; 'skip:\xe2 10': 0.65; 'to:addr:gmail.com': 0.65; 'between': 0.67; '8bit%:43': 0.74; 'special': 0.74; '2014,': 0.84; 'deleted?': 0.84; 'hanging': 0.84; 'it\xe2\x80\x99s': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=COwd8xHK6tgt3+5WyRAhe1GIfIfSUjpKu/lM7i5JR+g=; b=ipXhQuQxAbdU3IAtnetGgAR3j3khilzlKsJDP91N7gD+Ljyg+FNdwbI2Ycp4Jhbvdk anWL7S3ymTZ7C4AiQOYPqwUwhdlRkmEDuaKG0h3uK4uHwonBisvn3uj6PQqoz7A039ya Zoi5D2eFd5NBEp7oNGDTYKAQPSbgsJDXX/59zzmzCKDV4aTMT0wh4VuEwpLloHwxfLYD Nktfuug+8v0VKPb5/cPdp1hBQCkJ/lZrMzD+TKInFYDaeDWp7v6dzDRCgSRo0aLpWM8a prKvq8SoKy9esg+tGXMo15WvwaFIUaojjG3ULo2kG31HU7g5qsZVkgSRAStzNJiklRDr Gsnw==
X-Received by 10.68.203.226 with SMTP id kt2mr452207pbc.141.1416737424007; Sun, 23 Nov 2014 02:10:24 -0800 (PST)
Content-Type multipart/alternative; boundary="Apple-Mail=_EA22E60D-8009-42BB-BC44-98503C3DC7E7"
Mime-Version 1.0 (Mac OS X Mail 8.1 \(1993\))
Subject Re: unloading a module created with imp.new_module
From Patrick Stinson <patrickkidd@gmail.com>
In-Reply-To <CALwzid=9Zgu3svDP-mv8-1DrZkKkDcNTxy+D=obwGsOGzB5aZQ@mail.gmail.com>
Date Sun, 23 Nov 2014 01:10:21 -0900
References <C093BC09-3AB7-49DB-B160-265A34C6BCDA@gmail.com> <CALwzidnHV8smFHtvixtdhX-i94PWbwNKSRrR6HAfhn2HOKqxsw@mail.gmail.com> <CALwzid=9Zgu3svDP-mv8-1DrZkKkDcNTxy+D=obwGsOGzB5aZQ@mail.gmail.com>
To Ian Kelly <ian.g.kelly@gmail.com>
X-Mailer Apple Mail (2.1993)
X-Mailman-Approved-At Mon, 24 Nov 2014 09:16:01 +0100
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16233.1416816962.18130.python-list@python.org> (permalink)
Lines 223
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416816962 news.xs4all.nl 2869 [2001:888:2000:d::a6]:57325
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81445

Show key headers only | 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