Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/IronPython/Runtime/FunctionCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ internal void LightThrowCompile(CodeContext/*!*/ context) {

lock (pyCtx._codeUpdateLock) {
if (context.LanguageContext.EnableTracing == enableTracing) {
LightThrowTarget = target;
LightThrowTarget = _lambda is Compiler.Ast.FunctionDefinition ? AddRecursionCheck(pyCtx, target) : target;
}
}
} catch (InvalidOperationException ex) {
Expand Down
5 changes: 5 additions & 0 deletions src/core/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,11 @@ public static void FunctionPushFrame(PythonContext context) {
if (PythonFunction.AddRecursionDepth(1) > context.RecursionLimit) {
throw PythonOps.RecursionError("maximum recursion depth exceeded");
}
try {
RuntimeHelpers.EnsureSufficientExecutionStack();
} catch (InsufficientExecutionStackException) {
throw PythonOps.RecursionError("maximum recursion depth exceeded");
}
}

public static void FunctionPushFrameCodeContext(CodeContext context) {
Expand Down
61 changes: 61 additions & 0 deletions tests/suite/modules/system_related/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,67 @@ def f(n):
print(False)
except RecursionError:
print(True)
"""
out = subprocess.check_output([sys.executable, '-c', code])
self.assertEqual(out.strip(), b'True')

def test_setrecursionlimit_light_throw_target(self):
import subprocess
code = """import sys
import time
from System.Threading import Thread, ThreadStart

def f(n, boom):
try:
if n == 0:
if boom:
raise ZeroDivisionError
return 0
return f(n - 1, boom)
except KeyError:
return -1

sys.setrecursionlimit(200)
for _ in range(40):
try:
f(2, True)
except ZeroDivisionError:
pass
time.sleep(3)

out = []
def run():
try:
eval("f(1000000, False)")
out.append(False)
except RecursionError:
out.append(True)

t = Thread(ThreadStart(run), 256 * 1024)
t.Start()
t.Join()
print(out[0])
"""
out = subprocess.check_output([sys.executable, '-c', code])
self.assertEqual(out.strip(), b'True')

@unittest.skipUnless(is_cli, 'IronPython specific test')
def test_setrecursionlimit_small_stack(self):
import subprocess
code = """import sys
from System.Threading import Thread, ThreadStart
sys.setrecursionlimit(10000000)
def f(n):
return f(n + 1) + 1
def run():
try:
f(0)
print(False)
except RecursionError:
print(True)
t = Thread(ThreadStart(run), 512 * 1024)
t.Start()
t.Join()
"""
out = subprocess.check_output([sys.executable, '-c', code])
self.assertEqual(out.strip(), b'True')
Expand Down
Loading