From dade3f365a129cf2180cdf184fce819fc1badfbb Mon Sep 17 00:00:00 2001 From: "Colin B. Macdonald" Date: Sat, 1 Apr 2023 15:13:24 -0700 Subject: [PATCH] Use sys.modules hacks to prevent setting dps/prec Fixes Issue #657. Co-authored-by: Warren Weckesser --- mpmath/__init__.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/mpmath/__init__.py b/mpmath/__init__.py index 683233bd1..d3e78e9ee 100644 --- a/mpmath/__init__.py +++ b/mpmath/__init__.py @@ -1,5 +1,8 @@ __version__ = '1.3.0' +import sys +import types + from .usertools import monitor, timing from .ctx_fp import FPContext @@ -433,6 +436,34 @@ unit_triangle = mp.unit_triangle sigmoid = mp.sigmoid + +class SetMPMathPrecisionError(RuntimeError): + + def __init__(self, attribute): + self._attribute = attribute + + def __str__(self): + name = self._attribute + return (f"cannot set '{name}' on 'mpmath'. Did you mean to " + f"set '{name}' on 'mpmath.mp'?") + + +class _MPMathModuleType(types.ModuleType): + + def _set_dps(self, value): + raise SetMPMathPrecisionError('dps') + + dps = property(fset=_set_dps) + + def _set_prec(self, value): + raise SetMPMathPrecisionError('prec') + + prec = property(fset=_set_prec) + + +sys.modules[__name__].__class__ = _MPMathModuleType + + # be careful when changing this name, don't use test*! def runtests(): """