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


Groups > comp.lang.python > #94893

Re: __main__ vs official module name: distinct module instances

References <mailman.1151.1438488073.3674.python-list@python.org> <55bdc996$0$1663$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmoLRDarzHGAoLW6bUFXZHjgGpjpjdZp=8S2hoTDSQBK9A@mail.gmail.com>
Date 2015-08-02 18:18 +1000
Subject Re: __main__ vs official module name: distinct module instances
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1154.1438503524.3674.python-list@python.org> (permalink)

Show all headers | View raw


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?

ChrisA

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


Thread

__main__ vs official module name: distinct module instances Cameron Simpson <cs@zip.com.au> - 2015-08-02 13:53 +1000
  Re: __main__ vs official module name: distinct module instances Steven D'Aprano <steve@pearwood.info> - 2015-08-02 17:41 +1000
    Re: __main__ vs official module name: distinct module instances Chris Angelico <rosuav@gmail.com> - 2015-08-02 18:16 +1000
    Re: __main__ vs official module name: distinct module instances Chris Angelico <rosuav@gmail.com> - 2015-08-02 18:18 +1000
    Re: __main__ vs official module name: distinct module instances Cameron Simpson <cs@zip.com.au> - 2015-08-03 10:57 +1000

csiph-web