Mark Dickinson avatar Mark Dickinson committed cf232ad

Move bytes-to-text conversions into the mpfr.py core.

Comments (0)

Files changed (3)

bigfloat_cython/bigfloat/core.py

 
     """
     digits, exp = mpfr.mpfr_get_str(base, ndigits, op, rounding_mode)
-    digits = digits.decode('ascii')
     negative = digits.startswith('-')
     if negative:
         digits = digits[1:]
     a valid string in the given base.
 
     """
-    s = s.encode('ascii').strip()
+    s = s.strip()
     ternary, endindex = mpfr.mpfr_strtofr(rop, s, base, rnd)
     if len(s) != endindex:
         raise ValueError("not a valid numeric string")

bigfloat_cython/bigfloat/test/test_bigfloat.py

 
     """
     import bigfloat.mpfr as mpfr
-    s = s.encode('ascii')
     if s == s.strip():
         ternary, endindex = mpfr.mpfr_strtofr(rop, s, base, rnd)
         if not s[endindex:]:

bigfloat_cython/src/mpfr.pyx

     check_rounding_mode(rnd)
     return cmpfr.mpfr_set_si_2exp(&rop._value, op, e, rnd)
 
-def mpfr_set_str(Mpfr_t rop not None, bytes s, int base, cmpfr.mpfr_rnd_t rnd):
+def mpfr_set_str(Mpfr_t rop not None, unicode s, int base, cmpfr.mpfr_rnd_t rnd):
     """
     Set rop from a string s.
 
     an overflow.
 
     """
+    cdef bytes bytes_s
+    bytes_s = s.encode('ascii')
     check_initialized(rop)
     check_base(base, False)
     check_rounding_mode(rnd)
-    return cmpfr.mpfr_set_str(&rop._value, s, base, rnd)
-
-def mpfr_strtofr(Mpfr_t rop not None, bytes s, int base, cmpfr.mpfr_rnd_t rnd):
+    return cmpfr.mpfr_set_str(&rop._value, bytes_s, base, rnd)
+
+def mpfr_strtofr(Mpfr_t rop not None, unicode s, int base, cmpfr.mpfr_rnd_t rnd):
     """
     Read a floating-point number from a string.
 
     """
     cdef char* endptr
     cdef char* startptr
-
-    startptr = s
+    cdef bytes bytes_s
+
+    bytes_s = s.encode('ascii')
+
+    startptr = bytes_s
 
     check_initialized(rop)
     check_base(base, True)
     check_rounding_mode(rnd)
     ternary = cmpfr.mpfr_strtofr(
         &rop._value,
-        s,
+        bytes_s,
         &endptr,
         base,
         rnd,
     finally:
         cmpfr.mpfr_free_str(c_digits)
 
-    return digits, exp
+    return digits.decode('ascii'), exp
 
 def mpfr_fits_slong_p(Mpfr_t x not None, cmpfr.mpfr_rnd_t rnd):
     """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.