Coverage for /usr/lib/python3/dist-packages/mpmath/libmp/backend.py: 58%

76 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1import os 

2import sys 

3 

4#----------------------------------------------------------------------------# 

5# Support GMPY for high-speed large integer arithmetic. # 

6# # 

7# To allow an external module to handle arithmetic, we need to make sure # 

8# that all high-precision variables are declared of the correct type. MPZ # 

9# is the constructor for the high-precision type. It defaults to Python's # 

10# long type but can be assinged another type, typically gmpy.mpz. # 

11# # 

12# MPZ must be used for the mantissa component of an mpf and must be used # 

13# for internal fixed-point operations. # 

14# # 

15# Side-effects # 

16# 1) "is" cannot be used to test for special values. Must use "==". # 

17# 2) There are bugs in GMPY prior to v1.02 so we must use v1.03 or later. # 

18#----------------------------------------------------------------------------# 

19 

20# So we can import it from this module 

21gmpy = None 

22sage = None 

23sage_utils = None 

24 

25if sys.version_info[0] < 3: 

26 python3 = False 

27else: 

28 python3 = True 

29 

30BACKEND = 'python' 

31 

32if not python3: 

33 MPZ = long 

34 xrange = xrange 

35 basestring = basestring 

36 

37 def exec_(_code_, _globs_=None, _locs_=None): 

38 """Execute code in a namespace.""" 

39 if _globs_ is None: 

40 frame = sys._getframe(1) 

41 _globs_ = frame.f_globals 

42 if _locs_ is None: 

43 _locs_ = frame.f_locals 

44 del frame 

45 elif _locs_ is None: 

46 _locs_ = _globs_ 

47 exec("""exec _code_ in _globs_, _locs_""") 

48else: 

49 MPZ = int 

50 xrange = range 

51 basestring = str 

52 

53 import builtins 

54 exec_ = getattr(builtins, "exec") 

55 

56# Define constants for calculating hash on Python 3.2. 

57if sys.version_info >= (3, 2): 

58 HASH_MODULUS = sys.hash_info.modulus 

59 if sys.hash_info.width == 32: 

60 HASH_BITS = 31 

61 else: 

62 HASH_BITS = 61 

63else: 

64 HASH_MODULUS = None 

65 HASH_BITS = None 

66 

67if 'MPMATH_NOGMPY' not in os.environ: 

68 try: 

69 try: 

70 import gmpy2 as gmpy 

71 except ImportError: 

72 try: 

73 import gmpy 

74 except ImportError: 

75 raise ImportError 

76 if gmpy.version() >= '1.03': 

77 BACKEND = 'gmpy' 

78 MPZ = gmpy.mpz 

79 except: 

80 pass 

81 

82if ('MPMATH_NOSAGE' not in os.environ and 'SAGE_ROOT' in os.environ or 

83 'MPMATH_SAGE' in os.environ): 

84 try: 

85 import sage.all 

86 import sage.libs.mpmath.utils as _sage_utils 

87 sage = sage.all 

88 sage_utils = _sage_utils 

89 BACKEND = 'sage' 

90 MPZ = sage.Integer 

91 except: 

92 pass 

93 

94if 'MPMATH_STRICT' in os.environ: 

95 STRICT = True 

96else: 

97 STRICT = False 

98 

99MPZ_TYPE = type(MPZ(0)) 

100MPZ_ZERO = MPZ(0) 

101MPZ_ONE = MPZ(1) 

102MPZ_TWO = MPZ(2) 

103MPZ_THREE = MPZ(3) 

104MPZ_FIVE = MPZ(5) 

105 

106try: 

107 if BACKEND == 'python': 

108 int_types = (int, long) 

109 else: 

110 int_types = (int, long, MPZ_TYPE) 

111except NameError: 

112 if BACKEND == 'python': 

113 int_types = (int,) 

114 else: 

115 int_types = (int, MPZ_TYPE)