[pypy-svn] pypy shorter-float-repr: Can't use the "with" statement here :-(

amauryfa commits-noreply at bitbucket.org
Mon Jan 24 18:35:25 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: shorter-float-repr
Changeset: r41266:415545b6a320
Date: 2011-01-24 18:33 +0100
http://bitbucket.org/pypy/pypy/changeset/415545b6a320/

Log:	Can't use the "with" statement here :-( because this code is also
	used by the ll implementation selected by the rtyper, and it seems
	that new RPython types cannot be created at this stage.

	Translation succeeded, but many tests failed.

diff --git a/pypy/rlib/rdtoa.py b/pypy/rlib/rdtoa.py
--- a/pypy/rlib/rdtoa.py
+++ b/pypy/rlib/rdtoa.py
@@ -39,14 +39,20 @@
     compilation_info=eci)
 
 def strtod(input):
-    with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as end_ptr:
-        with rffi.scoped_str2charp(input) as ll_input:
+    end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
+    try:
+        ll_input = rffi.str2charp(input)
+        try:
             result = dg_strtod(ll_input, end_ptr)
             if end_ptr[0] and ord(end_ptr[0][0]):
                 offset = (rffi.cast(rffi.LONG, end_ptr[0]) -
                           rffi.cast(rffi.LONG, ll_input))
                 raise ValueError("invalid input at position %d" % (offset,))
             return result
+        finally:
+            rffi.free_charp(ll_input)
+    finally:
+        lltype.free(end_ptr, flavor='raw')
 
 def format_nonfinite(digits, sign, flags):
     "Format dtoa's output for nonfinite numbers"
@@ -200,9 +206,12 @@
     return s
 
 def dtoa(value, code='r', mode=0, precision=0, flags=0):
-    with lltype.scoped_alloc(rffi.INTP.TO, 1) as decpt_ptr:
-        with lltype.scoped_alloc(rffi.INTP.TO, 1) as sign_ptr:
-            with lltype.scoped_alloc(rffi.CCHARPP.TO, 1) as end_ptr:
+    decpt_ptr = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
+    try:
+        sign_ptr = lltype.malloc(rffi.INTP.TO, 1, flavor='raw')
+        try:
+            end_ptr = lltype.malloc(rffi.CCHARPP.TO, 1, flavor='raw')
+            try:
                 digits = dg_dtoa(value, mode, precision,
                                      decpt_ptr, sign_ptr, end_ptr)
                 try:
@@ -221,6 +230,12 @@
 
                 finally:
                     dg_freedtoa(digits)
+            finally:
+                lltype.free(end_ptr, flavor='raw')
+        finally:
+            lltype.free(sign_ptr, flavor='raw')
+    finally:
+        lltype.free(decpt_ptr, flavor='raw')
 
 def dtoa_formatd(value, code, precision, flags):
     if code in 'EFG':


More information about the Pypy-commit mailing list