Skip to content

Commit

Permalink
Use sys.modules hacks to prevent setting dps/prec
Browse files Browse the repository at this point in the history
Fixes Issue mpmath#657.

Co-authored-by: Warren Weckesser <warren.weckesser@gmail.com>
  • Loading branch information
cbm755 and WarrenWeckesser committed Apr 1, 2023
1 parent fea8905 commit dade3f3
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions mpmath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
__version__ = '1.3.0'

import sys
import types

from .usertools import monitor, timing

from .ctx_fp import FPContext
Expand Down Expand Up @@ -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():
"""
Expand Down

0 comments on commit dade3f3

Please sign in to comment.