PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/longobject.py

https://bitbucket.org/pypy/pypy/
Python | 238 lines | 158 code | 20 blank | 60 comment | 16 complexity | 99806c4c05832b79ad5e5e65f827f485 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rtyper.lltypesystem import lltype, rffi
  2. from pypy.module.cpyext.api import (
  3. cpython_api, PyObject, build_type_checkers, Py_ssize_t,
  4. CONST_STRING, ADDR, CANNOT_FAIL)
  5. from pypy.objspace.std.longobject import W_LongObject
  6. from pypy.interpreter.error import OperationError
  7. from pypy.module.cpyext.intobject import PyInt_AsUnsignedLongMask
  8. from rpython.rlib.rbigint import rbigint
  9. from rpython.rlib.rarithmetic import intmask
  10. PyLong_Check, PyLong_CheckExact = build_type_checkers("Long")
  11. @cpython_api([lltype.Signed], PyObject)
  12. def PyLong_FromLong(space, val):
  13. """Return a new PyLongObject object from v, or NULL on failure."""
  14. return space.newlong(val)
  15. @cpython_api([Py_ssize_t], PyObject)
  16. def PyLong_FromSsize_t(space, val):
  17. """Return a new PyLongObject object from a C Py_ssize_t, or
  18. NULL on failure.
  19. """
  20. return space.newlong(val)
  21. @cpython_api([rffi.SIZE_T], PyObject)
  22. def PyLong_FromSize_t(space, val):
  23. """Return a new PyLongObject object from a C size_t, or NULL on
  24. failure.
  25. """
  26. return space.wrap(val)
  27. @cpython_api([rffi.LONGLONG], PyObject)
  28. def PyLong_FromLongLong(space, val):
  29. """Return a new PyLongObject object from a C long long, or NULL
  30. on failure."""
  31. return space.wrap(val)
  32. @cpython_api([rffi.ULONG], PyObject)
  33. def PyLong_FromUnsignedLong(space, val):
  34. """Return a new PyLongObject object from a C unsigned long, or
  35. NULL on failure."""
  36. return space.wrap(val)
  37. @cpython_api([rffi.ULONGLONG], PyObject)
  38. def PyLong_FromUnsignedLongLong(space, val):
  39. """Return a new PyLongObject object from a C unsigned long long,
  40. or NULL on failure."""
  41. return space.wrap(val)
  42. @cpython_api([PyObject], rffi.ULONG, error=-1)
  43. def PyLong_AsUnsignedLong(space, w_long):
  44. """
  45. Return a C unsigned long representation of the contents of pylong.
  46. If pylong is greater than ULONG_MAX, an OverflowError is
  47. raised."""
  48. try:
  49. return rffi.cast(rffi.ULONG, space.uint_w(w_long))
  50. except OperationError as e:
  51. if e.match(space, space.w_ValueError):
  52. e.w_type = space.w_OverflowError
  53. raise
  54. @cpython_api([PyObject], rffi.ULONG, error=-1)
  55. def PyLong_AsUnsignedLongMask(space, w_long):
  56. """Return a C unsigned long from a Python long integer, without checking
  57. for overflow.
  58. """
  59. return PyInt_AsUnsignedLongMask(space, w_long)
  60. @cpython_api([PyObject], lltype.Signed, error=-1)
  61. def PyLong_AsLong(space, w_long):
  62. """
  63. Return a C long representation of the contents of pylong. If
  64. pylong is greater than LONG_MAX, an OverflowError is raised
  65. and -1 will be returned."""
  66. return space.int_w(w_long)
  67. @cpython_api([PyObject], Py_ssize_t, error=-1)
  68. def PyLong_AsSsize_t(space, w_long):
  69. """Return a C Py_ssize_t representation of the contents of pylong. If
  70. pylong is greater than PY_SSIZE_T_MAX, an OverflowError is raised
  71. and -1 will be returned.
  72. """
  73. return space.int_w(w_long)
  74. @cpython_api([PyObject], rffi.LONGLONG, error=-1)
  75. def PyLong_AsLongLong(space, w_long):
  76. """
  77. Return a C unsigned long representation of the contents of pylong.
  78. If pylong is greater than ULONG_MAX, an OverflowError is
  79. raised."""
  80. return rffi.cast(rffi.LONGLONG, space.r_longlong_w(w_long))
  81. @cpython_api([PyObject], rffi.ULONGLONG, error=-1)
  82. def PyLong_AsUnsignedLongLong(space, w_long):
  83. """
  84. Return a C unsigned long representation of the contents of pylong.
  85. If pylong is greater than ULONG_MAX, an OverflowError is
  86. raised."""
  87. try:
  88. return rffi.cast(rffi.ULONGLONG, space.r_ulonglong_w(w_long))
  89. except OperationError as e:
  90. if e.match(space, space.w_ValueError):
  91. e.w_type = space.w_OverflowError
  92. raise
  93. @cpython_api([PyObject], rffi.ULONGLONG, error=-1)
  94. def PyLong_AsUnsignedLongLongMask(space, w_long):
  95. """Will first attempt to cast the object to a PyIntObject or
  96. PyLongObject, if it is not already one, and then return its value as
  97. unsigned long long, without checking for overflow.
  98. """
  99. num = space.bigint_w(w_long)
  100. return num.ulonglongmask()
  101. @cpython_api([PyObject, rffi.CArrayPtr(rffi.INT_real)], lltype.Signed,
  102. error=-1)
  103. def PyLong_AsLongAndOverflow(space, w_long, overflow_ptr):
  104. """
  105. Return a C long representation of the contents of pylong. If pylong is
  106. greater than LONG_MAX or less than LONG_MIN, set *overflow to 1 or -1,
  107. respectively, and return -1; otherwise, set *overflow to 0. If any other
  108. exception occurs (for example a TypeError or MemoryError), then -1 will be
  109. returned and *overflow will be 0."""
  110. overflow_ptr[0] = rffi.cast(rffi.INT_real, 0)
  111. try:
  112. return space.int_w(w_long)
  113. except OperationError as e:
  114. if not e.match(space, space.w_OverflowError):
  115. raise
  116. if space.is_true(space.gt(w_long, space.wrap(0))):
  117. overflow_ptr[0] = rffi.cast(rffi.INT_real, 1)
  118. else:
  119. overflow_ptr[0] = rffi.cast(rffi.INT_real, -1)
  120. return -1
  121. @cpython_api([PyObject, rffi.CArrayPtr(rffi.INT_real)], rffi.LONGLONG,
  122. error=-1)
  123. def PyLong_AsLongLongAndOverflow(space, w_long, overflow_ptr):
  124. """
  125. Return a C long long representation of the contents of pylong. If pylong is
  126. greater than PY_LLONG_MAX or less than PY_LLONG_MIN, set *overflow to 1 or
  127. -1, respectively, and return -1; otherwise, set *overflow to 0. If any
  128. other exception occurs (for example a TypeError or MemoryError), then -1
  129. will be returned and *overflow will be 0."""
  130. overflow_ptr[0] = rffi.cast(rffi.INT_real, 0)
  131. try:
  132. return rffi.cast(rffi.LONGLONG, space.r_longlong_w(w_long))
  133. except OperationError as e:
  134. if not e.match(space, space.w_OverflowError):
  135. raise
  136. if space.is_true(space.gt(w_long, space.wrap(0))):
  137. overflow_ptr[0] = rffi.cast(rffi.INT_real, 1)
  138. else:
  139. overflow_ptr[0] = rffi.cast(rffi.INT_real, -1)
  140. return -1
  141. @cpython_api([lltype.Float], PyObject)
  142. def PyLong_FromDouble(space, val):
  143. """Return a new PyLongObject object from v, or NULL on failure."""
  144. return space.long(space.wrap(val))
  145. @cpython_api([PyObject], lltype.Float, error=-1.0)
  146. def PyLong_AsDouble(space, w_long):
  147. """Return a C double representation of the contents of pylong. If
  148. pylong cannot be approximately represented as a double, an
  149. OverflowError exception is raised and -1.0 will be returned."""
  150. return space.float_w(space.float(w_long))
  151. @cpython_api([CONST_STRING, rffi.CCHARPP, rffi.INT_real], PyObject)
  152. def PyLong_FromString(space, str, pend, base):
  153. """Return a new PyLongObject based on the string value in str, which is
  154. interpreted according to the radix in base. If pend is non-NULL,
  155. *pend will point to the first character in str which follows the
  156. representation of the number. If base is 0, the radix will be determined
  157. based on the leading characters of str: if str starts with '0x' or
  158. '0X', radix 16 will be used; if str starts with '0', radix 8 will be
  159. used; otherwise radix 10 will be used. If base is not 0, it must be
  160. between 2 and 36, inclusive. Leading spaces are ignored. If there are
  161. no digits, ValueError will be raised."""
  162. s = rffi.charp2str(str)
  163. w_str = space.wrap(s)
  164. w_base = space.wrap(rffi.cast(lltype.Signed, base))
  165. if pend:
  166. pend[0] = rffi.ptradd(str, len(s))
  167. return space.call_function(space.w_long, w_str, w_base)
  168. @cpython_api([rffi.CWCHARP, Py_ssize_t, rffi.INT_real], PyObject)
  169. def PyLong_FromUnicode(space, u, length, base):
  170. """Convert a sequence of Unicode digits to a Python long integer value.
  171. The first parameter, u, points to the first character of the Unicode
  172. string, length gives the number of characters, and base is the radix
  173. for the conversion. The radix must be in the range [2, 36]; if it is
  174. out of range, ValueError will be raised."""
  175. w_value = space.wrap(rffi.wcharpsize2unicode(u, length))
  176. w_base = space.wrap(rffi.cast(lltype.Signed, base))
  177. return space.call_function(space.w_long, w_value, w_base)
  178. @cpython_api([rffi.VOIDP], PyObject)
  179. def PyLong_FromVoidPtr(space, p):
  180. """Create a Python integer or long integer from the pointer p. The pointer value
  181. can be retrieved from the resulting value using PyLong_AsVoidPtr().
  182. If the integer is larger than LONG_MAX, a positive long integer is returned."""
  183. return space.wrap(rffi.cast(ADDR, p))
  184. @cpython_api([PyObject], rffi.VOIDP, error=lltype.nullptr(rffi.VOIDP.TO))
  185. def PyLong_AsVoidPtr(space, w_long):
  186. """Convert a Python integer or long integer pylong to a C void pointer.
  187. If pylong cannot be converted, an OverflowError will be raised. This
  188. is only assured to produce a usable void pointer for values created
  189. with PyLong_FromVoidPtr().
  190. For values outside 0..LONG_MAX, both signed and unsigned integers are accepted."""
  191. return rffi.cast(rffi.VOIDP, space.uint_w(w_long))
  192. @cpython_api([PyObject], rffi.SIZE_T, error=-1)
  193. def _PyLong_NumBits(space, w_long):
  194. return space.uint_w(space.call_method(w_long, "bit_length"))
  195. @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
  196. def _PyLong_Sign(space, w_long):
  197. assert isinstance(w_long, W_LongObject)
  198. return w_long.num.sign
  199. UCHARP = rffi.CArrayPtr(rffi.UCHAR)
  200. @cpython_api([UCHARP, rffi.SIZE_T, rffi.INT_real, rffi.INT_real], PyObject)
  201. def _PyLong_FromByteArray(space, bytes, n, little_endian, signed):
  202. little_endian = rffi.cast(lltype.Signed, little_endian)
  203. signed = rffi.cast(lltype.Signed, signed)
  204. s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, bytes),
  205. rffi.cast(lltype.Signed, n))
  206. if little_endian:
  207. byteorder = 'little'
  208. else:
  209. byteorder = 'big'
  210. result = rbigint.frombytes(s, byteorder, signed != 0)
  211. return space.newlong_from_rbigint(result)