mirror of
https://github.com/python/cpython.git
synced 2025-10-17 12:18:23 +00:00
Merged revisions 77218 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77218 | mark.dickinson | 2010-01-01 17:27:30 +0000 (Fri, 01 Jan 2010) | 5 lines Issue #5080: turn the DeprecationWarning from float arguments passed to integer PyArg_Parse* format codes into a TypeError. Add a DeprecationWarning for floats passed with the 'L' format code, which didn't previously have a warning. ........
This commit is contained in:
parent
d78735d8e3
commit
de60401909
3 changed files with 41 additions and 12 deletions
|
@ -1,16 +1,7 @@
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
from test import support
|
||||||
from _testcapi import getargs_keywords
|
from _testcapi import getargs_keywords
|
||||||
|
|
||||||
import warnings
|
import warnings
|
||||||
warnings.filterwarnings("ignore",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
message=".*integer argument expected, got float",
|
|
||||||
module=__name__)
|
|
||||||
warnings.filterwarnings("ignore",
|
|
||||||
category=DeprecationWarning,
|
|
||||||
message=".*integer argument expected, got float",
|
|
||||||
module="unittest")
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
> How about the following counterproposal. This also changes some of
|
> How about the following counterproposal. This also changes some of
|
||||||
|
@ -197,9 +188,24 @@ class Signed_TestCase(unittest.TestCase):
|
||||||
class LongLong_TestCase(unittest.TestCase):
|
class LongLong_TestCase(unittest.TestCase):
|
||||||
def test_L(self):
|
def test_L(self):
|
||||||
from _testcapi import getargs_L
|
from _testcapi import getargs_L
|
||||||
# L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
|
# L returns 'long long', and does range checking (LLONG_MIN
|
||||||
|
# ... LLONG_MAX)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.filterwarnings(
|
||||||
|
"ignore",
|
||||||
|
category=DeprecationWarning,
|
||||||
|
message=".*integer argument expected, got float",
|
||||||
|
module=__name__)
|
||||||
|
self.assertEqual(3, getargs_L(3.14))
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.filterwarnings(
|
||||||
|
"error",
|
||||||
|
category=DeprecationWarning,
|
||||||
|
message=".*integer argument expected, got float",
|
||||||
|
module="unittest")
|
||||||
|
self.assertRaises(DeprecationWarning, getargs_L, 3.14)
|
||||||
|
|
||||||
self.assertRaises(TypeError, getargs_L, "Hello")
|
self.assertRaises(TypeError, getargs_L, "Hello")
|
||||||
self.assertEqual(3, getargs_L(3.14))
|
|
||||||
self.assertEqual(99, getargs_L(Int()))
|
self.assertEqual(99, getargs_L(Int()))
|
||||||
|
|
||||||
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
|
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
|
||||||
|
|
|
@ -146,6 +146,13 @@ Core and Builtins
|
||||||
C-API
|
C-API
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #5080: The argument parsing functions PyArg_ParseTuple,
|
||||||
|
PyArg_ParseTupleAndKeywords, PyArg_VaParse,
|
||||||
|
PyArg_VaParseTupleAndKeywords and PyArg_Parse now raise a
|
||||||
|
DeprecationWarning for float arguments passed with the 'L' format
|
||||||
|
code. This will become a TypeError in a future version of Python,
|
||||||
|
to match the behaviour of the other integer format codes.
|
||||||
|
|
||||||
- Issue #7033: function ``PyErr_NewExceptionWithDoc()`` added.
|
- Issue #7033: function ``PyErr_NewExceptionWithDoc()`` added.
|
||||||
|
|
||||||
- Issue #7414: 'C' code wasn't being skipped properly (for keyword arguments)
|
- Issue #7414: 'C' code wasn't being skipped properly (for keyword arguments)
|
||||||
|
|
|
@ -582,6 +582,19 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
|
||||||
|
|
||||||
#define CONV_UNICODE "(unicode conversion error)"
|
#define CONV_UNICODE "(unicode conversion error)"
|
||||||
|
|
||||||
|
/* explicitly check for float arguments when integers are expected. For now
|
||||||
|
* signal a warning. Returns true if an exception was raised. */
|
||||||
|
static int
|
||||||
|
float_argument_warning(PyObject *arg)
|
||||||
|
{
|
||||||
|
if (PyFloat_Check(arg) &&
|
||||||
|
PyErr_Warn(PyExc_DeprecationWarning,
|
||||||
|
"integer argument expected, got float" ))
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* Explicitly check for float arguments when integers are expected.
|
/* Explicitly check for float arguments when integers are expected.
|
||||||
Return 1 for error, 0 if ok. */
|
Return 1 for error, 0 if ok. */
|
||||||
static int
|
static int
|
||||||
|
@ -777,7 +790,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
||||||
#ifdef HAVE_LONG_LONG
|
#ifdef HAVE_LONG_LONG
|
||||||
case 'L': {/* PY_LONG_LONG */
|
case 'L': {/* PY_LONG_LONG */
|
||||||
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
|
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
|
||||||
PY_LONG_LONG ival = PyLong_AsLongLong( arg );
|
PY_LONG_LONG ival;
|
||||||
|
if (float_argument_warning(arg))
|
||||||
|
return converterr("long<L>", arg, msgbuf, bufsize);
|
||||||
|
ival = PyLong_AsLongLong(arg);
|
||||||
if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
|
if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
|
||||||
return converterr("long<L>", arg, msgbuf, bufsize);
|
return converterr("long<L>", arg, msgbuf, bufsize);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue