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

Date 2015-08-03 10:59 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: __main__ vs official module name: distinct module instances
References <CAPTjJmr8LXqF8kN_TP35NhvTz4CyuvPjyHZsXQ-DosZvdXVMDg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1164.1438563595.3674.python-list@python.org> (permalink)

Show all headers | 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