OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Print list (or tree) of active TRY blocks

  • Thread starter Thread starter IronPillow2
  • Start date Start date
I

IronPillow2

Guest
I am debugging some complicated legacy Python3 code and find myself needing to know, at a particular point in the executing code, all the try blocks that are active in the call stack. Is there a way to introspect and print out the nested tree?

So, for example, we might have this code:

Code:
def fn1():
    try:
        fn2()
    except IOError:
        # do something

def fn2():
    try:
        fn3()
    except NameError:
        # do something

def fn3():
    print( **YourAnswerHere** )

The idea is that if I invoke fn1() then the print statement in fn3 would produce something like:

Code:
    fn1: try / except IOError
        fn2: try / except NameError

If that's not possible, I'd be able to get along if it merely printed [fn1 fn2]. Ideally it would handle try / finally blocks as well.

I could find nothing about this using Google. I looked at every occurrence of the word try in the documentation for Python's inspect and traceback modules but found nothing. The ast module has classes related to try but I don't have a clue how to use them and it looks like those are for analyzing static code, rather than executing code. Thank you.
<p>I am debugging some complicated legacy Python3 code and find myself needing to know, at a particular point in the executing code, all the <code>try</code> blocks that are active in the call stack. Is there a way to introspect and print out the nested tree?</p>
<p>So, for example, we might have this code:</p>
<pre><code>def fn1():
try:
fn2()
except IOError:
# do something

def fn2():
try:
fn3()
except NameError:
# do something

def fn3():
print( **YourAnswerHere** )
</code></pre>
<p>The idea is that if I invoke <code>fn1()</code> then the print statement in <code>fn3</code> would produce something like:</p>
<pre><code> fn1: try / except IOError
fn2: try / except NameError
</code></pre>
<p>If that's not possible, I'd be able to get along if it merely printed <code>[fn1 fn2]</code>. Ideally it would handle <code>try / finally</code> blocks as well.</p>
<p>I could find nothing about this using Google. I looked at every occurrence of the word <code>try</code> in the documentation for Python's <code>inspect</code> and <code>traceback</code> modules but found nothing. The <code>ast</code> module has classes related to <code>try</code> but I don't have a clue how to use them and it looks like those are for analyzing static code, rather than executing code. Thank you.</p>
 
Top