Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'it;': 0.09; 'subject:module': 0.09; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'runtimeerror': 0.16; 'sys.modules': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'message- id:@mail.gmail.com': 0.27; 'found.': 0.27; 'raise': 0.29; 'problem': 0.33; "d'aprano": 0.33; 'raising': 0.33; 'steven': 0.33; 'that,': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'possible': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'things': 0.38; 'mean': 0.38; 'sure': 0.39; 'wish': 0.71; 'detecting': 0.84; 'subject:official': 0.84 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 :cc:content-type; bh=cmmU4llR0Gt2AxO0FCW9IWLeKSTVJmqcCfXOkTnEnrY=; b=n3XlezMiXRqqA5+WJ7OLPdfKxBBP3uQPn3HGqgGnXfoM5WSfYiI63oQKTkgIdvyL35 7m/bPqDZjfjZrreof47EoVDFtiiBIFA6nyMuKmJwXVnkLcrtifZVZStHgEz6iqct0rZf sleFNFeweMLlGtXEdGxyJ44k51x0hbU17GQ5f/ni6XumkC/Bl2iL1n5HNwLi2AAsHw4R t+cniasYapiP0acpVOF9oVbHt4d0LzeaByP6f6A+9YYMIt56UDWWh/U9vCmNPng5MVem HPHnnmgeJhtxNEbQiQUbZrhIOEOzVhAvDlugwLEd4Pq18snvQC46qKBlC5SBsjePHqg0 Zbkg== MIME-Version: 1.0 X-Received: by 10.107.12.141 with SMTP id 13mr14083284iom.157.1438503390217; Sun, 02 Aug 2015 01:16:30 -0700 (PDT) In-Reply-To: <55bdc996$0$1663$c3e8da3$5496439d@news.astraweb.com> References: <55bdc996$0$1663$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 2 Aug 2015 18:16:30 +1000 Subject: Re: __main__ vs official module name: distinct module instances From: Chris Angelico To: "Steven D'Aprano" Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 15 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1438503398 news.xs4all.nl 2918 [2001:888:2000:d::a6]:50743 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94891 On Sun, Aug 2, 2015 at 5:41 PM, Steven D'Aprano 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.