Skip to content

Commit 37f1038

Browse files
committed
Changed to use 'U' argument to PyArg_ParseTuple, instead of manually checking for unicode objects.
1 parent a95207a commit 37f1038

6 files changed

Lines changed: 16 additions & 21 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,15 @@ def __format__(self, format_spec):
525525
return str(self.x) + format_spec
526526

527527
# class that returns a bad type from __format__
528-
class H:
528+
class B:
529529
def __format__(self, format_spec):
530530
return 1.0
531531

532+
# class that is derived from string, used
533+
# as a format spec
534+
class C(str):
535+
pass
536+
532537
self.assertEqual(format(3, ''), '3')
533538
self.assertEqual(format(A(3), 'spec'), '3spec')
534539

@@ -550,7 +555,10 @@ def empty_format_spec(value):
550555
empty_format_spec(None)
551556

552557
# TypeError because self.__format__ returns the wrong type
553-
self.assertRaises(TypeError, format, H(), "")
558+
self.assertRaises(TypeError, format, B(), "")
559+
560+
# make sure we can take a subclass of str as a format spec
561+
self.assertEqual(format(0, C('10')), ' 0')
554562

555563
def test_getattr(self):
556564
import sys

Lib/test/test_unicode.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,6 @@ def __format__(self, format_spec):
581581
self.assertRaises(ValueError, format, "", "-")
582582
self.assertRaises(ValueError, "{0:=s}".format, '')
583583

584-
# check that __format__ returns a string
585-
#self.assertRaises(TypeError, "{0}".format, H())
586-
587584
def test_formatting(self):
588585
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
589586
# Testing Unicode formatting strings...

Objects/stringlib/formatter.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,8 @@ FORMAT_STRING(PyObject* value, PyObject* args)
768768
PyObject *result = NULL;
769769
InternalFormatSpec format;
770770

771-
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
771+
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
772772
goto done;
773-
if (!STRINGLIB_CHECK(format_spec)) {
774-
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
775-
goto done;
776-
}
777773

778774
/* check for the special case of zero length format spec, make
779775
it equivalent to str(value) */
@@ -843,12 +839,8 @@ FORMAT_LONG(PyObject* value, PyObject* args)
843839
PyObject *tmp = NULL;
844840
InternalFormatSpec format;
845841

846-
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
847-
goto done;
848-
if (!STRINGLIB_CHECK(format_spec)) {
849-
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
842+
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
850843
goto done;
851-
}
852844

853845
/* check for the special case of zero length format spec, make
854846
it equivalent to str(value) */
@@ -917,12 +909,8 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
917909
PyObject *tmp = NULL;
918910
InternalFormatSpec format;
919911

920-
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
921-
goto done;
922-
if (!STRINGLIB_CHECK(format_spec)) {
923-
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
912+
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
924913
goto done;
925-
}
926914

927915
/* check for the special case of zero length format spec, make
928916
it equivalent to str(value) */

Objects/stringlib/stringdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define STRINGLIB_CHAR char
1010
#define STRINGLIB_TYPE_NAME "string"
11+
#define STRINGLIB_PARSE_CODE "S"
1112
#define STRINGLIB_EMPTY string_empty
1213
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
1314
#define STRINGLIB_TODECIMAL(x) (STRINGLIB_ISDECIMAL(x) ? (x - '0') : -1)

Objects/stringlib/unicodedefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define STRINGLIB_CHAR Py_UNICODE
1010
#define STRINGLIB_TYPE_NAME "unicode"
11+
#define STRINGLIB_PARSE_CODE "U"
1112
#define STRINGLIB_EMPTY unicode_empty
1213
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
1314
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ builtin_format(PyObject *self, PyObject *args)
293293
goto done;
294294
}
295295

296-
if (!PyArg_ParseTuple(args, "O|O:format", &value, &spec))
296+
if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec))
297297
goto done;
298298

299299
/* initialize the default value */

0 commit comments

Comments
 (0)