PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/cpyext/pystrtod.py

https://bitbucket.org/pypy/pypy/
Python | 120 lines | 99 code | 9 blank | 12 comment | 13 complexity | 6af47b30758c43f1e75e44b0506096c0 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import errno
  2. from pypy.interpreter.error import oefmt
  3. from pypy.module.cpyext.api import cpython_api, CONST_STRING
  4. from pypy.module.cpyext.pyobject import PyObject
  5. from rpython.rlib import rdtoa
  6. from rpython.rlib import rfloat
  7. from rpython.rlib import rposix, jit
  8. from rpython.rlib.rarithmetic import intmask
  9. from rpython.rtyper.lltypesystem import lltype
  10. from rpython.rtyper.lltypesystem import rffi
  11. # PyOS_double_to_string's "type", if non-NULL, will be set to one of:
  12. Py_DTST_FINITE = 0
  13. Py_DTST_INFINITE = 1
  14. Py_DTST_NAN = 2
  15. # Match the "type" back to values in CPython
  16. DOUBLE_TO_STRING_TYPES_MAP = {
  17. rfloat.DIST_FINITE: Py_DTST_FINITE,
  18. rfloat.DIST_INFINITY: Py_DTST_INFINITE,
  19. rfloat.DIST_NAN: Py_DTST_NAN
  20. }
  21. @cpython_api([CONST_STRING, rffi.CCHARPP, PyObject], rffi.DOUBLE, error=-1.0)
  22. @jit.dont_look_inside # direct use of _get_errno()
  23. def PyOS_string_to_double(space, s, endptr, w_overflow_exception):
  24. """Convert a string s to a double, raising a Python
  25. exception on failure. The set of accepted strings corresponds to
  26. the set of strings accepted by Python's float() constructor,
  27. except that s must not have leading or trailing whitespace.
  28. The conversion is independent of the current locale.
  29. If endptr is NULL, convert the whole string. Raise
  30. ValueError and return -1.0 if the string is not a valid
  31. representation of a floating-point number.
  32. If endptr is not NULL, convert as much of the string as
  33. possible and set *endptr to point to the first unconverted
  34. character. If no initial segment of the string is the valid
  35. representation of a floating-point number, set *endptr to point
  36. to the beginning of the string, raise ValueError, and return
  37. -1.0.
  38. If s represents a value that is too large to store in a float
  39. (for example, "1e500" is such a string on many platforms) then
  40. if overflow_exception is NULL return Py_HUGE_VAL (with
  41. an appropriate sign) and don't set any exception. Otherwise,
  42. overflow_exception must point to a Python exception object;
  43. raise that exception and return -1.0. In both cases, set
  44. *endptr to point to the first character after the converted value.
  45. If any other error occurs during the conversion (for example an
  46. out-of-memory error), set the appropriate Python exception and
  47. return -1.0.
  48. """
  49. user_endptr = True
  50. try:
  51. if not endptr:
  52. endptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
  53. user_endptr = False
  54. result = rdtoa.dg_strtod(s, endptr)
  55. endpos = (rffi.cast(rffi.LONG, endptr[0]) -
  56. rffi.cast(rffi.LONG, s))
  57. if endpos == 0 or (not user_endptr and not endptr[0][0] == '\0'):
  58. raise oefmt(space.w_ValueError,
  59. "invalid input at position %d", endpos)
  60. err = rffi.cast(lltype.Signed, rposix._get_errno())
  61. if err == errno.ERANGE:
  62. rposix._set_errno(rffi.cast(rffi.INT, 0))
  63. if w_overflow_exception is None:
  64. if result > 0:
  65. return rfloat.INFINITY
  66. else:
  67. return -rfloat.INFINITY
  68. else:
  69. raise oefmt(w_overflow_exception, "value too large")
  70. return result
  71. finally:
  72. if not user_endptr:
  73. lltype.free(endptr, flavor='raw')
  74. @cpython_api([rffi.DOUBLE, lltype.Char, rffi.INT_real, rffi.INT_real, rffi.INTP], rffi.CCHARP)
  75. def PyOS_double_to_string(space, val, format_code, precision, flags, ptype):
  76. """Convert a double val to a string using supplied
  77. format_code, precision, and flags.
  78. format_code must be one of 'e', 'E', 'f', 'F',
  79. 'g', 'G' or 'r'. For 'r', the supplied precision
  80. must be 0 and is ignored. The 'r' format code specifies the
  81. standard repr() format.
  82. flags can be zero or more of the values Py_DTSF_SIGN,
  83. Py_DTSF_ADD_DOT_0, or Py_DTSF_ALT, or-ed together:
  84. Py_DTSF_SIGN means to always precede the returned string with a sign
  85. character, even if val is non-negative.
  86. Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look
  87. like an integer.
  88. Py_DTSF_ALT means to apply "alternate" formatting rules. See the
  89. documentation for the PyOS_snprintf() '#' specifier for
  90. details.
  91. If ptype is non-NULL, then the value it points to will be set to one of
  92. Py_DTST_FINITE, Py_DTST_INFINITE, or Py_DTST_NAN, signifying that
  93. val is a finite number, an infinite number, or not a number, respectively.
  94. The return value is a pointer to buffer with the converted string or
  95. NULL if the conversion failed. The caller is responsible for freeing the
  96. returned string by calling PyMem_Free().
  97. """
  98. buffer, rtype = rfloat.double_to_string(val, format_code,
  99. intmask(precision),
  100. intmask(flags))
  101. if ptype != lltype.nullptr(rffi.INTP.TO):
  102. ptype[0] = rffi.cast(rffi.INT, DOUBLE_TO_STRING_TYPES_MAP[rtype])
  103. bufp = rffi.str2charp(buffer)
  104. return bufp