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


Groups > comp.lang.python > #81491

Re: unloading a module created with imp.new_module

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 <patrickkidd@gmail.com>
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 <patrickkidd@gmail.com>
In-Reply-To <m4v5m5$ns5$1@ger.gmane.org>
Date Mon, 24 Nov 2014 21:56:05 -0900
References <C093BC09-3AB7-49DB-B160-265A34C6BCDA@gmail.com> <CALwzidnHV8smFHtvixtdhX-i94PWbwNKSRrR6HAfhn2HOKqxsw@mail.gmail.com> <CALwzid=9Zgu3svDP-mv8-1DrZkKkDcNTxy+D=obwGsOGzB5aZQ@mail.gmail.com> <B8391E6B-5AA4-4125-96BC-74B12B46E927@gmail.com> <m4v5m5$ns5$1@ger.gmane.org>
To Ned Batchelder <ned@nedbatchelder.com>
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 <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.16269.1416898576.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

> 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
>> ‘__del__’, and running that source code string using exec with the
>> module’s dict like so:
>> 
>> import rtmidi
>> importsys
>> import types
>> importtime
>> importgc
>> 
>> 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.
>> 
> 
> 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’s a good question. My original intention was to delete the module and everything contained in it since that’s 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’t make much sense.

The solution that I’ve come up with (which seems to be working really well so far), is to delete all objects in the module’s dict except the usual implicit objects (__import__, __name__, etc), then re-compile the code into the module’s 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’s object hierarchy. But a user would have to go out of their way to do that so it’s 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’s users to write plugins for it in python using a built-in editor.

Thanks! Great thread


> 
> -- 
> Ned Batchelder, http://nedbatchelder.com <http://nedbatchelder.com/>
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list <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-24 21:56 -0900

csiph-web