Kaydet (Commit) fa767efe authored tarafından Eric Smith's avatar Eric Smith

Partially revert r60376: restore ability for ints to be automatically converted…

Partially revert r60376: restore ability for ints to be automatically converted to floats, if a float type specifier is given to an int.  PEP 3101 should be clarified on this point.Also, remove unused local variables left over from r60376.
üst fe82e774
...@@ -530,17 +530,23 @@ class LongTest(unittest.TestCase): ...@@ -530,17 +530,23 @@ class LongTest(unittest.TestCase):
self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed
self.assertRaises(ValueError, format, 3, "+c") # sign not allowed self.assertRaises(ValueError, format, 3, "+c") # sign not allowed
# with 'c' # with 'c'
# other format specifiers shouldn't work on ints,
# in particular float and string specifiers # ensure that only int and float type specifiers work
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
[chr(x) for x in range(ord('A'), ord('Z')+1)]): [chr(x) for x in range(ord('A'), ord('Z')+1)]):
if not format_spec in 'bcdoxX': if not format_spec in 'bcdoxXeEfFgGn%':
self.assertRaises(ValueError, format, 0, format_spec) self.assertRaises(ValueError, format, 0, format_spec)
self.assertRaises(ValueError, format, 1, format_spec) self.assertRaises(ValueError, format, 1, format_spec)
self.assertRaises(ValueError, format, -1, format_spec) self.assertRaises(ValueError, format, -1, format_spec)
self.assertRaises(ValueError, format, 2**100, format_spec) self.assertRaises(ValueError, format, 2**100, format_spec)
self.assertRaises(ValueError, format, -(2**100), format_spec) self.assertRaises(ValueError, format, -(2**100), format_spec)
# ensure that float type specifiers work; format converts
# the int to a float
for format_spec in 'eEfFgGn%':
for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
self.assertEqual(format(value, format_spec),
format(float(value), format_spec))
def test_nan_inf(self): def test_nan_inf(self):
self.assertRaises(OverflowError, int, float('inf')) self.assertRaises(OverflowError, int, float('inf'))
......
...@@ -764,7 +764,6 @@ PyObject * ...@@ -764,7 +764,6 @@ PyObject *
FORMAT_STRING(PyObject* value, PyObject* args) FORMAT_STRING(PyObject* value, PyObject* args)
{ {
PyObject *format_spec; PyObject *format_spec;
PyObject *tmp = NULL;
PyObject *result = NULL; PyObject *result = NULL;
InternalFormatSpec format; InternalFormatSpec format;
...@@ -796,7 +795,6 @@ FORMAT_STRING(PyObject* value, PyObject* args) ...@@ -796,7 +795,6 @@ FORMAT_STRING(PyObject* value, PyObject* args)
} }
done: done:
Py_XDECREF(tmp);
return result; return result;
} }
...@@ -834,6 +832,21 @@ FORMAT_LONG(PyObject* value, PyObject* args) ...@@ -834,6 +832,21 @@ FORMAT_LONG(PyObject* value, PyObject* args)
result = format_long_internal(value, &format); result = format_long_internal(value, &format);
break; break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'n':
case '%':
/* convert to float */
tmp = PyNumber_Float(value);
if (tmp == NULL)
goto done;
result = format_float_internal(value, &format);
break;
default: default:
/* unknown */ /* unknown */
PyErr_Format(PyExc_ValueError, "Unknown conversion type %c", PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
...@@ -851,7 +864,6 @@ FORMAT_FLOAT(PyObject *value, PyObject *args) ...@@ -851,7 +864,6 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
{ {
PyObject *format_spec; PyObject *format_spec;
PyObject *result = NULL; PyObject *result = NULL;
PyObject *tmp = NULL;
InternalFormatSpec format; InternalFormatSpec format;
if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec)) if (!PyArg_ParseTuple(args, STRINGLIB_PARSE_CODE ":__format__", &format_spec))
...@@ -890,6 +902,5 @@ FORMAT_FLOAT(PyObject *value, PyObject *args) ...@@ -890,6 +902,5 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
} }
done: done:
Py_XDECREF(tmp);
return result; return result;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment