Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; '12px;': 0.05; 'interpreter': 0.05; 'output': 0.05; '"""': 0.07; 'matches': 0.07; 'think,': 0.07; 'string': 0.09; '"""class': 0.09; 'editor.': 0.09; 'exec': 0.09; 'objects,': 0.09; 'subject:module': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'gui': 0.12; 'question.': 0.14; 'thread': 0.14; '.py': 0.16; '24,': 0.16; '__del__': 0.16; 'a()': 0.16; 'dict': 0.16; 'hierarchy.': 0.16; 'interpreter,': 0.16; 'subject:created': 0.16; 'helvetica;': 0.16; 'wrote:': 0.18; 'app': 0.19; 'module': 0.19; 'patrick': 0.19; 'examples': 0.20; 'seems': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; 'source': 0.25; 'script': 0.25; 'define': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'url:mailman': 0.30; '(which': 0.31; 'code': 0.31; 'app.': 0.31; 'away.': 0.31; 'fault': 0.31; 'implicit': 0.31; 'prints': 0.31; 'subject:skip:i 10': 0.31; 'allows': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'thanks!': 0.32; 'run': 0.32; 'another': 0.32; 'url:python': 0.33; 'running': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'created': 0.35; 'except': 0.35; 'something': 0.35; 'objects': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'done,': 0.36; 'done': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'url:org': 0.36; 'skip:- 20': 0.37; 'being': 0.38; 'auto;': 0.38; 'message- id:@gmail.com': 0.38; 'nov': 0.38; 'expect': 0.39; 'delete': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'users': 0.40; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'break': 0.61; 'skip:t 30': 0.61; 'new': 0.61; 'header:Message-Id:1': 0.63; 'more': 0.64; 'great': 0.65; 'charset:windows-1252': 0.65; 'within': 0.65; 'between': 0.67; 'inline': 0.74; '2014,': 0.84; 'batchelder': 0.84; 'dict.': 0.84; 'doesn\x92t': 0.84; 'etc),': 0.84; 'it\x92s': 0.84; 'i\x92ve': 0.84; 'plugins': 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=E1vdD1B63NgSy49LAquwIv9aKARXwEInnQfY1HoJ9x0=; b=CvatUVViwIYI4H3vNeaThEcnTOQ8MKVjT6P32z3HaGOIBITqN6lU/I5IFUq31rSXQT 8yn4BQyP55o88aMRFWABAvRJO/jiT5ZDvMp8Ko17VSzu9Lm0pBAjvijlPwsqsc6jjeER d59aNJpIN5XUfHVrTNwpHACUhV7HPyeDsZ2nElA+kNxQZneQeZ2rhhzTidsLKodOZJ7u TBdtcJNfiMZvWUcwDWcuL0ZkE+cyNlzc+TEHHMsyJ7I2Ce7c5wgAOaL7T0IdEjg2TeMp 1xsFFwYlKlDKJYUlBROj6KoC6vrVA+NLZYJJUQy2JVS4lohmwCTsYxWgOON+t4YRiQzx 6VXA== X-Received: by 10.67.14.71 with SMTP id fe7mr40310370pad.27.1416898567781; Mon, 24 Nov 2014 22:56:07 -0800 (PST) Content-Type: multipart/alternative; boundary="Apple-Mail=_743BE199-2D51-4058-8258-9E1A8A1DAC6D" 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: Mon, 24 Nov 2014 21:56:05 -0900 References: To: Ned Batchelder X-Mailer: Apple Mail (2.1993) Cc: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 228 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416898576 news.xs4all.nl 2846 [2001:888:2000:d::a6]:53913 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81491 --Apple-Mail=_743BE199-2D51-4058-8258-9E1A8A1DAC6D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 > On Nov 24, 2014, at 2:44 AM, Ned Batchelder = wrote: >=20 > On 11/23/14 5:10 AM, Patrick Stinson wrote: >> I am defining a single class with a destructor method that prints >> =91__del__=92, and running that source code string using exec with = the >> module=92s dict like so: >>=20 >> import rtmidi >> importsys >> import types >> importtime >> importgc >>=20 >> s=3D """ >> class A: >> def __del__(self): >> print('__del__') >> a =3D A() >> """ >>=20 >> m =3D types.ModuleType('mine') >> exec(s, m.__dict__) >> print('deleting...') >> m=3D None >> print('done') >>=20 >> and the output is: >>=20 >> deleting... >> done >> __del__ >>=20 >> I the =93__del__" to come between =93deleting=85=94 and =93done=94. = This is not >> being run from the interactive interpreter by via a .py file. >>=20 >=20 > Let's look at this another way: Why do you need the module to be = unloaded? Isn't it enough to have the new code loaded? I think that=92s a good question. My original intention was to delete = the module and everything contained in it since that=92s what best = matches what the user would expect by recompiling a script that they = just created within this app. But the more I read and think, this purist = perspective doesn=92t make much sense. The solution that I=92ve come up with (which seems to be working really = well so far), is to delete all objects in the module=92s dict except the = usual implicit objects (__import__, __name__, etc), then re-compile the = code into the module=92s dict. This even works well when I define = QWidget gui objects, as they are hidden and deleted when the last = reference goes away. The main drawbacks for this approach are that the scripts exist in the = same interpreter, and so can directly manipulate the app=92s object = hierarchy. But a user would have to go out of their way to do that so = it=92s their own fault if they break something :) I would *love* to see code examples of how this sort of thing is done, = where you write a GUI app in python that allows it=92s users to write = plugins for it in python using a built-in editor. Thanks! Great thread >=20 > --=20 > Ned Batchelder, http://nedbatchelder.com >=20 > --=20 > https://mail.python.org/mailman/listinfo/python-list = --Apple-Mail=_743BE199-2D51-4058-8258-9E1A8A1DAC6D Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=windows-1252
On Nov 24, 2014, at 2:44 AM, Ned Batchelder <ned@nedbatchelder.com> wrote:

On 11/23/14 5:10 AM, Patrick Stinson = wrote:
I am defining a single class with a destructor method = that prints
=91__del__=92, and running that source code = string using exec with the
module=92s dict like so:

import rtmidi
importsys
import types
importtime
importgc
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 =93__del__" to = come between =93deleting=85=94 and =93done=94. This is not
being run from the interactive interpreter by via a .py = file.


Let's look at this another way: Why do you need = the module to be unloaded?  Isn't it enough to have the new code = loaded?

I think = that=92s a good question. My original intention was to delete the module = and everything contained in it since that=92s what best matches what the = user would expect by recompiling a script that they just created within = this app. But the more I read and think, this purist perspective doesn=92t= make much sense.

The solution that = I=92ve come up with (which seems to be working really well so far), is = to delete all objects in the module=92s dict except the usual implicit = objects (__import__, __name__, etc), then re-compile the code into the = module=92s dict. This even works well when I define QWidget gui objects, = as they are hidden and deleted when the last reference goes = away.

The main drawbacks for this = approach are that the scripts exist in the same interpreter, and so can = directly manipulate the app=92s object hierarchy. But a user would have = to go out of their way to do that so it=92s their own fault if they = break something :)

I would *love* to = see code examples of how this sort of thing is done, where you write a = GUI app in python that allows it=92s users to write plugins for it in = python using a built-in editor.

Thanks! Great thread


<= /blockquote>

= --Apple-Mail=_743BE199-2D51-4058-8258-9E1A8A1DAC6D--