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 8521259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:46:20+00:00 2026-06-11T06:46:20+00:00

Possible Duplicate: How can I modify a Python traceback object when raising an exception?

  • 0

Possible Duplicate:
How can I modify a Python traceback object when raising an exception?

Consider this toy example:

def twice(n):
    _validate_twice_args(n)
    return 2*n

def _validate_twice_args(n):
    if type(n) != int:
        raise TypeError('n must be an int')

twice(None)
--
Traceback (most recent call last):
  File "demo.py", line 9, in <module>
    twice(None)
  File "demo.py", line 2, in twice
    _validate_twice_args(n)
  File "demo.py", line 7, in _validate_twice_args
    raise TypeError('n must be an int')
TypeError: n must be an int

Even though the locus of the error is the call twice(None), the traceback refers to code that the person responsible for the error is not even aware of, (“_validate who? I never called that! I don’t even know what it is!”), and unnecessarily exposes code that should be “behind the API”. Even in the absence of “private” helper functions like _validate_twice_args, the stacktrace printed in response to bad arguments unnecessarily exposes internal code, and obscures the locus of the error.

For example, if one inlines the code of _validate_twice_args, the stack trace looks like this:

Traceback (most recent call last):
  File "demo.py", line 10, in <module>
    twice(None)
  File "demo.py", line 3, in twice
    raise TypeError('n must be an int')
TypeError: n must be an int

To get an idea of what the stack trace for this sort of error should look like, here’s the one resulting from a very similar type of error, the call twice() instead of twice(None), but one that is raised by Python, before control is passed to twice:

Traceback (most recent call last):
  File "demo.py", line 9, in <module>
    twice()
TypeError: twice() takes exactly 1 argument (0 given)

In this case too, the error lies in a call to twice with an invalid arguments list. Accordingly the stack trace points directly to the locus of the error (and not to the line in the underlying [most likely C] code where the error was first detected, thank goodness). This is as it should be, IMO1.

How can I modify twice_args and/or _validate_twice_args so that the last line of the stacktrace refers to the source code line of the buggy call to twice?


1I realize that opinions on what the stacktrace should say in such cases differ from mine, but these philosophical considerations are not the subject of this thread.

Edit: emphasized remarks in the original post, but apparently overlooked by the first two responders.

  • 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-11T06:46:22+00:00Added an answer on June 11, 2026 at 6:46 am

    See How can I modify a Python traceback object when raising an exception?

    There you’ll find more advice not to do this, and also a link to some ugly code in jinja2 that shows you how to do it if you really must.

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

Sidebar

Related Questions

Possible Duplicate: How can I prevent SQL injection in PHP? This is the example
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Can you animate a height change on a UITableViewCell when selected? This
Possible Duplicate: Can Read-Only Properties be Implemented in Pure JavaScript? I have an Object
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Edit: This has nothing
Possible Duplicate: Can distribute setuptools be used to port packages implemented in python 2
Possible Duplicate: Can you write object oriented code in C? I'm wondering if it's
Possible Duplicate: Can I install Python 2.7.1 64bit along side of an exsiting 32bit
Possible Duplicate: String literals vs const char* in C Why i can do this?
Possible Duplicate: Can I multiply charAt in Java? When I compile this : String

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.