Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'skip:p 40': 0.04; 'value,': 0.04; 'exception,': 0.07; 'imports': 0.07; 'type,': 0.07; 'executed': 0.09; 'restored': 0.09; 'am,': 0.12; 'subject:file': 0.12; 'def': 0.13; '10:59': 0.16; 'copyfile': 0.16; 'file")': 0.16; 'quitting': 0.16; 'reload': 0.16; 'shutil': 0.16; 'wrote:': 0.18; 'trying': 0.21; 'wrote': 0.22; 'header:In- Reply-To:1': 0.22; "skip:' 40": 0.23; 'string': 0.24; 'subject:change': 0.24; 'module': 0.26; 'work,': 0.26; "i'm": 0.26; 'import': 0.27; 'temporary': 0.29; 'problem': 0.29; 'class': 0.29; 'example': 0.29; 'correct': 0.29; 'idea': 0.32; "can't": 0.32; 'message-id:@gmail.com': 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'probably': 0.34; 'certain': 0.34; 'file.': 0.34; 'something': 0.35; 'file': 0.36; 'subject:with': 0.36; 'but': 0.37; 'with.': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'skip:_ 10': 0.37; 'received:10.0.0': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'option': 0.39; 'ways': 0.39; 'why': 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'achieve': 0.61; 'skip:o 30': 0.63; 'skip:w 30': 0.63; 'restore': 0.68; 'interrupted': 0.73; 'andrea': 0.84; 'easy_install': 0.84; 'something.': 0.84; 'stupid': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=G9TIzHmerTNlsG+Np8sy1GQG+AmdZveeYneY/nmyFkM=; b=QOiPAePd/KpzuVWawnhrpv6OY9CbY3NI3zRD7oubqoj7g7vWcNqhdFG/syKy3GGNUI Wj0FXH7eq4/IH34eghK+qKPfg6Imlbr00WdhzjnRG3OMedZOoeibbh06PBCMxU1ol5Id 0+cTXbUNf/I7m/tEjwF4KI8F5GwmLLawGckPE= Date: Tue, 13 Dec 2011 14:21:33 +0000 From: Andrea Crotti User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111108 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: change a file with a context manager References: <4EE72FF4.8010203@gmail.com> In-Reply-To: <4EE72FF4.8010203@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323786099 news.xs4all.nl 6846 [2001:888:2000:d::a6]:45550 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17137 On 12/13/2011 10:59 AM, Andrea Crotti wrote: > So I have the following problem, I need something to copy a file to a > certain position > and restore it after. > So I wrote this simple context manager: > > class WithCorrectEasyInstall(object): > > def __enter__(self): > import pkg_resources > from shutil import copyfile > easy_install_file = > pkg_resources.resource_filename('psi.devsonly', > 'windows_easy_install.pth') > self.global_easy_install = > 'c:/python25/Lib/site-packages/easy-install.pth' > # can use mmap, a temporary file or just write to string > self.stored = open(self.global_easy_install).read() > logger.debug("copying the correct easy_install.pth file to the > global position") > copyfile(easy_install_file, self.global_easy_install) > > def __exit__(self, type, value, traceback): > #TODO: make sure it's restored also when quitting in some > other ways > # restore the old file > open(self.global_easy_install, 'w').write(self.stored) > logger.debug("restoring the old easy_install.pth file") > > > The problem is that this is not executed for example when the program > is interrupted > with for example a KeyboardInterrupt. > But that's also an exception, why is it not caught by the context > manager? > > And if there are better way to do what I'm trying to achieve they are > welcome :) Alright it was probably a stupid idea to start with. The problem is that if the easy_install is wrong even the imports in the module will not work, so I can't even start to do something. Another possible option is to just add to the path everything contained in that correct easy_install.pth file. Or is there a way to reload the path settings?