Hi,
the following code compiles into something that segfaults. Attached is
a patch trying to fix that.

        def nstr(i):
            if i==0: return None
            return str(i)
        def fn(i):
            return (str(nstr(0)), str(nstr(i)))


-- 
Håkan Ardö
Index: pypy/rpython/rstr.py
===================================================================
--- pypy/rpython/rstr.py	(revision 60718)
+++ pypy/rpython/rstr.py	(working copy)
@@ -9,6 +9,7 @@
 from pypy.rpython import rint
 from pypy.rpython.lltypesystem.lltype import Signed, Bool, Void, UniChar,\
      cast_primitive
+from pypy.rpython.annlowlevel import llstr
 
 class AbstractStringRepr(Repr):
     pass
@@ -278,6 +279,16 @@
         hop.exception_is_here()
         return hop.gendirectcall(self.ll.ll_float, v_str)
 
+    def rtype_str(self, hop):
+        [v_self] = hop.inputargs(self)
+        if hop.args_s[0].can_be_none():
+            return hop.gendirectcall(self.ll_str_or_none, v_self)
+        return hop.gendirectcall(self.ll_str, v_self)
+
+    def ll_str_or_none(self,s):
+        if not s: return llstr('None')
+        return s
+
     def ll_str(self, s):
         return s
 
Index: pypy/rpython/test/test_rstr.py
===================================================================
--- pypy/rpython/test/test_rstr.py	(revision 60718)
+++ pypy/rpython/test/test_rstr.py	(working copy)
@@ -820,6 +820,16 @@
         res = self.interpret(f, [1])
         assert self.ll_to_string(res) == "hello"
 
+    def test_none_or_string(self):
+        def nstr(i):
+            if i==0: return None
+            return str(i)
+        def fn(i):
+            return (str(nstr(0)), str(nstr(i)))
+        from pypy.translator.c.test.test_genc import compile
+        f = compile(fn,[int])
+        assert f(7)==('None', '7')
+
 def FIXME_test_str_to_pystringobj():
     def f(n):
         if n >= 0:
_______________________________________________
[email protected]
http://codespeak.net/mailman/listinfo/pypy-dev

Reply via email to