Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!hq-usenetpeers.eweka.nl!81.171.88.15.MISMATCH!eweka.nl!lightspeed.eweka.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'argument': 0.04; 'subject:Python': 0.05; 'attribute': 0.05; 'python': 0.09; '"if': 0.09; "ain't": 0.09; 'club.': 0.09; 'does,': 0.09; 'happen.': 0.09; 'information?': 0.09; 'none.': 0.09; 'path.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'subject:Why': 0.09; 'stored': 0.10; 'library': 0.15; 'producing': 0.15; 'bug,': 0.16; 'fixing,': 0.16; 'imo.': 0.16; 'one)': 0.16; 'programmer,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reliably': 0.16; 'roy': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'directory.': 0.17; '>>>': 0.18; '(or': 0.18; 'import': 0.21; 'programming': 0.23; "i've": 0.23; 'idea': 0.24; 'second': 0.24; 'script': 0.24; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'developers': 0.26; 'supported': 0.26; '(most': 0.27; 'core': 0.27; 'see,': 0.27; 'header:X-Complaints-To:1': 0.28; 'initial': 0.28; 'run': 0.28; 'post': 0.28; 'record': 0.28; 'inspect': 0.29; 'writes:': 0.29; 'case,': 0.29; 'definition': 0.29; '"the': 0.29; "i'm": 0.29; 'relative': 0.30; 'on,': 0.30; 'expect': 0.31; 'code': 0.31; 'gets': 0.32; 'file': 0.32; 'from:addr:yahoo.co.uk': 0.32; "skip:' 20": 0.32; 'could': 0.32; 'print': 0.32; '11,': 0.33; 'point,': 0.33; 'right?': 0.33; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'or,': 0.34; 'wrong': 0.34; 'described': 0.35; 'path': 0.35; 'stable': 0.35; 'doing': 0.35; 'subject:?': 0.35; 'something': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; '(i.e.': 0.36; 'programmers': 0.36; 'should': 0.36; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'fact': 0.38; 'object': 0.38; 'some': 0.38; 'nothing': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'application': 0.40; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'different': 0.63; 'ever': 0.63; 'more': 0.63; 'behavior': 0.64; 'making': 0.64; 'following.': 0.65; 'learned': 0.65; 'smith': 0.71; 'gotten': 0.75; 'article': 0.78; 'is)': 0.84; 'pardon': 0.84; 'picture.': 0.84; 'start-up,': 0.84; 'officially': 0.91; 'subject:remember': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Why doesn't Python remember the initial directory? Date: Mon, 20 Aug 2012 08:10:14 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-26-58.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: X-Antivirus: avast! (VPS 120819-1, 19/08/2012), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345446515 news.xs4all.nl 6897 [2001:888:2000:d::a6]:40604 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27467 On 20/08/2012 02:57, kj wrote: > In Roy Smith writes: > >> In article , kj >> wrote: > >>> As far as I've been able to determine, Python does not remember >>> (immutably, that is) the working directory at the program's start-up, >>> or, if it does, it does not officially expose this information. > >> Why would you expect that it would? What would it (or you) do with this >> information? > >> More to the point, doing a chdir() is not something any library code >> would do (at least not that I'm aware of), so if the directory changed, >> it's because some application code did it. In which case, you could >> have just stored the working directory yourself. > > This means that no library code can ever count on, for example, > being able to reliably find the path to the file that contains the > definition of __main__. That's a weakness, IMO. One manifestation > of this weakness is that os.chdir breaks inspect.getmodule, at > least on Unix. If you have some Unix system handy, you can try > the following. First change the argument to os.chdir below to some > valid directory other than your working directory. Then, run the > script, making sure that you refer to it using a relative path. > When I do this on my system (OS X + Python 2.7.3), the script bombs > at the last print statement, because the second call to inspect.getmodule > (though not the first one) returns None. > > import inspect > import os > > frame = inspect.currentframe() > > print inspect.getmodule(frame).__name__ > > os.chdir('/some/other/directory') # where '/some/other/directory' is > # different from the initial directory > > print inspect.getmodule(frame).__name__ > > ............... > > % python demo.py > python demo.py > __main__ > Traceback (most recent call last): > File "demo.py", line 11, in > print inspect.getmodule(frame).__name__ > AttributeError: 'NoneType' object has no attribute '__name__' > > > > I don't know of any way to fix inspect.getmodule that does not > involve, directly or indirectly, keeping a stable record of the > starting directory. > > But, who am I kidding? What needs fixing, right? That's not a > bug, that's a feature! Etc. > > By now I have learned to expect that 99.99% of Python programmers > will find that there's nothing wrong with behavior like the one > described above, that it is in fact exactly As It Should Be, because, > you see, since Python is the epitome of perfection, it follows > inexorably that any flaw or shortcoming one may *perceive* in Python > is only an *illusion*: the flaw or shortcoming is really in the > benighted programmer, for having stupid ideas about programming > (i.e. any idea that may entail that Python is not *gasp* perfect). > Pardon my cynicism, but the general vibe from the replies I've > gotten to my post (i.e. "if Python ain't got it, it means you don't > need it") is entirely in line with these expectations. > I see, I see, I get the picture. You're in the "The OS is flawed so I expect the Python core developers to do my work for me by producing code that gets around every known flaw in every supported OS" club. Somehow I don't think that will happen. -- Cheers. Mark Lawrence.