473,555 Members | 2,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference counting and file objects

def uselessHash(fil ename):
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.
Jul 19 '05 #1
6 1722
John Reese <jt*@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.
Jul 19 '05 #2
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
Jul 19 '05 #3

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 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


Does the idiom:

lines = file("myfile"," r").readline s()

have any better guarantee of being closed automatically?

Peter

Jul 19 '05 #4
Peter wrote:
Does the idiom:

lines = file("myfile"," r").readline s()

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
Jul 19 '05 #5
"Martin v. Löwis" <ma****@v.loewi s.de> writes:
lines = file("myfile"," r").readline s()

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?
Jul 19 '05 #6
Paul Rubin wrote:
lines = file("myfile"," r").readline s()
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):p rint "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
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2168
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does not use garbage collection (as Java does). So if the script runs a loop: for i in range(100): f = Obj(i)
1
2696
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last client deletes a refernce to it
1
3234
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
1
1868
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage collector, such as the one used in Java, maintains a record of whether or not an object is currentlys being used. An unused object is tagged as garbage,
7
2431
by: Fred Hedges | last post by:
I'm wondering if a tool exists that will integrate with the VS 2005 debugger, such that when I set a breakpoint I can inspect a reference and see how many other references exist to that object. I'm sure there is an internal reference count somewhere but have no idea how to inspect it.
3
2236
by: Ole Nielsby | last post by:
I need to implement reference counting in a class hierarch, in a thread safe manner. (The classes are umanaged but I might want to compile them with the /clr option.) Some of the objects - atoms - are registered in a global hash table - let's call it an atom table. When an atom refernce count reaches zero, the atom is removed from the...
1
2267
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
1713
by: mdoxdo | last post by:
Hi all, I was wondering if there is a way to get the number of references an objects have (ie, other objects reference that object)? I know that .Net GC uses a different mechanism than the RefCount in C++ since .Net uses managed objects. I just need to know if there is any way possible to get this number. Any comments would be...
5
5487
by: colin | last post by:
Hi, I need to cache various resource objects such as images for example, wich might be referenced from multiple objects (wich could be considered documents for ease of discusion) and the idea is simply to make sure they arnt duplicated as they take up considerable memory, so I can look up to see if an existing copy exists. however once...
275
12106
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7824
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8060
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7903
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5452
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.