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

  • Home
  • SEARCH
  • 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 9199283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:29:59+00:00 2026-06-17T22:29:59+00:00

This nice little Python decorator can configurably disabled decorated functions: enabled = get_bool_from_config() def

  • 0

This nice little Python decorator can configurably disabled decorated functions:

enabled = get_bool_from_config()

def run_if_enabled(fn):
    def wrapped(*args, **kwargs):
        try:
            return fn(*args, **kwargs) if enabled else None
        except Exception:
            log.exception('')
            return None
    return wrapped

alas, if an exception is raised within fn() the traceback shows only up to the wrapper:

Traceback (most recent call last):
  File "C:\my_proj\run.py", line 46, in wrapped
    return fn(*args, **kwargs) if enabled else None
  File "C:\my_proj\run.py", line 490, in a_decorated_function
    some_dict['some_value']
KeyError: 'some_value'
  1. Why?
  2. Can I workaround to see the full traceback?
  • 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-06-17T22:30:00+00:00Added an answer on June 17, 2026 at 10:30 pm

    Ah! Ok, now this is an interesting question!

    Here is is the same approximate function, but grabbing the exception directly from sys.exc_info():

    import sys
    import traceback
    
    def save_if_allowed(fn):
        def wrapped(*args, **kwargs):
            try:
                return fn(*args, **kwargs) if enabled else None
            except Exception:
                print "The exception:"
                print "".join(traceback.format_exception(*sys.exc_info()))
                return None
        return wrapped
    
    @save_if_allowed
    def stuff():
        raise Exception("stuff")
    
    
    def foo():
        stuff()
    
    foo()
    

    And it’s true: no higher stack frames are included in the traceback that’s printed:

    $ python test.py
    The exception:
    Traceback (most recent call last):
      File "x.py", line 21, in wrapped
        return fn(*args, **kwargs) if enabled else None
      File "x.py", line 29, in stuff
        raise Exception("stuff")
    Exception: stuff
    

    Now, to narrow this down a bit, I suspect it’s happening because the stack frame only includes stack information up until the most recent try/except block… So we should be able to recreate this without the decorator:

    $ cat test.py
    def inner():
        raise Exception("inner")
    
    def outer():
        try:
            inner()
        except Exception:
            print "".join(traceback.format_exception(*sys.exc_info()))
    
    def caller():
        outer()
    
    caller()
    
    $ python test.py
    Traceback (most recent call last):
      File "x.py", line 42, in outer
        inner()
      File "x.py", line 38, in inner
        raise Exception("inner")
    Exception: inner
    

    Ah ha! Now, on reflection, this does make sense in a certain kind of way: at this point, the exception has only encountered two stack frames: that of inner() and that of outer() — the exception doesn’t yet know from whence outer() was called.

    So, to get the complete stack, you’ll need to combine the current stack with the exception’s stack:

    $ cat test.py
    def inner():
        raise Exception("inner")
    
    def outer():
        try:
            inner()
        except Exception:
            exc_info = sys.exc_info()
            stack = traceback.extract_stack()
            tb = traceback.extract_tb(exc_info[2])
            full_tb = stack[:-1] + tb
            exc_line = traceback.format_exception_only(*exc_info[:2])
            print "Traceback (most recent call last):"
            print "".join(traceback.format_list(full_tb)),
            print "".join(exc_line)
    
    def caller():
        outer()
    
    caller()
    
    $ python test.py
    Traceback (most recent call last):
      File "test.py", line 56, in <module>
        caller()
      File "test.py", line 54, in caller
        outer()
      File "test.py", line 42, in outer
        inner()
      File "test.py", line 38, in inner
        raise Exception("inner")
    Exception: inner
    

    See also:

    • http://docs.python.org/2/library/sys.html
    • http://docs.python.org/2/library/traceback.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Made this nice little loop for hiding and showing div's, works as a charm
I have this nice little MSBuild-based daily build setup that I use on my
So, I found this nice little query: Select count(*) FROM Table_X WHERE year(DATE) =
I am getting frustrated at the fact, that this little nice menu will wrap
Hey, I've got this nice little piece of code, much like all the other
So, I have this nice little view that I've made, which basically shows two
I have got this nice little method to remove control characters from a string.
I have this nice little line of code <html style=background-image:url(file:///C:\inetpub\wwwroot\lifehacks.png); background-repeat:repeat; background-attachment:fixed; overflow:scroll;> I'm
So I use this nice little widget to create tabs but my editor is
I'm using this nice little jQuery script to rotate a background image: http://www.diplo.co.uk/blog/2011/2/23/simple-jquery-background-image-rotator.aspx The

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.