mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Fix the problem of not raising a TypeError exception when doing:
'%g' % '1'
'%d' % '1'
Add a test for these conditions
Fix the test so that if not exception is raise, this is a failure
This commit is contained in:
parent
673c0a2247
commit
88fe4ff5a9
2 changed files with 15 additions and 9 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from test.test_support import verbose, have_unicode
|
from test.test_support import verbose, have_unicode, TestFailed
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# test string formatting operator (I am not sure if this is being tested
|
# test string formatting operator (I am not sure if this is being tested
|
||||||
|
|
@ -210,9 +210,15 @@ def test_exc(formatstr, args, exception, excmsg):
|
||||||
if verbose: print 'no'
|
if verbose: print 'no'
|
||||||
print 'Unexpected exception'
|
print 'Unexpected exception'
|
||||||
raise
|
raise
|
||||||
|
else:
|
||||||
|
raise TestFailed, 'did not get expected exception: %s' % excmsg
|
||||||
|
|
||||||
test_exc('abc %a', 1, ValueError,
|
test_exc('abc %a', 1, ValueError,
|
||||||
"unsupported format character 'a' (0x61) at index 5")
|
"unsupported format character 'a' (0x61) at index 5")
|
||||||
if have_unicode:
|
if have_unicode:
|
||||||
test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
|
test_exc(unicode('abc %\u3000','raw-unicode-escape'), 1, ValueError,
|
||||||
"unsupported format character '?' (0x3000) at index 5")
|
"unsupported format character '?' (0x3000) at index 5")
|
||||||
|
|
||||||
|
test_exc('%d', '1', TypeError, "int argument required")
|
||||||
|
test_exc('%g', '1', TypeError, "float argument required")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3120,11 +3120,11 @@ formatfloat(char *buf, size_t buflen, int flags,
|
||||||
worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
|
worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/
|
||||||
char fmt[20];
|
char fmt[20];
|
||||||
double x;
|
double x;
|
||||||
v = PyNumber_Float(v);
|
x = PyFloat_AsDouble(v);
|
||||||
if (!v)
|
if (x == -1.0 && PyErr_Occurred()) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "float argument required");
|
||||||
return -1;
|
return -1;
|
||||||
x = PyFloat_AS_DOUBLE(v);
|
}
|
||||||
Py_DECREF(v);
|
|
||||||
if (prec < 0)
|
if (prec < 0)
|
||||||
prec = 6;
|
prec = 6;
|
||||||
if (type == 'f' && fabs(x)/1e25 >= 1e25)
|
if (type == 'f' && fabs(x)/1e25 >= 1e25)
|
||||||
|
|
@ -3299,11 +3299,11 @@ formatint(char *buf, size_t buflen, int flags,
|
||||||
char fmt[64]; /* plenty big enough! */
|
char fmt[64]; /* plenty big enough! */
|
||||||
long x;
|
long x;
|
||||||
|
|
||||||
v = PyNumber_Int(v);
|
x = PyInt_AsLong(v);
|
||||||
if (!v)
|
if (x == -1 && PyErr_Occurred()) {
|
||||||
|
PyErr_SetString(PyExc_TypeError, "int argument required");
|
||||||
return -1;
|
return -1;
|
||||||
x = PyInt_AS_LONG(v);
|
}
|
||||||
Py_DECREF(v);
|
|
||||||
if (prec < 0)
|
if (prec < 0)
|
||||||
prec = 1;
|
prec = 1;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue