launchpad-dev team mailing list archive
-
launchpad-dev team
-
Mailing list archive
-
Message #02548
Re: The with statement
On Feb 11, 2010, at 07:38 AM, Robert Collins wrote:
>On Wed, 2010-02-10 at 14:42 -0500, Barry Warsaw wrote:
>>
>> with cwd('/path/to/stuff'):
>> # ...
>>
>> is pretty obvious.
>
>And extremely dangerous, as well as trivially broken. Not safe to use
>from any server code.
Any more so than its try/finally cousin?
Case in point: DSCFile.unpackAndCheckSource() which chdirs to a temp
directory. The try/finally is actually worse here than an equivalent
with-statement because there's a window of opportunity for the two lines
between the chdir() and the try to fail, leaving you in the tempdir because
the finally never gets called.
Do a grep over our code for os.chdir() for some enfrightenment. :) A lot of
uses are in tests, and at the very least promoting a common pattern through a
convenient context manager can bring some consistency to our code.
-Barry
Attachment:
signature.asc
Description: PGP signature
References