[pypy-svn] pypy default: (fijal, arigo)

arigo commits-noreply at bitbucket.org
Thu Jan 20 13:52:10 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r41014:c0b0d50425f0
Date: 2011-01-20 13:51 +0100
http://bitbucket.org/pypy/pypy/changeset/c0b0d50425f0/

Log:	(fijal, arigo)

	Support in RPython of str(x) where x is either a string or None. For
	None, returns the string None.

diff --git a/pypy/rpython/lltypesystem/rstr.py b/pypy/rpython/lltypesystem/rstr.py
--- a/pypy/rpython/lltypesystem/rstr.py
+++ b/pypy/rpython/lltypesystem/rstr.py
@@ -145,6 +145,8 @@
     def ll_str(self, s):
         # XXX crazy that this is here, but I don't want to break
         #     rmodel logic
+        if not s:
+            return self.ll.ll_constant('None')
         lgt = len(s.chars)
         result = mallocstr(lgt)
         for i in range(lgt):

diff --git a/pypy/rpython/rstr.py b/pypy/rpython/rstr.py
--- a/pypy/rpython/rstr.py
+++ b/pypy/rpython/rstr.py
@@ -284,7 +284,10 @@
         return hop.gendirectcall(self.ll.ll_float, v_str)
 
     def ll_str(self, s):
-        return s
+        if s:
+            return s
+        else:
+            return self.ll.ll_constant('None')
 
 class __extend__(AbstractUnicodeRepr):
     def rtype_method_encode(self, hop):

diff --git a/pypy/rpython/test/test_rstr.py b/pypy/rpython/test/test_rstr.py
--- a/pypy/rpython/test/test_rstr.py
+++ b/pypy/rpython/test/test_rstr.py
@@ -888,6 +888,22 @@
             return c[i].encode("latin-1")
         assert self.ll_to_string(self.interpret(f, [0])) == "a"
 
+    def test_str_none(self):
+        const = self.const
+        def g():
+            pass
+        def f(i):
+            if i > 5:
+                u = None
+            else:
+                u = const('xxx')
+            g()    # hack for flow object space
+            return str(u)
+        assert self.ll_to_string(self.interpret(f, [3])) == 'xxx'
+        got = self.interpret(f, [7])
+        assert self.ll_to_string(got) == 'None'
+
+
 def FIXME_test_str_to_pystringobj():
     def f(n):
         if n >= 0:

diff --git a/pypy/rpython/ootypesystem/rstr.py b/pypy/rpython/ootypesystem/rstr.py
--- a/pypy/rpython/ootypesystem/rstr.py
+++ b/pypy/rpython/ootypesystem/rstr.py
@@ -66,6 +66,8 @@
         return ootype.make_unicode(value)
 
     def ll_str(self, value):
+        if not value:
+            return self.ll.ll_constant('None')
         sb = ootype.new(ootype.StringBuilder)
         lgt = value.ll_strlen()
         sb.ll_allocate(lgt)


More information about the Pypy-commit mailing list