summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-04-10 13:52:48 (GMT)
committerGuido van Rossum <guido@python.org>2000-04-10 13:52:48 (GMT)
commit9706486b9fb8d25762095c855c3c2a976dcbf1d3 (patch)
tree30c40a1a3db5c20fe03014760f7e63e03bcd043b
parent45ad3c48977110e1097efcf6332407deaf575c2e (diff)
downloadcpython-9706486b9fb8d25762095c855c3c2a976dcbf1d3.zip
cpython-9706486b9fb8d25762095c855c3c2a976dcbf1d3.tar.gz
cpython-9706486b9fb8d25762095c855c3c2a976dcbf1d3.tar.bz2
Marc-Andre Lemburg:
* '...%s...' % u"abc" now coerces to Unicode just like string methods. Care is taken not to reevaluate already formatted arguments -- only the first Unicode object appearing in the argument mapping is looked up twice. Added test cases for this to test_unicode.py.
-rw-r--r--Lib/test/test_unicode.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 5c0a063..4f4196c 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -255,6 +255,15 @@ assert u"%c" % (36,) == u'$'
assert u"%r, %r" % (u"abc", "abc") == u"u'abc', 'abc'"
assert u"%(x)s, %(y)s" % {'x':u"abc", 'y':"def"} == u'abc, def'
assert u"%(x)s, %(ä)s" % {'x':u"abc", u'ä'.encode('utf-8'):"def"} == u'abc, def'
+# formatting jobs delegated from the string implementation:
+assert '...%(foo)s...' % {'foo':u"abc"} == u'...abc...'
+assert '...%(foo)s...' % {'foo':"abc"} == '...abc...'
+assert '...%(foo)s...' % {u'foo':"abc"} == '...abc...'
+assert '...%(foo)s...' % {u'foo':u"abc"} == u'...abc...'
+assert '...%(foo)s...' % {u'foo':u"abc",'def':123} == u'...abc...'
+assert '...%(foo)s...' % {u'foo':u"abc",u'def':123} == u'...abc...'
+assert '...%s...%s...%s...%s...' % (1,2,3,u"abc") == u'...1...2...3...abc...'
+assert '...%s...' % u"abc" == u'...abc...'
print 'done.'
# Test builtin codecs
@@ -265,6 +274,26 @@ assert unicode('hello','utf-8') == u'hello'
assert unicode('hello','utf8') == u'hello'
assert unicode('hello','latin-1') == u'hello'
+try:
+ u'Andr\202 x'.encode('ascii')
+ u'Andr\202 x'.encode('ascii','strict')
+except ValueError:
+ pass
+else:
+ raise AssertionError, "u'Andr\202'.encode('ascii') failed to raise an exception"
+assert u'Andr\202 x'.encode('ascii','ignore') == "Andr x"
+assert u'Andr\202 x'.encode('ascii','replace') == "Andr? x"
+
+try:
+ unicode('Andr\202 x','ascii')
+ unicode('Andr\202 x','ascii','strict')
+except ValueError:
+ pass
+else:
+ raise AssertionError, "unicode('Andr\202') failed to raise an exception"
+assert unicode('Andr\202 x','ascii','ignore') == u"Andr x"
+assert unicode('Andr\202 x','ascii','replace') == u'Andr\uFFFD x'
+
assert u'hello'.encode('ascii') == 'hello'
assert u'hello'.encode('utf-8') == 'hello'
assert u'hello'.encode('utf8') == 'hello'