Author: Amaury Forgeot d'Arc <[email protected]>
Branch: decimal-libmpdec
Changeset: r73803:7838c75545bd
Date: 2014-09-28 15:35 +0200
http://bitbucket.org/pypy/pypy/changeset/7838c75545bd/
Log: Decimal.to_eng_string()
diff --git a/pypy/module/_decimal/interp_decimal.py
b/pypy/module/_decimal/interp_decimal.py
--- a/pypy/module/_decimal/interp_decimal.py
+++ b/pypy/module/_decimal/interp_decimal.py
@@ -497,6 +497,19 @@
cp = rmpdec.mpd_class(self.mpd, context.ctx)
return space.wrap(rffi.charp2str(cp))
+ def to_eng_string_w(self, space, w_context=None):
+ context = interp_context.ensure_context(space, w_context)
+ with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as cp_ptr:
+ size = rmpdec.mpd_to_eng_size(cp_ptr, self.mpd, context.capitals)
+ if size < 0:
+ raise OperationError(space.w_MemoryError, space.w_None)
+ cp = cp_ptr[0]
+ try:
+ result = rffi.charpsize2str(cp, size)
+ finally:
+ rmpdec.mpd_free(cp)
+ return space.wrap(result) # Convert bytes to unicode
+
def to_integral_w(self, space, w_rounding=None, w_context=None):
context = interp_context.ensure_context(space, w_context)
w_workctx = context.copy_w(space)
@@ -1128,6 +1141,7 @@
next_plus = make_unary_method('mpd_qnext_plus'),
normalize = make_unary_method('mpd_qreduce'),
number_class = interp2app(W_Decimal.number_class_w),
+ to_eng_string = interp2app(W_Decimal.to_eng_string_w),
to_integral = interp2app(W_Decimal.to_integral_w),
to_integral_exact = interp2app(W_Decimal.to_integral_exact_w),
to_integral_value = interp2app(W_Decimal.to_integral_w),
diff --git a/rpython/rlib/rmpdec.py b/rpython/rlib/rmpdec.py
--- a/rpython/rlib/rmpdec.py
+++ b/rpython/rlib/rmpdec.py
@@ -46,7 +46,7 @@
"mpd_qsetprec", "mpd_qsetemin", "mpd_qsetemax", "mpd_qsetround",
"mpd_qsetclamp",
"mpd_maxcontext",
"mpd_qnew", "mpd_del",
- "mpd_to_sci", "mpd_to_sci_size",
+ "mpd_to_sci", "mpd_to_sci_size", "mpd_to_eng_size",
"mpd_iszero", "mpd_isnegative", "mpd_issigned",
"mpd_isfinite", "mpd_isinfinite",
"mpd_isnormal", "mpd_issubnormal", "mpd_isspecial", "mpd_iscanonical",
@@ -244,6 +244,8 @@
'mpd_to_sci', [MPD_PTR, rffi.INT], rffi.CCHARP)
mpd_to_sci_size = external(
'mpd_to_sci_size', [rffi.CCHARPP, MPD_PTR, rffi.INT], rffi.SSIZE_T)
+mpd_to_eng_size = external(
+ 'mpd_to_eng_size', [rffi.CCHARPP, MPD_PTR, rffi.INT], rffi.SSIZE_T)
# Operations
mpd_iszero = external(
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit