Skip to content

Commit 3001b5e

Browse files
committed
Add a decorator for precision contexts
This allows wrapping functions to set internal precisions using context managers
1 parent 183f877 commit 3001b5e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/flint/flint_base/flint_context.pyx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ from flint.flintlib.types.flint cimport (
66
)
77
from flint.utils.conversion cimport prec_to_dps, dps_to_prec
88

9+
from functools import wraps
10+
911
cdef class FlintContext:
1012
def __init__(self):
1113
self.default()
@@ -91,6 +93,24 @@ class PrecisionManager:
9193
self.eprec = eprec
9294
self.edps = edps
9395

96+
def __call__(self, func):
97+
@wraps(func)
98+
def wrapped(*args, **kwargs):
99+
_oldprec = self.ctx.prec
100+
101+
try:
102+
if self.eprec is not None:
103+
self.ctx.prec = self.eprec
104+
105+
if self.edps is not None:
106+
self.ctx.dps = self.edps
107+
108+
return func(*args, **kwargs)
109+
finally:
110+
self.ctx.prec = _oldprec
111+
112+
return wrapped
113+
94114
def __enter__(self):
95115
self._oldprec = self.ctx.prec
96116

0 commit comments

Comments
 (0)