backend.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import os
  2. import sys
  3. #----------------------------------------------------------------------------#
  4. # Support GMPY for high-speed large integer arithmetic. #
  5. # #
  6. # To allow an external module to handle arithmetic, we need to make sure #
  7. # that all high-precision variables are declared of the correct type. MPZ #
  8. # is the constructor for the high-precision type. It defaults to Python's #
  9. # long type but can be assinged another type, typically gmpy.mpz. #
  10. # #
  11. # MPZ must be used for the mantissa component of an mpf and must be used #
  12. # for internal fixed-point operations. #
  13. # #
  14. # Side-effects #
  15. # 1) "is" cannot be used to test for special values. Must use "==". #
  16. # 2) There are bugs in GMPY prior to v1.02 so we must use v1.03 or later. #
  17. #----------------------------------------------------------------------------#
  18. # So we can import it from this module
  19. gmpy = None
  20. sage = None
  21. sage_utils = None
  22. if sys.version_info[0] < 3:
  23. python3 = False
  24. else:
  25. python3 = True
  26. BACKEND = 'python'
  27. if not python3:
  28. MPZ = long
  29. xrange = xrange
  30. basestring = basestring
  31. def exec_(_code_, _globs_=None, _locs_=None):
  32. """Execute code in a namespace."""
  33. if _globs_ is None:
  34. frame = sys._getframe(1)
  35. _globs_ = frame.f_globals
  36. if _locs_ is None:
  37. _locs_ = frame.f_locals
  38. del frame
  39. elif _locs_ is None:
  40. _locs_ = _globs_
  41. exec("""exec _code_ in _globs_, _locs_""")
  42. else:
  43. MPZ = int
  44. xrange = range
  45. basestring = str
  46. import builtins
  47. exec_ = getattr(builtins, "exec")
  48. # Define constants for calculating hash on Python 3.2.
  49. if sys.version_info >= (3, 2):
  50. HASH_MODULUS = sys.hash_info.modulus
  51. if sys.hash_info.width == 32:
  52. HASH_BITS = 31
  53. else:
  54. HASH_BITS = 61
  55. else:
  56. HASH_MODULUS = None
  57. HASH_BITS = None
  58. if 'MPMATH_NOGMPY' not in os.environ:
  59. try:
  60. try:
  61. import gmpy2 as gmpy
  62. except ImportError:
  63. try:
  64. import gmpy
  65. except ImportError:
  66. raise ImportError
  67. if gmpy.version() >= '1.03':
  68. BACKEND = 'gmpy'
  69. MPZ = gmpy.mpz
  70. except:
  71. pass
  72. if ('MPMATH_NOSAGE' not in os.environ and 'SAGE_ROOT' in os.environ or
  73. 'MPMATH_SAGE' in os.environ):
  74. try:
  75. import sage.all
  76. import sage.libs.mpmath.utils as _sage_utils
  77. sage = sage.all
  78. sage_utils = _sage_utils
  79. BACKEND = 'sage'
  80. MPZ = sage.Integer
  81. except:
  82. pass
  83. if 'MPMATH_STRICT' in os.environ:
  84. STRICT = True
  85. else:
  86. STRICT = False
  87. MPZ_TYPE = type(MPZ(0))
  88. MPZ_ZERO = MPZ(0)
  89. MPZ_ONE = MPZ(1)
  90. MPZ_TWO = MPZ(2)
  91. MPZ_THREE = MPZ(3)
  92. MPZ_FIVE = MPZ(5)
  93. try:
  94. if BACKEND == 'python':
  95. int_types = (int, long)
  96. else:
  97. int_types = (int, long, MPZ_TYPE)
  98. except NameError:
  99. if BACKEND == 'python':
  100. int_types = (int,)
  101. else:
  102. int_types = (int, MPZ_TYPE)