Author: Antonio Cuni <[email protected]>
Branch: rpython-unicode-formatting
Changeset: r56135:02b40b853dc6
Date: 2012-07-18 14:09 +0200
http://bitbucket.org/pypy/pypy/changeset/02b40b853dc6/
Log: correctly support u'%s' % my_unicode_string
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
@@ -170,6 +170,13 @@
return result
@jit.elidable
+ def ll_unicode(self, s):
+ if s:
+ return s
+ else:
+ return self.convert_const(u'None')
+
+ @jit.elidable
def ll_encode_latin1(self, s):
length = len(s.chars)
result = mallocstr(length)
@@ -985,8 +992,13 @@
vitem, r_arg = argsiter.next()
if not hasattr(r_arg, 'll_str'):
raise TyperError("ll_str unsupported for: %r" % r_arg)
- if code == 's' or (code == 'r' and isinstance(r_arg,
InstanceRepr)):
- # XXX: if it's unicode we don't want to call ll_str
+ if code == 's':
+ if is_unicode:
+ # only UniCharRepr and UnicodeRepr has it so far
+ vchunk = hop.gendirectcall(r_arg.ll_unicode, vitem)
+ else:
+ vchunk = hop.gendirectcall(r_arg.ll_str, vitem)
+ elif code == 'r' and isinstance(r_arg, InstanceRepr):
vchunk = hop.gendirectcall(r_arg.ll_str, vitem)
elif code == 'd':
assert isinstance(r_arg, IntegerRepr)
diff --git a/pypy/rpython/rstr.py b/pypy/rpython/rstr.py
--- a/pypy/rpython/rstr.py
+++ b/pypy/rpython/rstr.py
@@ -483,6 +483,8 @@
# xxx suboptimal, maybe
return str(unicode(ch))
+ def ll_unicode(self, ch):
+ return unicode(ch)
class __extend__(AbstractCharRepr,
AbstractUniCharRepr):
diff --git a/pypy/rpython/test/test_runicode.py
b/pypy/rpython/test/test_runicode.py
--- a/pypy/rpython/test/test_runicode.py
+++ b/pypy/rpython/test/test_runicode.py
@@ -1,3 +1,4 @@
+# -*- encoding: utf-8 -*-
from pypy.rpython.lltypesystem.lltype import malloc
from pypy.rpython.lltypesystem.rstr import LLHelpers, UNICODE
@@ -194,6 +195,15 @@
assert self.interpret(fn, [u'(']) == False
assert self.interpret(fn, [u'\u1058']) == False
assert self.interpret(fn, [u'X']) == True
+
+ def test_strformat_unicode_arg(self):
+ const = self.const
+ def percentS(s):
+ return const("before %s after") % (s,)
+
+ res = self.interpret(percentS, [const(u'à')])
+ assert self.ll_to_string(res) == const(u'before à after')
+
def unsupported(self):
py.test.skip("not supported")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit