473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Why did someone write this?

try:
exc_type, exc_value, exc_traceback = sys.exc_info()
# Do something
finally:
exc_traceback = None

Why the try/finally with setting exc_traceback to None? The python docs
didn't give me any clue, and I'm wondering what this person knows that
I don't.

Thanks,
-Sandra

Apr 7 '06 #1
4 1056

Sandra> try:
Sandra> exc_type, exc_value, exc_traceback = sys.exc_info()
Sandra> # Do something
Sandra> finally:
Sandra> exc_traceback = None

Sandra> Why the try/finally with setting exc_traceback to None?

The intent is to decrement the reference count to any objects referenced by
exc_traceback. Without it, the frame(s) referenced by the traceback remain
alive until exc_traceback goes out of scope.

Skip

Apr 7 '06 #2
Sandra-24 wrote:
try:
exc_type, exc_value, exc_traceback = sys.exc_info()
# Do something
finally:
exc_traceback = None

Why the try/finally with setting exc_traceback to None? The python docs
didn't give me any clue, and I'm wondering what this person knows that
I don't.


You just have not found the right part of the doc:
http://docs.python.org/lib/module-sys.html#l2h-337

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
Apr 7 '06 #3
I can't believe I missed it in the documentation. Maybe it wasn't in
the offline version I was using, but more likely it was just one of
those things.

So the trouble seems to be that the traceback holds a reference to the
frame where the exception occurred, and as a result a local variable
that references the traceback in that frame now holds a reference to
it's own frame (preventing the frame from being recliamed) and can't be
cleaned up prior to python 2.2 with GC enabled.

Which means it's fine to hold a reference to the traceback in a frame
not in the traceback, or (I think) to create a temporary unamed
reference to it.

Thanks for your help guys!
-Sandra

Apr 7 '06 #4
In article <11**********************@j33g2000cwa.googlegroups .com>,
"Sandra-24" <sa***********@yahoo.com> wrote:
I can't believe I missed it in the documentation. Maybe it wasn't in
the offline version I was using, but more likely it was just one of
those things.

So the trouble seems to be that the traceback holds a reference to the
frame where the exception occurred, and as a result a local variable
that references the traceback in that frame now holds a reference to
it's own frame (preventing the frame from being recliamed) and can't be
cleaned up prior to python 2.2 with GC enabled.

Which means it's fine to hold a reference to the traceback in a frame
not in the traceback, or (I think) to create a temporary unamed
reference to it.


I don't think that's exactly it - the problem wasn't exactly
circularity, and GC wouldn't help. But as I try to write a
test program that demonstrates, it looks like current versions
of Python have done something with sys.exc_traceback that
avoids at least some of the problems.

import sys
class A:
def __del__(self):
print 'A.__del__'

def f():
a = A()
try:
xyz
except:
print 'caught expected error'
print 'returning from f'
return sys.exc_traceback

t = f()
print 'Done'
In order to get the traceback to preserve "a" past its
natural lifetime, I had to return it to the caller, because
today sys.exc_traceback disappears when f() returns. But
it shows the effect on order of execution: 'A.__del__'
will appear after 'Done', when it should appear before it.

When I did this (sys.exc_traceback = None), the applications
used a terminal graphics library, like curses, and I often
depended on finalization (__del__) to run the "close" rendering
for a graphic element. Worked fine until an exception, so I
add this precaution to every I/O flush.

Donn Cave, do**@u.washington.edu
Apr 8 '06 #5

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

Similar topics

6
by: Michael Lauzon | last post by:
A while back, I had a program that I was working on, while taking Java in a Web Design & Development course -- this was back in 1999 -- not having the mindset of a programmer I failed that part of...
42
by: Mike P. | last post by:
Hello I come from the world of C++ programming, and I'm used to writing programs that are actually executed by the CPU, and that run with some semblance of performance. I have taken the time to...
5
by: iam1708 via DotNetMonster.com | last post by:
I was looking at the docs for Thread and can't understand the different between "unnamed data slot "and "data slot".and docs in Thread.GetData() say "Threads use a local store memory mechanism to...
0
by: John Whitmer | last post by:
I am trying to put together a web page to show remote users which PC's are available for RDP so they can use. The page work's fine except for when the goes to the pc to pull the info on if anybody...
9
by: TCMA | last post by:
I am looking for some tools to help me understand source code of a program written in C++ by someone else. Are there any non-commercial, open source C or C++ tools to reverse engineer C or C++...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
40
by: aslamhenry | last post by:
please key in any 5 digits number : 56789 and the ouput is 5678 9 567 89 56 789 5 6789
14
by: 2005 | last post by:
Would it suppress errors? Is he trying to hide errors in his code?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.