Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de60401

Browse files
committed
Merged revisions 77218 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent d78735d commit de60401

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

Lib/test/test_getargs2.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import unittest
22
from test import support
33
from _testcapi import getargs_keywords
4-
54
import warnings
6-
warnings.filterwarnings("ignore",
7-
category=DeprecationWarning,
8-
message=".*integer argument expected, got float",
9-
module=__name__)
10-
warnings.filterwarnings("ignore",
11-
category=DeprecationWarning,
12-
message=".*integer argument expected, got float",
13-
module="unittest")
145

156
"""
167
> How about the following counterproposal. This also changes some of
@@ -197,9 +188,24 @@ def test_n(self):
197188
class LongLong_TestCase(unittest.TestCase):
198189
def test_L(self):
199190
from _testcapi import getargs_L
200-
# L returns 'long long', and does range checking (LLONG_MIN ... LLONG_MAX)
191+
# L returns 'long long', and does range checking (LLONG_MIN
192+
# ... LLONG_MAX)
193+
with warnings.catch_warnings():
194+
warnings.filterwarnings(
195+
"ignore",
196+
category=DeprecationWarning,
197+
message=".*integer argument expected, got float",
198+
module=__name__)
199+
self.assertEqual(3, getargs_L(3.14))
200+
with warnings.catch_warnings():
201+
warnings.filterwarnings(
202+
"error",
203+
category=DeprecationWarning,
204+
message=".*integer argument expected, got float",
205+
module="unittest")
206+
self.assertRaises(DeprecationWarning, getargs_L, 3.14)
207+
201208
self.assertRaises(TypeError, getargs_L, "Hello")
202-
self.assertEqual(3, getargs_L(3.14))
203209
self.assertEqual(99, getargs_L(Int()))
204210

205211
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ Core and Builtins
146146
C-API
147147
-----
148148

149+
- Issue #5080: The argument parsing functions PyArg_ParseTuple,
150+
PyArg_ParseTupleAndKeywords, PyArg_VaParse,
151+
PyArg_VaParseTupleAndKeywords and PyArg_Parse now raise a
152+
DeprecationWarning for float arguments passed with the 'L' format
153+
code. This will become a TypeError in a future version of Python,
154+
to match the behaviour of the other integer format codes.
155+
149156
- Issue #7033: function ``PyErr_NewExceptionWithDoc()`` added.
150157

151158
- Issue #7414: 'C' code wasn't being skipped properly (for keyword arguments)

Python/getargs.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,19 @@ converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
582582

583583
#define CONV_UNICODE "(unicode conversion error)"
584584

585+
/* explicitly check for float arguments when integers are expected. For now
586+
* signal a warning. Returns true if an exception was raised. */
587+
static int
588+
float_argument_warning(PyObject *arg)
589+
{
590+
if (PyFloat_Check(arg) &&
591+
PyErr_Warn(PyExc_DeprecationWarning,
592+
"integer argument expected, got float" ))
593+
return 1;
594+
else
595+
return 0;
596+
}
597+
585598
/* Explicitly check for float arguments when integers are expected.
586599
Return 1 for error, 0 if ok. */
587600
static int
@@ -777,7 +790,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
777790
#ifdef HAVE_LONG_LONG
778791
case 'L': {/* PY_LONG_LONG */
779792
PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
780-
PY_LONG_LONG ival = PyLong_AsLongLong( arg );
793+
PY_LONG_LONG ival;
794+
if (float_argument_warning(arg))
795+
return converterr("long<L>", arg, msgbuf, bufsize);
796+
ival = PyLong_AsLongLong(arg);
781797
if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
782798
return converterr("long<L>", arg, msgbuf, bufsize);
783799
} else {

0 commit comments

Comments
 (0)