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: 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>': 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 In-Reply-To: Date: Sun, 23 Nov 2014 01:10:21 -0900 References: To: Ian Kelly X-Mailer: Apple Mail (2.1993) X-Mailman-Approved-At: Mon, 24 Nov 2014 09:16:01 +0100 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --Apple-Mail=_EA22E60D-8009-42BB-BC44-98503C3DC7E7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I am defining a single class with a destructor method that prints = =E2=80=98__del__=E2=80=99, and running that source code string using = exec with the module=E2=80=99s dict like so: import rtmidi import sys import types import time import gc s =3D """ = =20 class A: = =20 def __del__(self): = =20 print('__del__') = =20 a =3D A() = =20 """ m =3D types.ModuleType('mine') exec(s, m.__dict__) print('deleting...') m =3D None print('done') and the output is: deleting... done __del__ I the =E2=80=9C__del__" to come between =E2=80=9Cdeleting=E2=80=A6=E2=80=9D= and =E2=80=9Cdone=E2=80=9D. This is not being run from the interactive = interpreter by via a .py file. > On Nov 23, 2014, at 12:56 AM, Ian Kelly wrote: >=20 > On Sun, Nov 23, 2014 at 2:48 AM, Ian Kelly = wrote: >> On Sat, Nov 22, 2014 at 11:49 PM, Patrick Stinson = 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=E2=80=99t= seem to do the job, and it=E2=80=99s not in sys.modules, so where is = the dangling reference? >>=20 >> How are you determining that the module is not deleted? >=20 > =46rom my testing, using Python 3.4: >=20 >>>> for i in range(5): imp.new_module('spam') > ... > > > > > >>>> import gc, types >>>> [m for m in gc.get_objects() if isinstance(m, types.ModuleType) and = m.__name__ =3D=3D 'spam'] > [] >>>> 42 > 42 >>>> [m for m in gc.get_objects() if isinstance(m, types.ModuleType) and = m.__name__ =3D=3D 'spam'] > [] >=20 > 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. > --=20 > https://mail.python.org/mailman/listinfo/python-list --Apple-Mail=_EA22E60D-8009-42BB-BC44-98503C3DC7E7 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 I am defining a single class with a destructor method that = prints =E2=80=98__del__=E2=80=99, and running that source code string = using exec with the module=E2=80=99s dict like so:

import rtmidi
import sys
import = types
import time
import gc

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

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

and the output = is:

deleting...
done
__del__

I the =E2=80=9C__del__" = to come between =E2=80=9Cdeleting=E2=80=A6=E2=80=9D and =E2=80=9Cdone=E2=80= =9D. 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=E2=80=99t seem to do the job, and it=E2=80=99s not in = sys.modules, so where is the dangling reference?

How are you determining that the = module is not deleted?

=46rom = 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__ =3D=3D 'spam']
[<module = 'spam'>]
42
42
[m for m in = gc.get_objects() if isinstance(m, types.ModuleType) and m.__name__ =3D=3D = '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

= --Apple-Mail=_EA22E60D-8009-42BB-BC44-98503C3DC7E7--