Changed to use 'U' argument to PyArg_ParseTuple, instead of manually … · wrongnull/cpython@37f1038 · GitHub
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

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

0 commit comments

Comments
 (0)