This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Incorrect exception behavior in handling recursive call.
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Mark.Shannon, serhiy.storchaka, xxm
Priority: normal Keywords:

Created on 2021-01-18 04:14 by xxm, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg385170 - (view) Author: Xinmeng Xia (xxm) Date: 2021-01-18 04:14
Seeing the following programs, we try to catch a recursive call error in exception handling.  The behaviors between Python 3.10.0a4 and older version are inconsistent. The outputs are attached in the end. The output on Python 3.10.0a4 is very weird. Two "print statements" lie in same "except" block to print "exception info" and a string "kk". "kk" is printed once while "exception info" is printed twice! I think a bug probably exists in Python 3.10.0a4 parser on handling stacks. 


=========================
def foo(c):
         try:
             c = c + 1
             print("ss"+str(c))
             foo(c)
         except Exception as e:
             print(str(e))
             print("kk")
         print(c)
c = 0
foo(c)
=========================

Output in Python 3.10.0a2 and older version(expected)
------------------------------------------------------------------------
ss1
ss2
....
ss996
maximum recursion depth exceeded while calling a Python object
kk
996
995
......
3
2
1
------------------------------------------------------------------------

Output in Python 3.10.0a4 (unexpected)
------------------------------------------------------------------------
ss1
ss2
....
ss996
maximum recursion depth exceeded while calling a Python object
maximum recursion depth exceeded while calling a Python object
kk
995
......
3
2
1
------------------------------------------------------------------------
msg385190 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2021-01-18 11:14
If you make calls in an exception handler that is handling a RecursionError, without unwinding first, then it is likely that another RecursionError may occur.

What is strange is that the second RecursionError is raised after `print(str(e))` has printed the exception, which is weird and needs further investigation.

The following code, using `list.append` shows what happens without the additional RecursionError from print.
`list.append` is safe to use as it never raises a RecursionError.

    import sys
    sys.setrecursionlimit(100)
    events = []

    def foo(c):
        try:
            c = c + 1
            events.append("ss"+str(c))
            foo(c)
        except Exception as e:
            events.append(e)
            events.append("kk")
        events.append(c)

    c = 0
    foo(c)
    for ev in events:
        print(ev)


ss1
ss2
....
ss97
maximum recursion depth exceeded while getting the str of an object
kk
97
95
......
3
2
1
msg385192 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-01-18 11:50
Maybe stdout.flush() or stdout.buffer.flush() raise a RecursionError?

When run the interpreter with option -u the second error is gone.
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87116
2021-01-18 11:50:33serhiy.storchakasetmessages: + msg385192
2021-01-18 11:14:45Mark.Shannonsetmessages: + msg385190
2021-01-18 07:08:01serhiy.storchakasetnosy: + Mark.Shannon, serhiy.storchaka
2021-01-18 04:14:44xxmcreate