[pypy-svn] r48669 - in pypy/dist/pypy: rpython rpython/lltypesystem/module rpython/module rpython/module/test translator/c/src

fijal at codespeak.net fijal at codespeak.net
Wed Nov 14 01:23:56 CET 2007


Author: fijal
Date: Wed Nov 14 01:23:55 2007
New Revision: 48669

Removed:
   pypy/dist/pypy/rpython/lltypesystem/module/ll_strtod.py
Modified:
   pypy/dist/pypy/rpython/extfuncregistry.py
   pypy/dist/pypy/rpython/extfunctable.py
   pypy/dist/pypy/rpython/module/ll_strtod.py
   pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
   pypy/dist/pypy/translator/c/src/ll_strtod.h
Log:
Move ll_strtod to rpython. Because the C level helpers are ok, just make sure
they don't touch the RPython interface. Otherwise just call them via rffi helpers


Modified: pypy/dist/pypy/rpython/extfuncregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extfuncregistry.py	(original)
+++ pypy/dist/pypy/rpython/extfuncregistry.py	Wed Nov 14 01:23:55 2007
@@ -69,3 +69,7 @@
     llimpl = func_with_new_name(func, name)
     register_external(func, args, res, 'll_os_path.ll_%s' % name,
                       llimpl=llimpl, sandboxsafe=True)
+
+# -------------------- strtod functions ----------------------
+
+from pypy.rpython.module import ll_strtod

Modified: pypy/dist/pypy/rpython/extfunctable.py
==============================================================================
--- pypy/dist/pypy/rpython/extfunctable.py	(original)
+++ pypy/dist/pypy/rpython/extfunctable.py	Wed Nov 14 01:23:55 2007
@@ -163,13 +163,6 @@
 ntpath.isabs = isabs
 
 # ___________________________________________________________
-# string->float helper
-from pypy.rlib import rarithmetic
-declare(rarithmetic.parts_to_float, float, 'll_strtod/parts_to_float')
-# float->string helper
-declare(rarithmetic.formatd, str, 'll_strtod/formatd')
-
-# ___________________________________________________________
 # stackless
 from pypy.rlib import rstack
 declare(rstack.stack_frames_depth, int, 'll_stackless/stack_frames_depth')

Modified: pypy/dist/pypy/rpython/module/ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/ll_strtod.py	(original)
+++ pypy/dist/pypy/rpython/module/ll_strtod.py	Wed Nov 14 01:23:55 2007
@@ -1,3 +1,54 @@
 
+import py
+from pypy.rpython.extfunc import BaseLazyRegistering, extdef, registering
+from pypy.rlib import rarithmetic
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.translator.tool.cbuild import cache_c_module
+from pypy.tool.autopath import pypydir
 
+class CConfig:
+    _includes_ = ['ll_strtod.h']
 
+class RegisterStrtod(BaseLazyRegistering):
+    def __init__(self):
+        # HACK HACK HACK
+        # we need to have some sane way of doing stuff below
+        # problem: we don't have a way to call things in our header files
+        from pypy.tool.udir import udir
+        c_file = udir.join('test_strtod.c')
+        c_file.write(py.code.Source("""
+        #include <src/ll_strtod.h>
+        """))
+        cache_c_module([c_file], '_ll_strtod')
+        self._libraries_ = [str(py.path.local(pypydir).join('_cache',
+                                                            '_ll_strtod.so'))]
+        self.configure(CConfig)
+    
+    @registering(rarithmetic.formatd)
+    def register_formatd(self):
+        ll_strtod = self.llexternal('LL_strtod_formatd',
+                                    [rffi.CCHARP, rffi.DOUBLE], rffi.CCHARP,
+                                    sandboxsafe=True, threadsafe=False)
+        
+        def llimpl(fmt, x):
+            res = ll_strtod(fmt, x)
+            return rffi.charp2str(res)
+
+        return extdef([str, float], str, 'll_strtod.ll_strtod_formatd',
+                      llimpl=llimpl)
+
+    @registering(rarithmetic.parts_to_float)
+    def register_parts_to_float(self):
+        ll_parts_to_float = self.llexternal('LL_strtod_parts_to_float',
+                                            [rffi.CCHARP] * 4, rffi.DOUBLE,
+                                            sandboxsafe=True,
+                                            threadsafe=False)
+
+        def llimpl(sign, beforept, afterpt, exponent):
+            res = ll_parts_to_float(sign, beforept, afterpt, exponent)
+            if res == -1 and rffi.get_errno() == 42:
+                raise ValueError("Wrong literal for float")
+            return res
+
+        return extdef([str, str, str, str], float,
+                      'll_strtod.ll_strtod_parts_to_float', llimpl=llimpl)

Modified: pypy/dist/pypy/rpython/module/test/test_ll_strtod.py
==============================================================================
--- pypy/dist/pypy/rpython/module/test/test_ll_strtod.py	(original)
+++ pypy/dist/pypy/rpython/module/test/test_ll_strtod.py	Wed Nov 14 01:23:55 2007
@@ -1,11 +1,18 @@
 import py
 
