Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
reference counting and file objects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Reese  
View profile  
 More options May 17 2005, 2:50 pm
Newsgroups: comp.lang.python
From: John Reese <j...@ofb.net>
Date: Tue, 17 May 2005 18:50:19 +0000 (UTC)
Local: Tues, May 17 2005 2:50 pm
Subject: reference counting and file objects
def uselessHash(filename):
  fp= open(filename)
  hash= 0
  for line in fp:
    hash ^= hash(line.strip())
  fp.close() # do I need this or is fp closed by ref count?
  return hash

Consider the function above.  Do I need the fp.close(), or will the
file be closed automatically when fp goes out of scope and its
reference count drops to zero?

If so, there's really no point in calling .close() in situations like
this where the function completes in a relatively short time,, and if
not, I should probably be using try-finally.  That's my thinking... or
the thinking of the coworker who was arguing with me.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options May 17 2005, 2:58 pm
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 17 May 2005 11:58:24 -0700
Local: Tues, May 17 2005 2:58 pm
Subject: Re: reference counting and file objects

John Reese <j...@ofb.net> writes:
> Consider the function above.  Do I need the fp.close(), or will the
> file be closed automatically when fp goes out of scope and its
> reference count drops to zero?

In CPython, fp gets closed when it leaves scope.  In other
implementations you may need try-finally.  There's been occasional
discussion of possible new Python control blocks to make this easier.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options May 17 2005, 3:07 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Tue, 17 May 2005 21:07:13 +0200
Local: Tues, May 17 2005 3:07 pm
Subject: Re: reference counting and file objects

Paul Rubin wrote:
>>Consider the function above.  Do I need the fp.close(), or will the
>>file be closed automatically when fp goes out of scope and its
>>reference count drops to zero?

> In CPython, fp gets closed when it leaves scope.

One issue is that when the function terminates through an exception,
the file stays open as long as the traceback is available, which
exists until the next exception is raised (and thus replaces this
traceback).

If the function terminates normally (i.e. through return), it is
as you say.

Regards,
Martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter  
View profile  
 More options May 24 2005, 12:50 pm
Newsgroups: comp.lang.python
From: "Peter" <pe...@monicol.co.uk>
Date: 24 May 2005 09:50:38 -0700
Local: Tues, May 24 2005 12:50 pm
Subject: Re: reference counting and file objects

Does the idiom:

lines = file("myfile","r").readlines()

have any better guarantee of being closed automatically?

Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options May 24 2005, 3:26 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Tue, 24 May 2005 21:26:02 +0200
Local: Tues, May 24 2005 3:26 pm
Subject: Re: reference counting and file objects

Peter wrote:
> Does the idiom:

> lines = file("myfile","r").readlines()

> have any better guarantee of being closed automatically?

Yes. The file object only lives on the evaluation stack,
and that is discarded in any case when the function terminates
(whether through a return or through an exception). In
addition, the object is released as soon as readlines
returns.

Regards,
Martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options May 24 2005, 3:44 pm
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 24 May 2005 12:44:05 -0700
Local: Tues, May 24 2005 3:44 pm
Subject: Re: reference counting and file objects
"Martin v. Löwis" <mar...@v.loewis.de> writes:

> > lines = file("myfile","r").readlines()

> > have any better guarantee of being closed automatically?

> Yes. The file object only lives on the evaluation stack,
> and that is discarded in any case when the function terminates
> (whether through a return or through an exception). In
> addition, the object is released as soon as readlines
> returns.

It's released even if the exception is raised inside readlines?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options May 24 2005, 6:07 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Wed, 25 May 2005 00:07:56 +0200
Local: Tues, May 24 2005 6:07 pm
Subject: Re: reference counting and file objects

Paul Rubin wrote:
>>>lines = file("myfile","r").readlines()

> It's released even if the exception is raised inside readlines?

I think so, but only because readlines is a builtin function.
If it wasn't, there would be a stack frame for readlines, which
would have "self" as a local variable.

As readlines is builtin, it will not make it into the traceback.
So the reference to the file would be only on the evaluation
stack, at first, but that then gets copied into the argument
tuple. The argument tuple, in turn, is released in the process
of unwinding (again, it wouldn't if readlines wasn't builtin).

I might be wrong, but I think the following trace shows that
this is what happens:

>>> class F:

...   def __del__(self):print "released"
...
>>> len(F())

released
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: F instance has no attribute '__len__'

Compare this to a Python function:

>>> def len2(o): raise Exception
...
>>> len2(F())

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in len2
Exception
>>> raise ""

released
Traceback (most recent call last):
  File "<stdin>", line 1, in ?

However, this analysis also shows that it is quite delicate
to rely on this pattern.

Regards,
Martin


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google