[pypy-svn] r29832 - in pypy/dist/pypy/rpython: . test

arigo at codespeak.net arigo at codespeak.net
Sat Jul 8 16:04:52 CEST 2006


Author: arigo
Date: Sat Jul  8 16:04:50 2006
New Revision: 29832

Modified:
   pypy/dist/pypy/rpython/rbuiltin.py
   pypy/dist/pypy/rpython/test/test_rbuiltin.py
Log:
Supports passing a single char to extfuncs expecting a string.
This is a hack.  The whole exttablefunc should go away somehow.


Modified: pypy/dist/pypy/rpython/rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/rbuiltin.py	Sat Jul  8 16:04:50 2006
@@ -492,18 +492,28 @@
 
 from pypy.rpython import extfunctable
 
+def rnormalize(rtyper, r):
+    # this replaces char_repr with string_repr, because so far we have
+    # no external function expecting a char, but only external functions
+    # that happily crash if passed a char instead of a string
+    if r == rtyper.type_system.rstr.char_repr:
+        r = rtyper.type_system.rstr.string_repr
+    return r
+
 def make_rtype_extfunc(extfuncinfo):
     if extfuncinfo.ll_annotable:
         def rtype_extfunc(hop):
             ll_function = extfuncinfo.get_ll_function(hop.rtyper.type_system)
-            vars = hop.inputargs(*hop.args_r)
+            vars = hop.inputargs(*[rnormalize(hop.rtyper, r)
+                                   for r in hop.args_r])
             hop.exception_is_here()
             return hop.gendirectcall(ll_function, *vars)
     else:
         def rtype_extfunc(hop):
             ll_function = extfuncinfo.get_ll_function(hop.rtyper.type_system)
             resulttype = hop.r_result
-            vars = hop.inputargs(*hop.args_r)
+            vars = hop.inputargs(*[rnormalize(hop.rtyper, r)
+                                   for r in hop.args_r])
             hop.exception_is_here()
             return hop.llops.genexternalcall(ll_function.__name__, vars, resulttype=resulttype,
                                              _callable = ll_function)

Modified: pypy/dist/pypy/rpython/test/test_rbuiltin.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_rbuiltin.py	(original)
+++ pypy/dist/pypy/rpython/test/test_rbuiltin.py	Sat Jul  8 16:04:50 2006
@@ -167,6 +167,20 @@
         hello = open(tmpdir).read()
         assert hello == "hello world"
 
+    def test_os_write_single_char(self):
+        tmpdir = str(udir.udir.join("os_write_test_char"))
+        import os
+        def wr_open(fname):
+            fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+            os.write(fd, "x")
+            return fd
+        def f():
+            return wr_open(tmpdir)
+        res = self.interpret(f, [])
+        os.close(res)
+        hello = open(tmpdir).read()
+        assert hello == "x"
+
     def test_os_read(self):
         import os
         tmpfile = str(udir.udir.join("os_read_test"))



More information about the Pypy-commit mailing list