Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   RE: does python have useless destructors? (http://www.velocityreviews.com/forums/t331921-re-does-python-have-useless-destructors.html)

Delaney, Timothy C (Timothy) 06-10-2004 03:59 AM

RE: does python have useless destructors?
 
Michael Geary wrote:

>>> try:
>>> myfile = open("myfilepath", "w")
>>> myfile.write(reallybigbuffer)
>>> finally:
>>> myfile.close()

>>
>> ... "feels" just right. This is how you do it when you
>> want to be sure the file is closed, regardless of
>> other considerations like garbage collection, etc.
>>
>> It is simple, clean, and most of all, very explicit.

>
> And it has a bug. :-)
>
> What if "myfilepath" is not writable? The open() call raises an
> exception, which jumps to the finally: block, where myfile.close()
> fails because myfile is undefined.
>
> What would be the cleanest way to code this to handle that error
> situation?


myfile = open("myfilepath", "w")

try:
myfile.write(reallybigbuffer)
finally:
myfile.close()

Tim Delaney


Tim Bradshaw 06-11-2004 03:44 PM

Re: does python have useless destructors?
 
"Delaney, Timothy C (Timothy)" <tdelaney@avaya.com> wrote in message news:<mailman.780.1086840001.6949.python-list@python.org>...

>
> myfile = open("myfilepath", "w")
>
> try:
> myfile.write(reallybigbuffer)
> finally:
> myfile.close()
>


I don't think this is save. Is it certain that there can be no
problem between the open and the try? The expansion of Lisp macros
which do this typically look like:

myfile = None

try:
myfile = open(...)
...
finally:
if myfile
myfile.close()

Marcin 'Qrczak' Kowalczyk 06-11-2004 03:52 PM

Re: does python have useless destructors?
 
On Fri, 11 Jun 2004 08:44:39 -0700, Tim Bradshaw wrote:

>> myfile = open("myfilepath", "w")
>>
>> try:
>> myfile.write(reallybigbuffer)
>> finally:
>> myfile.close()

>
> I don't think this is save. Is it certain that there can be no
> problem between the open and the try? The expansion of Lisp macros
> which do this typically look like:
>
> myfile = None
>
> try:
> myfile = open(...)


What is the difference? If there are no asynchronous exceptions, both
are safe (nothing can happen in-between). And if there are asynchronous
exceptions, both are unsafe (the exception might come after open returned
but before its result is stored).

Glasgow Haskell has functions which temporarily block asynchronous
exceptions for cases like this.

--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.pl/~qrczak/


Peter Hansen 06-11-2004 07:27 PM

Re: does python have useless destructors?
 
Tim Bradshaw wrote:

> "Delaney, Timothy C (Timothy)" <tdelaney@avaya.com> wrote in message news:<mailman.780.1086840001.6949.python-list@python.org>...
>
>
>>myfile = open("myfilepath", "w")
>>
>>try:
>> myfile.write(reallybigbuffer)
>>finally:
>> myfile.close()

>
> I don't think this is save. Is it certain that there can be no
> problem between the open and the try?


Yes, it's certain to be safe (barring multithreaded stuff
rebinding 'myfile' in between, which is irrelevant to the
discussion). Are you perhaps concerned that the open itself
might fail? If so, it needs its own try/except block, as I
noted elsewhere. The finally is only need *if* the open
succeeds, but unless you insert code between open() and
'try', it's safe.

-Peter

Tim Bradshaw 06-14-2004 06:38 PM

Re: does python have useless destructors?
 
Peter Hansen <peter@engcorp.com> wrote in message news:<u72dnUZjC7EJmlfdRVn-hQ@powergate.ca>...

> Yes, it's certain to be safe (barring multithreaded stuff
> rebinding 'myfile' in between, which is irrelevant to the
> discussion). Are you perhaps concerned that the open itself
> might fail? If so, it needs its own try/except block, as I
> noted elsewhere. The finally is only need *if* the open
> succeeds, but unless you insert code between open() and
> 'try', it's safe.


Yes, I think it is safe: I was thinking from the point of view of a
Lisp macro implementor, where you have to deal with a lot of more
general issues (for instance multiple bindings, which essentially
insert code between the open() and the try, and user reassignments,
which mean that the variable binding vanishes, which you need to cope
with as well. Getting a *general* version of this correct (which is
what I understood the original poster to be asking about) is hard to
do, but that's not a problem for a language without macros.

--tim


All times are GMT. The time now is 01:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.