Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc6e8fe

Browse files
committed
Simplifed argument parsing in object.__format__, added test case.
1 parent 0041223 commit fc6e8fe

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,10 @@ def empty_format_spec(value):
558558
# TypeError because self.__format__ returns the wrong type
559559
self.assertRaises(TypeError, format, B(), "")
560560

561+
# TypeError because format_spec is not unicode
562+
self.assertRaises(TypeError, format, object(), 4)
563+
self.assertRaises(TypeError, format, object(), object())
564+
561565
# make sure we can take a subclass of str as a format spec
562566
self.assertEqual(format(0, C('10')), ' 0')
563567

Objects/typeobject.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,12 +2950,8 @@ object_format(PyObject *self, PyObject *args)
29502950
PyObject *result = NULL;
29512951
PyObject *format_meth = NULL;
29522952

2953-
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
2953+
if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
29542954
return NULL;
2955-
if (!PyUnicode_Check(format_spec)) {
2956-
PyErr_SetString(PyExc_TypeError, "Unicode object required");
2957-
return NULL;
2958-
}
29592955

29602956
self_as_str = PyObject_Str(self);
29612957
if (self_as_str != NULL) {

0 commit comments

Comments
 (0)