472,353 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

python reference counting and exceptions

Hello,

I have a question refering python's reference counting/garbage
collection in combination with thrown exceptions.

I'm very new to this mailinglist, so I hope, that this question has not
been asked and answered before.

In c++, it is possible to write a locking system similar to the one below:
void myFunction()
{
# create a resource lock. Locks some resource
ResourceLock myLock;

# the following line may throw an exception
doStuff();
}
myLock is a stack variable which is destroyed as soon as the function is
left (either by returning or because of an uncatched exception). As
myLock is destroyed, the destructor will automatically unlock the resource.

The following python code has the same effect (at least on my system and
my python implementation).
class MyLock(object):
def __init__(self):
print "MyLock.__init__"

def __del__(self):
print "MyLock.__del__"

def myFunction():
print "prelock"
lock = MyLock()
print "postlock"

print "prefun"
try:
myFunction()
except Exception, e:
print str(e)
print "postfun"

output is:
prefun
prelock
MyLock.__init__
postlock
MyLock.__del__
postfun
MyLock.__init__
MyLock.__del__
However, when myFunction raises an exception, lock is not destroyed as
soon as the function is exited but only at the end of the script
My question is: is there some reliable way to mimic the c++ code snipped
above in python without adding a try-except-unlock-rethrow block around
every peace of code that locks some resources.
Greets,

Andreas Huesgen
Sep 12 '06 #1
0 890

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

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue,...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here:...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.