Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6570787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:48:00+00:00 2026-05-25T14:48:00+00:00

I’m aware that the sys.exc_info documentation says to take care when dealing with traceback

  • 0

I’m aware that the sys.exc_info documentation says to take care when dealing with traceback objects, but am still uncertain of how safe or unsafe some cases are. Additionally, the documentation says “Warning: Don’t do this!”, followed immediately by “Note: Actually, its ok”, which further confuses me.

In any case, the docs and “Why is there a need to explicitly delete the sys.exc_info() traceback in Python?” (Alex Martelli’s answer), seem to imply its only local variables that reference the traceback value assigned to them that cause a problem.

This leaves me with a few questions:

  1. What, exactly, does “local variable” mean in this context? I’m struggling for terminology, but: does this mean only variables created in the function, or also variable created by the function parameters? What about other variables in scope, e.g, globals or self?
  2. How do closures affect the potential circular references of the traceback? The general thought being: a closure can reference everything its enclosing function can, so a traceback with a reference to a closure can end up referencing quite a bit. I’m struggling to come up with a more concrete example, but some combination of: an inner function, code that returns sys.exc_info(), with expensive-short-lived-objects in scope somewhere.

Feel free to tell me where my conclusions or assumptions are wrong, as I’ve reasoned myself into belief and non-belief of my own statements several times as I’ve written this :).

While I’d like answers to my specific examples, I’m also asking for general advice, knowledge, or war stories on how to safely deal with tracebacks in more esoteric situations (e.g, you have to run a loop and want to accumulate any raised exceptions, you have to spawn a new thread and need to report any of its raised exceptions, you have to create closures and callbacks and have to communicate back raised exceptions, etc).

Example 1: an inner function that does error handling

def DoWebRequest():
  thread, error_queue = CreateThread(ErrorRaisingFunc)
  thread.start()
  thread.join()
  if not error_queue.empty():
    # Purposefully not calling error_queue.get() for illustrative purposes
    print 'error!'

def CreateThread(func):
  error_queue = Queue.Queue()
  def Handled():
    try:
      func()
    except Exception:
      error_queue.put(sys.exc_info())
  thread = threading.Thread(target=Handled)
  return thread, error_queue

Does the Handled() closure cause any raised exception to reference error_queue and result in a circular reference because error_queue also contains the traceback? Is removing the traceback from error_queue (i.e., calling .get()) enough to eliminate the circular-reference?

Example 2: a long lived object in scope of exc_info, or returning exc_info

long_lived_cache = {}

def Alpha(key):
  expensive_object = long_lived_cache.get(key)
  if not expensive_object:
    expensive_object = ComputeExpensiveObject()
    long_lived_cache[key] = expensive_object

  exc_info = AlphaSub(expensive_object)
  if exc_info:
    print 'error!', exc_info

def AlphaSub(expensive_object):
  try:
    ErrorRaisingFunc(expensive_object)
    return None
  except Exception:
    return sys.exc_info()

Does the raised exception of AlphaSub() have a reference to expensive_object, and, because expensive_object is cached, the traceback never goes away? If so, how does one break such a cycle?

Alternatively, exc_info contains the Alpha stack frame, and the Alpha stack frame contains the reference to exc_info, resulting in a circular reference. If so, how does one break such a cycle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:48:01+00:00Added an answer on May 25, 2026 at 2:48 pm

    What, exactly, does “local variable” mean in this context? I’m struggling for terminology, but: does this mean only variables created in the function, or also variable created by the function parameters? What about other variables in scope, e.g, globals or self?

    “Local variables” are all name bindings created inside a function. This includes any function parameters, and any variables assigned. E.g. in the following:

    def func(fruwappah, qitzy=None):
        if fruwappah:
            fruit_cake = 'plain'
        else:
            fruit_cake = qitzy
        frosting = 'orange'
    

    the variables fruwappah, qitzy, fruit_cake, and frosting are all local. Oh, and since self is in the function header (when it is, just not in my example ;), it’s also local.

    How do closures affect the potential circular references of the traceback? The general thought being: a closure can reference everything its enclosing function can, so a traceback with a reference to a closure can end up referencing quite a bit. I’m struggling to come up with a more concrete example, but some combination of: an inner function, code that returns sys.exc_info(), with expensive-short-lived-objects in scope somewhere.

    As the answer you linked to states: a traceback references every function (and its variables) that was active at the time the exception occurred. In other words, whether there’s a closure involved or not is irrelevant — assigning to a closure (non-local), or for that matter a global, variable will create a circular reference.

    There are two basic ways to deal with this:

    1. Define a function that will be called after the exception is raised — it won’t have a stack frame in the traceback, so when it ends all its variables– including the traceback –will go away; or
    2. Make sure and del traceback_object when you are done with it.

    Having said all that, I have as yet to need the traceback object in my own code — the Exception, along with its various attributes, has been sufficient so far.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.