[Python-checkins] r88704 - in python/branches/py3k: Lib/test/test_unicode.py Misc/NEWS Objects/unicodeobject.c

victor.stinner python-checkins at python.org
Wed Mar 2 01:04:25 CET 2011


Author: victor.stinner
Date: Wed Mar  2 01:04:25 2011
New Revision: 88704

Log:
Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats

Modified:
   python/branches/py3k/Lib/test/test_unicode.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/unicodeobject.c

Modified: python/branches/py3k/Lib/test/test_unicode.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unicode.py	(original)
+++ python/branches/py3k/Lib/test/test_unicode.py	Wed Mar  2 01:04:25 2011
@@ -1430,7 +1430,9 @@
     # Test PyUnicode_FromFormat()
     def test_from_format(self):
         support.import_module('ctypes')
-        from ctypes import pythonapi, py_object, c_int
+        from ctypes import (pythonapi, py_object,
+            c_int, c_long, c_longlong, c_ssize_t,
+            c_uint, c_ulong, c_ulonglong, c_size_t)
         if sys.maxunicode == 65535:
             name = "PyUnicodeUCS2_FromFormat"
         else:
@@ -1466,21 +1468,29 @@
         self.assertEqual(PyUnicode_FromFormat(b'[%%]'), '[%]')
         self.assertEqual(PyUnicode_FromFormat(b'%%%s', b'abc'), '%abc')
 
-        # test "%i"
+        # test integer formats (%i, %d, %u)
         self.assertEqual(PyUnicode_FromFormat(b'%03i', c_int(10)), '010')
         self.assertEqual(PyUnicode_FromFormat(b'%0.4i', c_int(10)), '0010')
+        self.assertEqual(PyUnicode_FromFormat(b'%i', c_int(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%li', c_long(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%lli', c_longlong(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%zi', c_ssize_t(-123)), '-123')
+
+        self.assertEqual(PyUnicode_FromFormat(b'%d', c_int(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%ld', c_long(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%lld', c_longlong(-123)), '-123')
+        self.assertEqual(PyUnicode_FromFormat(b'%zd', c_ssize_t(-123)), '-123')
+
+        self.assertEqual(PyUnicode_FromFormat(b'%u', c_uint(123)), '123')
+        self.assertEqual(PyUnicode_FromFormat(b'%lu', c_ulong(123)), '123')
+        self.assertEqual(PyUnicode_FromFormat(b'%llu', c_ulonglong(123)), '123')
+        self.assertEqual(PyUnicode_FromFormat(b'%zu', c_size_t(123)), '123')
 
-        # not supported: copy the raw format string. these tests are just here
-        # to check for crashs and should not be considered as specifications
-        self.assertEqual(PyUnicode_FromFormat(b'%1%s', b'abc'), '%s')
-        self.assertEqual(PyUnicode_FromFormat(b'%1abc'), '%1abc')
-        self.assertEqual(PyUnicode_FromFormat(b'%+i', c_int(10)), '%+i')
-        self.assertEqual(PyUnicode_FromFormat(b'%.%s', b'abc'), '%.%s')
-
-        # other tests
+        # test %A
         text = PyUnicode_FromFormat(b'%%A:%A', 'abc\xe9\uabcd\U0010ffff')
         self.assertEqual(text, r"%A:'abc\xe9\uabcd\U0010ffff'")
 
+        # test %V
         text = PyUnicode_FromFormat(b'repr=%V', 'abc', b'xyz')
         self.assertEqual(text, 'repr=abc')
 
@@ -1494,6 +1504,13 @@
         text = PyUnicode_FromFormat(b'repr=%V', None, b'abc\xff')
         self.assertEqual(text, 'repr=abc\ufffd')
 
+        # not supported: copy the raw format string. these tests are just here
+        # to check for crashs and should not be considered as specifications
+        self.assertEqual(PyUnicode_FromFormat(b'%1%s', b'abc'), '%s')
+        self.assertEqual(PyUnicode_FromFormat(b'%1abc'), '%1abc')
+        self.assertEqual(PyUnicode_FromFormat(b'%+i', c_int(10)), '%+i')
+        self.assertEqual(PyUnicode_FromFormat(b'%.%s', b'abc'), '%.%s')
+
     # Test PyUnicode_AsWideChar()
     def test_aswidechar(self):
         from _testcapi import unicode_aswidechar

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Mar  2 01:04:25 2011
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #10831: PyUnicode_FromFormat() supports %li, %lli and %zi formats.
+
 - Issue #10829: Refactor PyUnicode_FromFormat(), use the same function to parse
   the format string in the 3 steps, fix crashs on invalid format strings.
 

Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Wed Mar  2 01:04:25 2011
@@ -753,20 +753,20 @@
     size_tflag = 0;
 
     if (*f == 'l') {
-        if (f[1] == 'd' || f[1] == 'u') {
+        if (f[1] == 'd' || f[1] == 'u' || f[1] == 'i') {
             longflag = 1;
             ++f;
         }
 #ifdef HAVE_LONG_LONG
         else if (f[1] == 'l' &&
-                 (f[2] == 'd' || f[2] == 'u')) {
+                 (f[2] == 'd' || f[2] == 'u' || f[2] == 'i')) {
             longlongflag = 1;
             f += 2;
         }
 #endif
     }
     /* handle the size_t flag. */
-    else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) {
+    else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u' || f[1] == 'i')) {
         size_tflag = 1;
         ++f;
     }
@@ -1044,9 +1044,10 @@
                 *s++ = ordinal;
                 break;
             }
+            case 'i':
             case 'd':
                 makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
-                        width, precision, 'd');
+                        width, precision, *f);
                 if (longflag)
                     sprintf(realbuffer, fmt, va_arg(vargs, long));
 #ifdef HAVE_LONG_LONG
@@ -1075,11 +1076,6 @@
                     sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));
                 appendstring(realbuffer);
                 break;
-            case 'i':
-                makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'i');
-                sprintf(realbuffer, fmt, va_arg(vargs, int));
-                appendstring(realbuffer);
-                break;
             case 'x':
                 makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
                 sprintf(realbuffer, fmt, va_arg(vargs, int));


More information about the Python-checkins mailing list