-class BaseTest(object):
-    
+from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.rlib import rarithmetic
+
+class BaseTestStrtod(BaseRtypingTest):    
+    def test_formatd(self):
+        def f(y):
+            return rarithmetic.formatd("%.2f", y)
+
+        assert self.ll_to_string(self.interpret(f, [3.0])) == f(3.0)
+
     def test_parts_to_float(self):
-        #py.test.skip("wip")
-        Support = self.Support
-        Impl = self.Implementation
+        def f(a, b, c, d):
+            return rarithmetic.parts_to_float(a, b, c, d)
         
         data = [
         (("","1","","")     , 1.0),
@@ -17,20 +24,8 @@
         ]
 
         for parts, val in data:
-            assert Impl.ll_strtod_parts_to_float(*map(Support.to_rstr, parts)) == val
-
-
-    def test_formatd(self):
-        Support = self.Support
-        Impl = self.Implementation
-        
-        res = Impl.ll_strtod_formatd(Support.to_rstr("%.2f"), 1.5)
-        assert Support.from_rstr(res) == "1.50"
+            args = [self.string_to_ll(i) for i in parts]
+            assert self.interpret(f, args) == val
 
-class TestLL(BaseTest):
-    from pypy.rpython.module.support import LLSupport as Support
-    from pypy.rpython.lltypesystem.module.ll_strtod import Implementation
-
-class TestOO(BaseTest):
-    from pypy.rpython.module.support import OOSupport as Support
-    from pypy.rpython.ootypesystem.module.ll_strtod import Implementation
+class TestLLStrtod(BaseTestStrtod, LLRtypeMixin):
+    pass

Modified: pypy/dist/pypy/translator/c/src/ll_strtod.h
==============================================================================
--- pypy/dist/pypy/translator/c/src/ll_strtod.h	(original)
+++ pypy/dist/pypy/translator/c/src/ll_strtod.h	Wed Nov 14 01:23:55 2007
@@ -1,12 +1,15 @@
 #include <locale.h>
 #include <ctype.h>
-
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
 
 /* prototypes */
 
-double LL_strtod_parts_to_float(RPyString *sign, RPyString *beforept,
-				RPyString *afterpt, RPyString *exponent);
-RPyString *LL_strtod_formatd(RPyString *fmt, double x);
+double LL_strtod_parts_to_float(char *sign, char *beforept,
+				char *afterpt, char *exponent);
+char *LL_strtod_formatd(char *fmt, double x);
 
 
 /* implementations */
@@ -14,10 +17,10 @@
 #ifndef PYPY_NOT_MAIN_FILE
 
 double LL_strtod_parts_to_float(
-	RPyString *sign, 
-	RPyString *beforept, 
-	RPyString *afterpt, 
-	RPyString *exponent)
+	char *sign, 
+	char *beforept, 
+	char *afterpt, 
+	char *exponent)
 {
 	char *fail_pos;
 	struct lconv *locale_data;
@@ -25,7 +28,7 @@
 	int decimal_point_len;
 	double x;
 	char *last;
-	char *expo = RPyString_AsString(exponent);
+	char *expo = exponent;
 	int buf_size;
 	char *s;
 
@@ -37,20 +40,20 @@
 	decimal_point = locale_data->decimal_point;
 	decimal_point_len = strlen(decimal_point);
 
-	buf_size = RPyString_Size(sign) + 
-		RPyString_Size(beforept) +
+	buf_size = strlen(sign) + 
+		strlen(beforept) +
 		decimal_point_len +
-		RPyString_Size(afterpt) +
+		strlen(afterpt) +
 		1 /* e */ +
 		strlen(expo) + 
 		1 /*  asciiz  */ ;
 
         s = (char*)malloc(buf_size);
 
-	strcpy(s, RPyString_AsString(sign));
-	strcat(s, RPyString_AsString(beforept));
+	strcpy(s, sign);
+	strcat(s, beforept);
 	strcat(s, decimal_point);
-	strcat(s, RPyString_AsString(afterpt));
+	strcat(s, afterpt);
 	strcat(s, "e");
 	strcat(s, expo);
 
@@ -61,7 +64,7 @@
 		fail_pos = last;
 	if (fail_pos == s || *fail_pos != '\0' || fail_pos != last) {
 		free(s);
-		RPyRaiseSimpleException(PyExc_ValueError, "invalid float literal");
+    errno = 42; // just because
 		return -1.0;
 	}
 	if (x == 0.0) { /* maybe a denormal value, ask for atof behavior */
@@ -72,12 +75,12 @@
 	return x;
 }
 
+char buffer[120]; /* this should be enough, from PyString_Format code */
+int buflen = 120;
 
-RPyString *LL_strtod_formatd(RPyString *fmt, double x) {
-	char buffer[120]; /* this should be enough, from PyString_Format code */
-	int buflen = 120;
+char* LL_strtod_formatd(char *fmt, double x) {
 	int res;
-	res = snprintf(buffer, buflen, RPyString_AsString(fmt), x);
+	res = snprintf(buffer, buflen, fmt, x);
 	if (res <= 0 || res >= buflen) {
 		strcpy(buffer, "??.?"); /* should not occur */
 	} else {
@@ -117,7 +120,7 @@
 		
 	}
 
-	return RPyString_FromString(buffer);
+	return buffer;
 }
 
 #endif /* PYPY_NOT_MAIN_FILE */



More information about the Pypy-commit mailing list