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: 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 In-Reply-To: Date: Sun, 23 Nov 2014 22:25:21 -0900 References: To: Ned Batchelder 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --Apple-Mail=_45115D34-7222-46D4-BBFD-3440382708D2 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 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=E2=80=99s app store is the plan. Right now I create a new module with types.ModuleType(=E2=80=98something=E2= =80=99), then exec the code in the module=E2=80=99s dict. Then when the = user saves the script again in my editor, it deletes all top-level = references in the module=E2=80=99s dict that don=E2=80=99t belong to the = core list: ['__spec__', '__name__', '__loader__', '__package__', = '__doc__', '__builtins__=E2=80=99] before running exec again.=20 So far this seems to effectively =E2=80=9Cdelete=E2=80=9D 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 = wrote: >=20 > 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=E2=80=99t= seem to do the job, and it=E2=80=99s not in sys.modules, so where is = the dangling reference? >>=20 >> Thanks! >>=20 >=20 > 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. >=20 > --=20 > Ned Batchelder, http://nedbatchelder.com >=20 > --=20 > https://mail.python.org/mailman/listinfo/python-list --Apple-Mail=_45115D34-7222-46D4-BBFD-3440382708D2 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
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=E2=80=99s app store is the = plan.

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

So far this seems to effectively =E2=80=9Cdelet= e=E2=80=9D 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=E2=80= =99t seem to do the job, and it=E2=80=99s 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

= --Apple-Mail=_45115D34-7222-46D4-BBFD-3440382708D2--