From 43440a621e022fbeaff24f84dcf4d5458601fedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20D=C3=B6rwald?= Date: Mon, 31 Mar 2003 18:07:50 +0000 Subject: Fix PyString_Format() so that '%c' % u'a' returns u'a' instead of raising a TypeError. (From SF patch #710127) Add tests to verify this is fixed. Add various tests for '%c' % int. --- Lib/test/string_tests.py | 1 + Lib/test/test_str.py | 4 ++++ Lib/test/test_unicode.py | 3 +++ Misc/NEWS | 3 +++ Objects/stringobject.c | 7 +++++++ 5 files changed, 18 insertions(+) diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 4bcd121..dcf961d 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -543,6 +543,7 @@ class MixinStrUnicodeUserStringTest: self.checkequal('"', "%c", '__mod__', 34) self.checkequal('$', "%c", '__mod__', 36) self.checkequal('10', "%d", '__mod__', 10) + self.checkequal('\x7f', "%c", '__mod__', 0x7f) for ordinal in (-100, 0x200000): # unicode raises ValueError, str raises OverflowError diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 1b26106..b9f4845 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -14,6 +14,10 @@ class StrTest( def fixtype(self, obj): return obj + def test_formatting(self): + string_tests.MixinStrUnicodeUserStringTest.test_formatting(self) + self.assertRaises(OverflowError, '%c'.__mod__, 0x1234) + def test_main(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(StrTest)) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 43e2eba..69f308e 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -358,6 +358,8 @@ class UnicodeTest( self.assertEqual(u"%r, %r" % (u"abc", "abc"), u"u'abc', 'abc'") self.assertEqual(u"%(x)s, %(y)s" % {'x':u"abc", 'y':"def"}, u'abc, def') self.assertEqual(u"%(x)s, %(\xfc)s" % {'x':u"abc", u'\xfc':"def"}, u'abc, def') + self.assertEqual(u'%c' % 0x1234, u'\u1234') + self.assertRaises(ValueError, u'%c'.__mod__, sys.maxunicode+1) # formatting jobs delegated from the string implementation: self.assertEqual('...%(foo)s...' % {'foo':u"abc"}, u'...abc...') @@ -375,6 +377,7 @@ class UnicodeTest( self.assertEqual('%*.*s' % (5,3,u'abc',), u' abc') self.assertEqual('%i %*.*s' % (10, 5,3,u'abc',), u'10 abc') self.assertEqual('%i%s %*.*s' % (10, 3, 5, 3, u'abc',), u'103 abc') + self.assertEqual('%c' % u'a', u'a') self.assertRaises(ValueError, u"%c".__mod__, (sys.maxunicode+1,)) diff --git a/Misc/NEWS b/Misc/NEWS index 41cb887..4a0e330 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,9 @@ Core and builtins unicode system with multiple active interpreters, or successive interpreter executions, would fail. +- "%c" % u"a" now returns a unicode string instead of raising a + TypeError. See SF patch #710127. + Extension modules ----------------- diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 9598ffb..2613c62 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3933,6 +3933,13 @@ PyString_Format(PyObject *format, PyObject *args) fill = '0'; break; case 'c': +#ifdef Py_USING_UNICODE + if (PyUnicode_Check(v)) { + fmt = fmt_start; + argidx = argidx_start; + goto unicode; + } +#endif pbuf = formatbuf; len = formatchar(pbuf, sizeof(formatbuf), v); if (len < 0) -- cgit v0.12