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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:30:52+00:00 2026-06-07T22:30:52+00:00

We have a simple AssertTrue function used in our python project and I wanted

  • 0

We have a simple AssertTrue function used in our python project and I wanted to modify the output it provides to print the code statement from which it was called. The code looks something like this:

1 import traceback
2
3 def AssertTrue(expr, reason=None):
4     print traceback.format_stack()[-2]
5
6 AssertTrue(1 == 2,
7         reason='One is not equal to two')

The output:

File "/tmp/fisken.py", line 7, in <module>
  reason='One is not equal to two')

I’m wondering why traceback.format_stack only gives me the code on line 7. The statement starts on line 6 and the expression I would like to see in the output is also on that same line. Doesn’t traceback handle multi-line function calls?

(Never mind that there are better ways to do AssertTrue(…). I’m just wondering why traceback.format_stack (and .extract_stack) does not behave as I expected it to)

  • 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-07T22:30:53+00:00Added an answer on June 7, 2026 at 10:30 pm

    Doesn’t traceback handle multi-line function calls?

    Many functions are tens or even (horrors) hundreds of lines long. If traceback did print the whole function, then stack traces would become incomprehensibly long. So I guess what you are seeing is an attempt to keep things clean and minimal.

    I have pulled together some answers to similar questions:

    • Python code to get current function into a variable?
    • How can I get the source code of a Python function?

    With the consideration that it inspect can only obtain the source for the whole function (if the source is available on the path) I can offer you this:

    import traceback
    import inspect
    import gc
    
    def giveupthefunc(frame):
        code  = frame.f_code
        globs = frame.f_globals
        functype = type(lambda: 0)
        funcs = []
        for func in gc.get_referrers(code):
            if type(func) is functype:
                if getattr(func, "func_code", None) is code:
                    if getattr(func, "func_globals", None) is globs:
                        funcs.append(func)
                        if len(funcs) > 1:
                            return None
        return funcs[0] if funcs else None
    
    
    def AssertTrue(expr, reason=None):
        print traceback.format_stack()[-2]
        frame = inspect.currentframe().f_back
        func = giveupthefunc(frame)
        if func:
            source = inspect.getsourcelines(func)
            i = source[1]
            for line in source[0]:
                print i, ":", line,
                i += 1
    
    
    
    def my_fun():
        AssertTrue(1 == 2,
                 reason='One is not equal to two')
    
    my_fun()
    

    Which produces:

    /Library/Frameworks/Python.framework/Versions/2.7/bin/python /Users/xxxx/Documents/PycharmProjects/scratchpad/test.py
      File "/Users/xxxx/Documents/PycharmProjects/scratchpad/test.py", line 35, in my_fun
        reason='One is not equal to two')
    
    33 : def my_fun():
    34 :     AssertTrue(1 == 2,
    35 :              reason='One is not equal to two')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple code that uses AudioUnit to render sine wave to the output.
Have simple , empty project without any code. just includes. The include linux/netfilter.h cause
I have simple code: <?php error_reporting(E_ALL); ini_set('display_errors', true); ini_set('html_errors', false); $test = array(); $test
I have simple CI (CodeIgniter) code. what I want to do is: when I
i have simple side menu with this html code : <div id=menu> <div> Menu
I have simple node js http server. var http = require(http); http.createServer(function(request, response) {
I have simple example: function File(name) { this.name = name this.text = null }
I have simple html code that plays YouTube video after click on the image:
I have simple WebView code like this: WebView wv = (WebView) findViewById(R.id.webview1); wv.loadUrl(http://en.wikipedia.org/wiki/Book); But
I have simple html code: <span class=someclass title=4.5 stars></span> or it could be: <span

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.