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

Restrict format presentation types to those specified in the 'Standard Format…

Restrict format presentation types to those specified in the 'Standard Format Specifiers' section of PEP 3101.
üst 412dc9c8
...@@ -133,13 +133,10 @@ class FormatTestCase(unittest.TestCase): ...@@ -133,13 +133,10 @@ class FormatTestCase(unittest.TestCase):
self.assertEqual(format(0.01, ''), '0.01') self.assertEqual(format(0.01, ''), '0.01')
self.assertEqual(format(0.01, 'g'), '0.01') self.assertEqual(format(0.01, 'g'), '0.01')
self.assertEqual(format(0, 'f'), '0.000000')
self.assertEqual(format(1.0, 'f'), '1.000000') self.assertEqual(format(1.0, 'f'), '1.000000')
self.assertEqual(format(1, 'f'), '1.000000')
self.assertEqual(format(-1.0, 'f'), '-1.000000') self.assertEqual(format(-1.0, 'f'), '-1.000000')
self.assertEqual(format(-1, 'f'), '-1.000000')
self.assertEqual(format( 1.0, ' f'), ' 1.000000') self.assertEqual(format( 1.0, ' f'), ' 1.000000')
self.assertEqual(format(-1.0, ' f'), '-1.000000') self.assertEqual(format(-1.0, ' f'), '-1.000000')
...@@ -152,6 +149,18 @@ class FormatTestCase(unittest.TestCase): ...@@ -152,6 +149,18 @@ class FormatTestCase(unittest.TestCase):
# conversion to string should fail # conversion to string should fail
self.assertRaises(ValueError, format, 3.0, "s") self.assertRaises(ValueError, format, 3.0, "s")
# other format specifiers shouldn't work on floats,
# in particular int specifiers
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)]):
if not format_spec in 'eEfFgGn%':
self.assertRaises(ValueError, format, 0.0, format_spec)
self.assertRaises(ValueError, format, 1.0, format_spec)
self.assertRaises(ValueError, format, -1.0, format_spec)
self.assertRaises(ValueError, format, 1e100, format_spec)
self.assertRaises(ValueError, format, -1e100, format_spec)
self.assertRaises(ValueError, format, 1e-100, format_spec)
self.assertRaises(ValueError, format, -1e-100, format_spec)
class ReprTestCase(unittest.TestCase): class ReprTestCase(unittest.TestCase):
def test_repr(self): def test_repr(self):
......
...@@ -526,16 +526,21 @@ class LongTest(unittest.TestCase): ...@@ -526,16 +526,21 @@ class LongTest(unittest.TestCase):
self.assertEqual(format(1234, "+b"), "+10011010010") self.assertEqual(format(1234, "+b"), "+10011010010")
self.assertEqual(format(-1234, "+b"), "-10011010010") self.assertEqual(format(-1234, "+b"), "-10011010010")
# conversion to float
self.assertEqual(format(0, 'f'), '0.000000')
# make sure these are errors # make sure these are errors
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'
self.assertRaises(ValueError, format, 3, "R") # bogus format type # other format specifiers shouldn't work on ints,
# conversion to string should fail # in particular float and string specifiers
self.assertRaises(ValueError, format, 3, "s") 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)]):
if not format_spec in 'bcdoxX':
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, 2**100, format_spec)
self.assertRaises(ValueError, format, -(2**100), format_spec)
def test_nan_inf(self): def test_nan_inf(self):
self.assertRaises(OverflowError, int, float('inf')) self.assertRaises(OverflowError, int, float('inf'))
......
...@@ -788,37 +788,6 @@ FORMAT_STRING(PyObject* value, PyObject* args) ...@@ -788,37 +788,6 @@ FORMAT_STRING(PyObject* value, PyObject* args)
/* no type conversion needed, already a string. do the formatting */ /* no type conversion needed, already a string. do the formatting */
result = format_string_internal(value, &format); result = format_string_internal(value, &format);
break; break;
#if 0
case 'b':
case 'c':
case 'd':
case 'o':
case 'x':
case 'X':
/* convert to integer */
/* XXX: make a stringlib function to do this when backporting,
since FromUnicode differs from FromString */
tmp = PyLong_FromUnicode(STRINGLIB_STR(value), STRINGLIB_LEN(value), 0);
if (tmp == NULL)
goto done;
result = format_long_internal(tmp, &format);
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'n':
case '%':
/* convert to float */
tmp = PyFloat_FromString(value);
if (tmp == NULL)
goto done;
result = format_float_internal(tmp, &format);
break;
#endif
default: default:
/* unknown */ /* unknown */
PyErr_Format(PyExc_ValueError, "Unknown conversion type %c", PyErr_Format(PyExc_ValueError, "Unknown conversion type %c",
...@@ -855,15 +824,6 @@ FORMAT_LONG(PyObject* value, PyObject* args) ...@@ -855,15 +824,6 @@ FORMAT_LONG(PyObject* value, PyObject* args)
/* type conversion? */ /* type conversion? */
switch (format.type) { switch (format.type) {
#if 0
case 's':
/* convert to string/unicode */
tmp = STRINGLIB_TOSTR(value);
if (tmp == NULL)
goto done;
result = format_string_internal(tmp, &format);
break;
#endif
case 'b': case 'b':
case 'c': case 'c':
case 'd': case 'd':
...@@ -874,21 +834,6 @@ FORMAT_LONG(PyObject* value, PyObject* args) ...@@ -874,21 +834,6 @@ 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",
...@@ -925,28 +870,6 @@ FORMAT_FLOAT(PyObject *value, PyObject *args) ...@@ -925,28 +870,6 @@ FORMAT_FLOAT(PyObject *value, PyObject *args)
/* type conversion? */ /* type conversion? */
switch (format.type) { switch (format.type) {
#if 0
case 's':
/* convert to string/unicode */
tmp = STRINGLIB_TOSTR(value);
if (tmp == NULL)
goto done;
result = format_string_internal(tmp, &format);
break;
#endif
case 'b':
case 'c':
case 'd':
case 'o':
case 'x':
case 'X':
/* convert to integer */
tmp = PyNumber_Long(value);
if (tmp == NULL)
goto done;
result = format_long_internal(tmp, &format);
break;
case 'e': case 'e':
case 'E': case 'E':
case 'f': case 'f':
......
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