[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: svn commit: r1156375 - /subversion/branches/fs-py/subversion/python/svn/fs.py

From: Hyrum K Wright <hyrum.wright_at_wandisco.com>
Date: Wed, 10 Aug 2011 16:44:36 -0500

On Wed, Aug 10, 2011 at 4:35 PM, Greg Stein <gstein_at_gmail.com> wrote:
> On Wed, Aug 10, 2011 at 17:26,  <hwright_at_apache.org> wrote:
>> Author: hwright
>> Date: Wed Aug 10 21:26:36 2011
>> New Revision: 1156375
>>
>> URL: http://svn.apache.org/viewvc?rev=1156375&view=rev
>> Log:
>> On the fs-py branch:
>> Followup to r1156347: use a bit cleaner syntax to automagically close files.
>>
>> * subversion/python/svn/fs.py
>>  (FS.set_uuid, FS._open_fs): Use the with statement.
>
> And now where did your .close() call go? You're still relying on 'f'
> going out of scope to close. IOW, right back to the zero refcount
> algorithm. And in which case, the original construction is much
> cleaner than all this "with" gunk.

In the case of an open()'d file, the file is closed when the object is
eventually garbage collected, which, by documentation[1] isn't
guaranteed to happen when the object is unreachable: "Do not depend on
immediate finalization of objects when they become unreachable (ex:
always close files)." Hence the need for an explicit close.

However, the "with" statement[2] used in this revision *does*
guarantee that the file will be closed immediately upon completion of
the block, even in the case that an exception is raised in the block.

As for "gunk," the with construction was introduced in Python 2.5,
almost 5 years ago and I find it cleaner.

-Hyrum

[1] http://docs.python.org/reference/datamodel.html#objects-values-and-types
[2] http://docs.python.org/reference/datamodel.html#context-managers

-- 
uberSVN: Apache Subversion Made Easy
http://www.uberSVN.com/
Received on 2011-08-10 23:45:04 CEST

This is an archived mail posted to the Subversion Dev mailing list.