Re: reference counting and file objects

2005-05-24 Thread "Martin v. Löwis"
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

Re: reference counting and file objects

2005-05-24 Thread Paul Rubin
"Martin v. Löwis" <[EMAIL PROTECTED]> 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 throu

Re: reference counting and file objects

2005-05-24 Thread "Martin v. Löwis"
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

Re: reference counting and file objects

2005-05-24 Thread Peter
Martin v. Löwis wrote: > 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 wh

Re: reference counting and file objects

2005-05-17 Thread "Martin v. Löwis"
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

Re: reference counting and file objects

2005-05-17 Thread Paul Rubin
John Reese <[EMAIL PROTECTED]> 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

reference counting and file objects

2005-05-17 Thread John Reese
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