mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Changed to use 'U' argument to PyArg_ParseTuple, instead of manually checking for unicode objects.
This commit is contained in:
parent
a95207ab3b
commit
37f10386f1
6 changed files with 16 additions and 21 deletions
|
@ -525,10 +525,15 @@ class BuiltinTest(unittest.TestCase):
|
||||||
return str(self.x) + format_spec
|
return str(self.x) + format_spec
|
||||||
|
|
||||||
# class that returns a bad type from __format__
|
# class that returns a bad type from __format__
|
||||||
class H:
|
class B:
|
||||||
def __format__(self, format_spec):
|
def __format__(self, format_spec):
|
||||||
return 1.0
|
return 1.0
|
||||||
|
|
||||||
|
# class that is derived from string, used
|
||||||
|
# as a format spec
|
||||||
|
class C(str):
|
||||||
|
pass
|
||||||
|
|
||||||
self.assertEqual(format(3, ''), '3')
|
self.assertEqual(format(3, ''), '3')
|
||||||
self.assertEqual(format(A(3), 'spec'), '3spec')
|
self.assertEqual(format(A(3), 'spec'), '3spec')
|
||||||
|
|
||||||
|
@ -550,7 +555,10 @@ class BuiltinTest(unittest.TestCase):
|
||||||
empty_format_spec(None)
|
empty_format_spec(None)
|
||||||
|
|
||||||
# TypeError because self.__format__ returns the wrong type
|
# TypeError because self.__format__ returns the wrong type
|
||||||
self.assertRaises(TypeError, format, H(), "")
|
self.assertRaises(TypeError, format, B(), "")
|
||||||
|
|
||||||
|
# make sure we can take a subclass of str as a format spec
|
||||||
|
self.assertEqual(format(0, C('10')), ' 0')
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
import sys
|
import sys
|
||||||
|
|
|
@ -581,9 +581,6 @@ class UnicodeTest(
|
||||||
self.assertRaises(ValueError, format, "", "-")
|
self.assertRaises(ValueError, format, "", "-")
|
||||||
self.assertRaises(ValueError, "{0:=s}".format, '')
|
self.assertRaises(ValueError, "{0:=s}".format, '')
|
||||||
|
|
||||||
# check that __format__ returns a string
|
|
||||||
#self.assertRaises(TypeError, "{0}".format, H())
|
|
||||||
|
|
||||||
def test_formatting(self):
|
def test_formatting(self):
|
||||||
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
string_tests.MixinStrUnicodeUserStringTest.test_formatting(self)
|
||||||
# Testing Unicode formatting strings...
|
# Testing Unicode formatting strings...
|
||||||
|
|
|
@ -768,12 +768,8 @@ FORMAT_STRING(PyObject* value, PyObject* args)
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
InternalFormatSpec format;
|
InternalFormatSpec format;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
|
||||||
goto done;
|
goto done;
|
||||||
if (!STRINGLIB_CHECK(format_spec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check for the special case of zero length format spec, make
|
/* check for the special case of zero length format spec, make
|
||||||
it equivalent to str(value) */
|
it equivalent to str(value) */
|
||||||
|
@ -843,12 +839,8 @@ FORMAT_LONG(PyObject* value, PyObject* args)
|
||||||
PyObject *tmp = NULL;
|
PyObject *tmp = NULL;
|
||||||
InternalFormatSpec format;
|
InternalFormatSpec format;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
|
||||||
goto done;
|
goto done;
|
||||||
if (!STRINGLIB_CHECK(format_spec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check for the special case of zero length format spec, make
|
/* check for the special case of zero length format spec, make
|
||||||
it equivalent to str(value) */
|
it equivalent to str(value) */
|
||||||
|
@ -917,12 +909,8 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
|
||||||
PyObject *tmp = NULL;
|
PyObject *tmp = NULL;
|
||||||
InternalFormatSpec format;
|
InternalFormatSpec format;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
|
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
|
||||||
goto done;
|
goto done;
|
||||||
if (!STRINGLIB_CHECK(format_spec)) {
|
|
||||||
PyErr_SetString(PyExc_TypeError, STRINGLIB_TYPE_NAME " object required");
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check for the special case of zero length format spec, make
|
/* check for the special case of zero length format spec, make
|
||||||
it equivalent to str(value) */
|
it equivalent to str(value) */
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#define STRINGLIB_CHAR char
|
#define STRINGLIB_CHAR char
|
||||||
#define STRINGLIB_TYPE_NAME "string"
|
#define STRINGLIB_TYPE_NAME "string"
|
||||||
|
#define STRINGLIB_PARSE_CODE "S"
|
||||||
#define STRINGLIB_EMPTY string_empty
|
#define STRINGLIB_EMPTY string_empty
|
||||||
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
|
#define STRINGLIB_ISDECIMAL(x) ((x >= '0') && (x <= '9'))
|
||||||
#define STRINGLIB_TODECIMAL(x) (STRINGLIB_ISDECIMAL(x) ? (x - '0') : -1)
|
#define STRINGLIB_TODECIMAL(x) (STRINGLIB_ISDECIMAL(x) ? (x - '0') : -1)
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#define STRINGLIB_CHAR Py_UNICODE
|
#define STRINGLIB_CHAR Py_UNICODE
|
||||||
#define STRINGLIB_TYPE_NAME "unicode"
|
#define STRINGLIB_TYPE_NAME "unicode"
|
||||||
|
#define STRINGLIB_PARSE_CODE "U"
|
||||||
#define STRINGLIB_EMPTY unicode_empty
|
#define STRINGLIB_EMPTY unicode_empty
|
||||||
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
|
#define STRINGLIB_ISDECIMAL Py_UNICODE_ISDECIMAL
|
||||||
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
|
#define STRINGLIB_TODECIMAL Py_UNICODE_TODECIMAL
|
||||||
|
|
|
@ -293,7 +293,7 @@ builtin_format(PyObject *self, PyObject *args)
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "O|O:format", &value, &spec))
|
if (!PyArg_ParseTuple(args, "O|U:format", &value, &spec))
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* initialize the default value */
|
/* initialize the default value */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue