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


Groups > comp.lang.python > #94909

Re: __main__ vs official module name: distinct module instances

Path csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'it;': 0.09; 'subject:module': 0.09; 'whichever': 0.09; 'interpreter': 0.15; '>on': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'module?': 0.16; 'runtimeerror': 0.16; 'send.': 0.16; 'simpson': 0.16; 'sys.modules': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'programmer': 0.18; '>>>': 0.20; '2015': 0.20; 'aug': 0.20; 'to:name:python-list@python.org': 0.20; 'cheers,': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'found.': 0.27; 'actual': 0.28; 'for,': 0.29; 'raise': 0.29; 'says': 0.32; 'problem': 0.33; "d'aprano": 0.33; 'raising': 0.33; 'steven': 0.33; 'file': 0.34; 'that,': 0.34; 'requiring': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'possible': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; 'mean': 0.38; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'above,': 0.63; 'cameron': 0.66; 'situation': 0.67; 'wish': 0.71; 'received:61': 0.72; "'you": 0.84; 'badly,': 0.84; 'detecting': 0.84; 'increasingly': 0.84; 'premature': 0.84; 'subject:official': 0.84
X-Authentication-Info Submitted using ID cskk@bigpond.com
X-Authority-Analysis v=2.0 cv=D6DF24tj c=1 sm=1 a=+4waRsk6nlVq0WGfWeltyg==:17 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=yEdEr6MRgwAA:10 a=uRRa74qj2VoA:10 a=pGLkceISAAAA:8 a=kZ7UWmmPAAAA:8 a=VqVPVWedFWEsIqF_wAkA:9 a=CjuIK1q_8ugA:10 a=+4waRsk6nlVq0WGfWeltyg==:117
Date Mon, 3 Aug 2015 10:59:41 +1000
From Cameron Simpson <cs@zip.com.au>
To "python-list@python.org" <python-list@python.org>
Subject Re: __main__ vs official module name: distinct module instances
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <CAPTjJmr8LXqF8kN_TP35NhvTz4CyuvPjyHZsXQ-DosZvdXVMDg@mail.gmail.com>
User-Agent Mutt/1.5.23 (2014-03-12)
References <CAPTjJmr8LXqF8kN_TP35NhvTz4CyuvPjyHZsXQ-DosZvdXVMDg@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.1164.1438563595.3674.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438563595 news.xs4all.nl 2906 [2001:888:2000:d::a6]:55823
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:94909

Show key headers only | View raw


On 02Aug2015 18:18, Chris Angelico <rosuav@gmail.com> wrote:
>On Sun, Aug 2, 2015 at 6:16 PM, Chris Angelico <rosuav@gmail.com> wrote:
>> On Sun, Aug 2, 2015 at 5:41 PM, Steven D'Aprano <steve@pearwood.info> wrote:
>>> * if you don't wish to do that, you're screwed, and I think that the
>>>   best you can do is program defensively by detecting the problem
>>>   after the event and bailing out:
>>>
>>>   # untested
>>>   import __main__
>>>   import myactualfilename
>>>   if os.path.samefile(__main__.__path__, myactualfilename.__path__):
>>>       raise RuntimeError
>>
>> Not sure what __path__ is here, as most of the things in my
>> sys.modules don't have it; do you mean __file__? In theory, it should
>> be possible to skim across sys.modules, looking for a match against
>> __main__, and raising RuntimeError if any is found.
>
>Oops, premature send.
>
>*In theory* it should be possible to do the above, but whichever
>attribute you look for, some modules may not have it. How does this
>play with, for instance, zipimport, where there's no actual file name
>for the module?

To my eyes, badly, which IMO strengthens my case for addressing the situation 
in the interpreter instead of requiring increasingly complex gyrations on the 
part of every programmer on the planet:-)

Cheers,
Cameron Simpson <cs@zip.com.au>

The CBR and ZXR should come with a warning sticker that says 'You are not
Mick Doohan, do NOT be a prat'  - UK's _BIKE_ magazine

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: __main__ vs official module name: distinct module instances Cameron Simpson <cs@zip.com.au> - 2015-08-03 10:59 +1000

csiph-web