File tree Expand file tree Collapse file tree 3 files changed +39
-8
lines changed Expand file tree Collapse file tree 3 files changed +39
-8
lines changed Original file line number Diff line number Diff line change 17
17
# ****************************************************************************
18
18
import sys
19
19
20
+ from sage .misc .misc import increase_recursion_limit
20
21
from sage .rings .integer_ring import ZZ
21
22
from sage .rings .real_mpfr import RR
22
23
from sage .rings .real_double import RDF
@@ -545,9 +546,8 @@ def _eval_(self, x):
545
546
max = x .parent ()(1.1 )* x + 10
546
547
abs_prec = (- self .approximate (max ).log2 () + rel_prec + 2 * max .log2 ()).ceil ()
547
548
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 ())
551
551
return self ._f [n ](2 * (x - n - x .parent ()(0.5 )))
552
552
553
553
def power_series (self , n , abs_prec ):
Original file line number Diff line number Diff line change 38
38
# https://www.gnu.org/licenses/
39
39
# ****************************************************************************
40
40
41
+ import contextlib
41
42
import functools
42
43
import os
43
44
import pdb
45
+ import sys
44
46
import warnings
45
47
46
48
from .lazy_string import lazy_string
@@ -1167,3 +1169,34 @@ def wrapper(*args, **kwargs):
1167
1169
return result
1168
1170
wrapper .has_run = False
1169
1171
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 )
Original file line number Diff line number Diff line change 560
560
from sage .misc .fast_methods import Singleton
561
561
from sage .misc .cachefunc import cached_method
562
562
from sage .misc .lazy_string import lazy_string
563
+ from sage .misc .misc import increase_recursion_limit
563
564
from sage .structure .coerce import parent_is_numerical , parent_is_real_numerical
564
565
from sage .structure .sage_object import SageObject
565
566
from sage .structure .richcmp import (richcmp , richcmp_method ,
@@ -8543,9 +8544,7 @@ def exactify(self):
8543
8544
sage: sys.setrecursionlimit(old_recursion_limit)
8544
8545
"""
8545
8546
import sys
8546
- old_recursion_limit = sys .getrecursionlimit ()
8547
- sys .setrecursionlimit (old_recursion_limit + 10 )
8548
- try :
8547
+ with increase_recursion_limit (10 ):
8549
8548
left = self ._left
8550
8549
right = self ._right
8551
8550
left .exactify ()
@@ -8560,8 +8559,7 @@ def exactify(self):
8560
8559
return ANRational (value )
8561
8560
else :
8562
8561
return ANExtensionElement (gen , value )
8563
- finally :
8564
- sys .setrecursionlimit (old_recursion_limit )
8562
+
8565
8563
8566
8564
# These are the functions used to add, subtract, multiply, and divide
8567
8565
# algebraic numbers. Basically, we try to compute exactly if both
You can’t perform that action at this time.
0 commit comments