mirror of https://github.com/python/cpython.git
Merged revisions 69498 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r69498 | mark.dickinson | 2009-02-10 15:46:50 +0000 (Tue, 10 Feb 2009) | 6 lines Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for negative arguments. Previously, it raised TypeError. Thanks Lisandro Dalcin. ........
This commit is contained in:
parent
eeba356308
commit
21776074cc
|
@ -168,17 +168,26 @@ All integers are implemented as "long" integer objects of arbitrary size.
|
||||||
|
|
||||||
.. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
|
.. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
|
||||||
|
|
||||||
Return a C :ctype:`long long` from a Python integer. If *pylong* cannot be
|
.. index::
|
||||||
represented as a :ctype:`long long`, an :exc:`OverflowError` will be raised.
|
single: OverflowError (built-in exception)
|
||||||
|
|
||||||
|
Return a C :ctype:`long long` from a Python integer. If *pylong*
|
||||||
|
cannot be represented as a :ctype:`long long`, an
|
||||||
|
:exc:`OverflowError` is raised and ``-1`` is returned.
|
||||||
|
|
||||||
.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
|
.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
|
||||||
|
|
||||||
Return a C :ctype:`unsigned long long` from a Python integer. If *pylong*
|
.. index::
|
||||||
cannot be represented as an :ctype:`unsigned long long`, an :exc:`OverflowError`
|
single: OverflowError (built-in exception)
|
||||||
will be raised if the value is positive, or a :exc:`TypeError` will be raised if
|
|
||||||
the value is negative.
|
|
||||||
|
|
||||||
|
Return a C :ctype:`unsigned long long` from a Python integer. If
|
||||||
|
*pylong* cannot be represented as an :ctype:`unsigned long long`,
|
||||||
|
an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
|
||||||
|
returned.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.1
|
||||||
|
A negative *pylong* now raises :exc:`OverflowError`, not
|
||||||
|
:exc:`TypeError`.
|
||||||
|
|
||||||
.. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
|
.. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ def decorator(*args, **kw):
|
||||||
def deprecated_err(func, *args):
|
def deprecated_err(func, *args):
|
||||||
try:
|
try:
|
||||||
func(*args)
|
func(*args)
|
||||||
except (struct.error, TypeError):
|
except (struct.error, OverflowError):
|
||||||
pass
|
pass
|
||||||
except DeprecationWarning:
|
except DeprecationWarning:
|
||||||
if not PY_STRUCT_OVERFLOW_MASKING:
|
if not PY_STRUCT_OVERFLOW_MASKING:
|
||||||
|
@ -191,7 +191,7 @@ def test_new_features(self):
|
||||||
|
|
||||||
def test_native_qQ(self):
|
def test_native_qQ(self):
|
||||||
# can't pack -1 as unsigned regardless
|
# can't pack -1 as unsigned regardless
|
||||||
self.assertRaises((struct.error, TypeError), struct.pack, "Q", -1)
|
self.assertRaises((struct.error, OverflowError), struct.pack, "Q", -1)
|
||||||
# can't pack string as 'q' regardless
|
# can't pack string as 'q' regardless
|
||||||
self.assertRaises(struct.error, struct.pack, "q", "a")
|
self.assertRaises(struct.error, struct.pack, "q", "a")
|
||||||
# ditto, but 'Q'
|
# ditto, but 'Q'
|
||||||
|
|
|
@ -152,6 +152,7 @@ Simon Cross
|
||||||
Drew Csillag
|
Drew Csillag
|
||||||
John Cugini
|
John Cugini
|
||||||
Tom Culliton
|
Tom Culliton
|
||||||
|
Lisandro Dalcin
|
||||||
Andrew Dalke
|
Andrew Dalke
|
||||||
Lars Damerow
|
Lars Damerow
|
||||||
Eric Daniel
|
Eric Daniel
|
||||||
|
|
|
@ -467,6 +467,9 @@ Build
|
||||||
C-API
|
C-API
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
- Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError
|
||||||
|
for negative arguments. Previously, it raised TypeError.
|
||||||
|
|
||||||
- Issue #4720: The format for PyArg_ParseTupleAndKeywords can begin with '|'.
|
- Issue #4720: The format for PyArg_ParseTupleAndKeywords can begin with '|'.
|
||||||
|
|
||||||
- Issue #3632: from the gdb debugger, the 'pyo' macro can now be called when
|
- Issue #3632: from the gdb debugger, the 'pyo' macro can now be called when
|
||||||
|
|
|
@ -97,6 +97,10 @@ TESTNAME(PyObject *error(const char*))
|
||||||
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
|
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
|
||||||
return error(
|
return error(
|
||||||
"PyLong_AsUnsignedXXX(-1) didn't complain");
|
"PyLong_AsUnsignedXXX(-1) didn't complain");
|
||||||
|
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||||
|
return error(
|
||||||
|
"PyLong_AsUnsignedXXX(-1) raised "
|
||||||
|
"something other than OverflowError");
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
UNBIND(x);
|
UNBIND(x);
|
||||||
|
|
||||||
|
@ -112,11 +116,15 @@ TESTNAME(PyObject *error(const char*))
|
||||||
return error(
|
return error(
|
||||||
"unexpected NULL from PyNumber_Lshift");
|
"unexpected NULL from PyNumber_Lshift");
|
||||||
|
|
||||||
uout = F_PY_TO_U(x);
|
uout = F_PY_TO_U(x);
|
||||||
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
|
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
|
||||||
return error(
|
return error(
|
||||||
"PyLong_AsUnsignedXXX(2**NBITS) didn't "
|
"PyLong_AsUnsignedXXX(2**NBITS) didn't "
|
||||||
"complain");
|
"complain");
|
||||||
|
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||||
|
return error(
|
||||||
|
"PyLong_AsUnsignedXXX(2**NBITS) raised "
|
||||||
|
"something other than OverflowError");
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
|
||||||
/* Signed complains about 2**(NBITS-1)?
|
/* Signed complains about 2**(NBITS-1)?
|
||||||
|
@ -132,6 +140,10 @@ TESTNAME(PyObject *error(const char*))
|
||||||
return error(
|
return error(
|
||||||
"PyLong_AsXXX(2**(NBITS-1)) didn't "
|
"PyLong_AsXXX(2**(NBITS-1)) didn't "
|
||||||
"complain");
|
"complain");
|
||||||
|
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||||
|
return error(
|
||||||
|
"PyLong_AsXXX(2**(NBITS-1)) raised "
|
||||||
|
"something other than OverflowError");
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
|
|
||||||
/* Signed complains about -2**(NBITS-1)-1?;
|
/* Signed complains about -2**(NBITS-1)-1?;
|
||||||
|
@ -153,6 +165,10 @@ TESTNAME(PyObject *error(const char*))
|
||||||
return error(
|
return error(
|
||||||
"PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
|
"PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
|
||||||
"complain");
|
"complain");
|
||||||
|
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
|
||||||
|
return error(
|
||||||
|
"PyLong_AsXXX(-2**(NBITS-1)-1) raised "
|
||||||
|
"something other than OverflowError");
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
UNBIND(y);
|
UNBIND(y);
|
||||||
|
|
||||||
|
|
|
@ -786,7 +786,7 @@ _PyLong_AsByteArray(PyLongObject* v,
|
||||||
if (Py_SIZE(v) < 0) {
|
if (Py_SIZE(v) < 0) {
|
||||||
ndigits = -(Py_SIZE(v));
|
ndigits = -(Py_SIZE(v));
|
||||||
if (!is_signed) {
|
if (!is_signed) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
"can't convert negative int to unsigned");
|
"can't convert negative int to unsigned");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue