Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:module': 0.09; '24,': 0.16; '__del__': 0.16; 'builtins.': 0.16; 'dict': 0.16; 'function?': 0.16; 'garbage': 0.16; 'globals': 0.16; "module's": 0.16; 'subject:created': 0.16; 'substitute': 0.16; 'wrote:': 0.18; 'module': 0.19; 'memory': 0.22; 'email addr:gmail.com>': 0.22; 'print': 0.22; '>': 0.26; 'references': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'testing': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'subject:skip:i 10': 0.31; 'class': 0.32; 'this.': 0.32; 'becomes': 0.33; 'call.': 0.33; 'could': 0.34; 'subject:with': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'being': 0.38; 'skip:& 10': 0.38; 'nov': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'expensive': 0.39; 'itself': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'strictly': 0.61; 'production': 0.68; 'beside': 0.84; 'careful': 0.91; 'comfort': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=FR6ElVsOtyaTDEzZPI0hC+TsUNMUAbD/EMYCUO3j0LI=; b=0METAOBKkuESPG0vVdMcBYIc0Hhh9Z1YlSWhdZVmbzVq/DN5D6zgoglDEVQMCkmXEk ZDd4gd+ne1qlm+LK4Dm2cU/PkOKV1E34MJzyoQL7ieB3ooNFHQ3ohK/Mp6TreVrEQvXh sFDyul4omFaakZCIK/oroEfeisbV+BJXRrd8BA77Ja9e/y8m7r+oiAGtZjvvSLkg9gyU i2QtL6cyxQhVAdWfa3MRCukALGLpeUvHEJuYRlZ7vPVkK9inILei96qA3XzTcIr9ZaqF elaVVJSSadosFQDa74e1763h4+vWrrwSQtEswH1dxBYUwwljiZRaKuB8eo6cjTwjh2yc 4JJA== MIME-Version: 1.0 X-Received: by 10.66.232.70 with SMTP id tm6mr34646795pac.120.1416841954209; Mon, 24 Nov 2014 07:12:34 -0800 (PST) In-Reply-To: <6E80C5B3-4203-4CA5-9796-175DB9567862@gmail.com> References: <6E80C5B3-4203-4CA5-9796-175DB9567862@gmail.com> Date: Mon, 24 Nov 2014 08:12:34 -0700 Subject: Re: unloading a module created with imp.new_module From: Ian Kelly To: Python Content-Type: multipart/alternative; boundary=047d7b111e0962371205089c39de 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416841957 news.xs4all.nl 2837 [2001:888:2000:d::a6]:38348 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5934 X-Received-Body-CRC: 827525031 Xref: csiph.com comp.lang.python:81457 --047d7b111e0962371205089c39de Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Nov 24, 2014 1:27 AM, "Patrick Stinson" wrote: > > How does the __del__ method have a reference to the module=E2=80=99s glob= als dict? because it references the print function? The module's dict becomes the __globals__ dict used by the function for looking up globals and builtins. > Crazy. Is there any other way to comfort when a module is being deleted beside defining a class object with a printing dtor? Modules don't strictly have to be ModuleType. You could substitute a class with a __del__ method for the module itself for testing this. Be careful with leaving this in your production code though, as the presence of __del__ methods can interfere with garbage collection and are not advisable prior to 3.4. You can also test whether an object is still in memory by looking for it in gc.get_objects() although be warned that is an expensive call. --047d7b111e0962371205089c39de Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


On Nov 24, 2014 1:27 AM, "Patrick Stinson" <patrickkidd@gmail.com> wrote:
>
> How does the __del__ method have a reference to the module=E2=80=99s g= lobals dict? because it references the print function?

The module's dict becomes the __globals__ dict used by t= he function for looking up globals and builtins.

> Crazy. Is there any other way to comfort when a module = is being deleted beside defining a class object with a printing dtor?

Modules don't strictly have to be ModuleType. You could = substitute a class with a __del__ method for the module itself for testing = this. Be careful with leaving this in your production code though, as the p= resence of __del__ methods can interfere with garbage collection and are no= t advisable prior to 3.4.

You can also test whether an object is still in memory by lo= oking for it in gc.get_objects() although be warned that is an expensive ca= ll.

--047d7b111e0962371205089c39de--