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


Groups > comp.lang.python > #81441

Re: unloading a module created with imp.new_module

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsfeed.freenet.ag!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed1a.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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'api.': 0.05; 'automate': 0.07; 'source.': 0.07; 'exec': 0.09; 'properly.': 0.09; 'subject:module': 0.09; 'api': 0.11; 'cc:addr :python-list': 0.11; 'python': 0.11; "'__doc__',": 0.16; '23,': 0.16; 'deletes': 0.16; 'dict': 0.16; 'editor,': 0.16; 'plan.': 0.16; 'qscintilla': 0.16; 'saved.': 0.16; 'subject:created': 0.16; 'top-level': 0.16; 'unload': 0.16; 'wrote:on': 0.16; 'wrote:': 0.18; 'app': 0.19; 'module': 0.19; 'patrick': 0.19; 'seems': 0.21; 'example': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'references': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'is?': 0.30; 'list:': 0.30; 'returned': 0.30; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'safely': 0.31; 'subject:skip:i 10': 0.31; 'thanks!': 0.32; 'run': 0.32; 'url:python': 0.33; 'running': 0.33; 'updated': 0.34; 'core': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'received:google.com': 0.35; 'belong': 0.36; 'url:listinfo': 0.36; 'possible': 0.36; 'url:org': 0.36; 'level': 0.37; 'message- id:@gmail.com': 0.38; 'nov': 0.38; 'saves': 0.38; 'pm,': 0.38; 'users': 0.40; 'url:mail': 0.40; 'how': 0.40; 'deleting': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'simple': 0.61; 'making': 0.63; 'header:Message-Id:1': 0.63; 'effectively': 0.66; '2014,': 0.84; 'batchelder': 0.84; 'deleted?': 0.84; 'dict.': 0.84; 'it\xe2\x80\x99s': 0.84; 'don\xe2\x80\x99t': 0.91; '8bit%:18': 0.93
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=tmKx+iMYEHFFUE6/ojn2cj709SxfYg1HWfVYSM3Qu98=; b=Rcm2rMFTe5UPcKTeH9thljTwn9M6WjdmWWjWHgPRk2VnGWEZ/C0QJGDHnnZUlFHRU+ DjwBc4VtRvq2m41Ap3IG1uQwm0hj9Ht8bxk01D0QPOkU6Lilrb1OK4VqTPImHNyICGCb 4zR0k236HRgRwJgvgG3hdha3LyoOfIGC7ViGGNzaBZkeivHWZTdmxeTaCPg2xr6HMI0+ Qcf8FH4fYzv4ItIAV8Wi9LWHZH7xmF6eVDUu2K98HJypmAyb6H2AQjiWku1+RCtUvumq KRRwVW6xLW6Rm1xoWaUA9X+P0FGLPw7Zg7L28BuhozX4PICq8dZhdk1jreKWqYK77D96 r/4A==
X-Received by 10.68.93.132 with SMTP id cu4mr30251635pbb.36.1416813924203; Sun, 23 Nov 2014 23:25:24 -0800 (PST)
Content-Type multipart/alternative; boundary="Apple-Mail=_45115D34-7222-46D4-BBFD-3440382708D2"
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 <m4u6o4$76f$1@ger.gmane.org>
Date Sun, 23 Nov 2014 22:25:21 -0900
References <C093BC09-3AB7-49DB-B160-265A34C6BCDA@gmail.com> <m4u6o4$76f$1@ger.gmane.org>
To Ned Batchelder <ned@nedbatchelder.com>
X-Mailer Apple Mail (2.1993)
X-Mailman-Approved-At Mon, 24 Nov 2014 09:16:01 +0100
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.16236.1416816962.18130.python-list@python.org> (permalink)
Lines 122
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416816962 news.xs4all.nl 2873 [2001:888:2000:d::a6]:57333
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81441

Show key headers only | View raw


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

Basically, I have an app and am making it possible for the users to automate it by writing python scripts that run against the app's API. It includes PyQt5 widgets, a QScintilla editor, and a simple API for I/O. Apple’s app store is the plan.

Right now I create a new module with types.ModuleType(‘something’), then exec the code in the module’s dict. Then when the user saves the script again in my editor, it deletes all top-level references in the module’s dict that don’t belong to the core list: ['__spec__', '__name__', '__loader__', '__package__', '__doc__', '__builtins__’] before running exec again. 

So far this seems to effectively “delete” the old module and re-compile it with the updated source. For example I can create Qt Widgets on the module level and they are safely deleted when the script is saved.


> On Nov 23, 2014, at 5:56 PM, Ned Batchelder <ned@nedbatchelder.com> wrote:
> 
> On 11/23/14 1:49 AM, 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’t seem to do the job, and it’s not in sys.modules, so where is the dangling reference?
>> 
>> Thanks!
>> 
> 
> This sounds tricky, and possible very difficult to do properly.  Do you mind if I ask what the larger problem is?  Python might not be very good at having modules come and go as you want.
> 
> -- 
> Ned Batchelder, http://nedbatchelder.com
> 
> -- 
> 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 22:25 -0900

csiph-web