Skip to content

Commit 81c5b64

Browse files
committed
Add a decorator for precision contexts
This allows wrapping functions to set internal precisions using context managers
1 parent 2c12f33 commit 81c5b64

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/flint/flint_base/flint_context.pyx

+20
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()
@@ -92,6 +94,24 @@ class PrecisionManager:
9294
self.eprec = eprec
9395
self.edps = edps
9496

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

0 commit comments

Comments
 (0)