Skip to content

Commit 874119d

Browse files
committed
more careful handling of the recursion limit
1 parent a643483 commit 874119d

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/sage/functions/transcendental.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# ****************************************************************************
1818
import sys
1919

20+
from sage.misc.misc import increase_recursion_limit
2021
from sage.rings.integer_ring import ZZ
2122
from sage.rings.real_mpfr import RR
2223
from sage.rings.real_double import RDF
@@ -545,9 +546,8 @@ def _eval_(self, x):
545546
max = x.parent()(1.1)*x + 10
546547
abs_prec = (-self.approximate(max).log2() + rel_prec + 2*max.log2()).ceil()
547548
self._f = {}
548-
if sys.getrecursionlimit() < max + 10:
549-
sys.setrecursionlimit(int(max) + 10)
550-
self._compute_power_series(max.floor(), abs_prec, cache_ring=x.parent())
549+
with increase_recursion_limit(int(max)):
550+
self._compute_power_series(max.floor(), abs_prec, cache_ring=x.parent())
551551
return self._f[n](2*(x-n-x.parent()(0.5)))
552552

553553
def power_series(self, n, abs_prec):

src/sage/misc/misc.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@
3838
# https://www.gnu.org/licenses/
3939
# ****************************************************************************
4040

41+
import contextlib
4142
import functools
4243
import os
4344
import pdb
45+
import sys
4446
import warnings
4547

4648
from .lazy_string import lazy_string
@@ -1167,3 +1169,34 @@ def wrapper(*args, **kwargs):
11671169
return result
11681170
wrapper.has_run = False
11691171
return wrapper
1172+
1173+
1174+
@contextlib.contextmanager
1175+
def increase_recursion_limit(increment):
1176+
r"""
1177+
Context manager to temporarily change the Python maximum recursion depth.
1178+
1179+
INPUT:
1180+
1181+
- `increment`: increment to add to the current limit
1182+
1183+
EXAMPLES::
1184+
1185+
sage: from sage.misc.misc import increase_recursion_limit
1186+
sage: def rec(n): None if n == 0 else rec(n-1)
1187+
sage: rec(10000)
1188+
Traceback (most recent call last):
1189+
...
1190+
RecursionError: maximum recursion depth exceeded...
1191+
sage: with increase_recursion_limit(10000): rec(10000)
1192+
sage: rec(10000)
1193+
Traceback (most recent call last):
1194+
...
1195+
RecursionError: maximum recursion depth exceeded...
1196+
"""
1197+
old_limit = sys.getrecursionlimit()
1198+
sys.setrecursionlimit(old_limit + increment)
1199+
try:
1200+
yield
1201+
finally:
1202+
sys.setrecursionlimit(old_limit)

src/sage/rings/qqbar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@
560560
from sage.misc.fast_methods import Singleton
561561
from sage.misc.cachefunc import cached_method
562562
from sage.misc.lazy_string import lazy_string
563+
from sage.misc.misc import increase_recursion_limit
563564
from sage.structure.coerce import parent_is_numerical, parent_is_real_numerical
564565
from sage.structure.sage_object import SageObject
565566
from sage.structure.richcmp import (richcmp, richcmp_method,
@@ -8543,9 +8544,7 @@ def exactify(self):
85438544
sage: sys.setrecursionlimit(old_recursion_limit)
85448545
"""
85458546
import sys
8546-
old_recursion_limit = sys.getrecursionlimit()
8547-
sys.setrecursionlimit(old_recursion_limit + 10)
8548-
try:
8547+
with increase_recursion_limit(10):
85498548
left = self._left
85508549
right = self._right
85518550
left.exactify()
@@ -8560,8 +8559,7 @@ def exactify(self):
85608559
return ANRational(value)
85618560
else:
85628561
return ANExtensionElement(gen, value)
8563-
finally:
8564-
sys.setrecursionlimit(old_recursion_limit)
8562+
85658563

85668564
# These are the functions used to add, subtract, multiply, and divide
85678565
# algebraic numbers. Basically, we try to compute exactly if both

0 commit comments

Comments
 (0)