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

Skip to content

Commit 2177607

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

6 files changed

Lines changed: 39 additions & 10 deletions

File tree

Doc/c-api/long.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,26 @@ All integers are implemented as "long" integer objects of arbitrary size.
168168

169169
.. cfunction:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
170170

171-
Return a C :ctype:`long long` from a Python integer. If *pylong* cannot be
172-
represented as a :ctype:`long long`, an :exc:`OverflowError` will be raised.
171+
.. index::
172+
single: OverflowError (built-in exception)
173173

174+
Return a C :ctype:`long long` from a Python integer. If *pylong*
175+
cannot be represented as a :ctype:`long long`, an
176+
:exc:`OverflowError` is raised and ``-1`` is returned.
174177

175178
.. cfunction:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
176179

177-
Return a C :ctype:`unsigned long long` from a Python integer. If *pylong*
178-
cannot be represented as an :ctype:`unsigned long long`, an :exc:`OverflowError`
179-
will be raised if the value is positive, or a :exc:`TypeError` will be raised if
180-
the value is negative.
180+
.. index::
181+
single: OverflowError (built-in exception)
182+
183+
Return a C :ctype:`unsigned long long` from a Python integer. If
184+
*pylong* cannot be represented as an :ctype:`unsigned long long`,
185+
an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
186+
returned.
181187

188+
.. versionchanged:: 3.1
189+
A negative *pylong* now raises :exc:`OverflowError`, not
190+
:exc:`TypeError`.
182191

183192
.. cfunction:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
184193

Lib/test/test_struct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def decorator(*args, **kw):
4848
def deprecated_err(func, *args):
4949
try:
5050
func(*args)
51-
except (struct.error, TypeError):
51+
except (struct.error, OverflowError):
5252
pass
5353
except DeprecationWarning:
5454
if not PY_STRUCT_OVERFLOW_MASKING:
@@ -191,7 +191,7 @@ def test_new_features(self):
191191

192192
def test_native_qQ(self):
193193
# can't pack -1 as unsigned regardless
194-
self.assertRaises((struct.error, TypeError), struct.pack, "Q", -1)
194+
self.assertRaises((struct.error, OverflowError), struct.pack, "Q", -1)
195195
# can't pack string as 'q' regardless
196196
self.assertRaises(struct.error, struct.pack, "q", "a")
197197
# ditto, but 'Q'

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Simon Cross
152152
Drew Csillag
153153
John Cugini
154154
Tom Culliton
155+
Lisandro Dalcin
155156
Andrew Dalke
156157
Lars Damerow
157158
Eric Daniel

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ Build
467467
C-API
468468
-----
469469

470+
- Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError
471+
for negative arguments. Previously, it raised TypeError.
472+
470473
- Issue #4720: The format for PyArg_ParseTupleAndKeywords can begin with '|'.
471474

472475
- Issue #3632: from the gdb debugger, the 'pyo' macro can now be called when

Modules/testcapi_long.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ TESTNAME(PyObject *error(const char*))
9797
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
9898
return error(
9999
"PyLong_AsUnsignedXXX(-1) didn't complain");
100+
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
101+
return error(
102+
"PyLong_AsUnsignedXXX(-1) raised "
103+
"something other than OverflowError");
100104
PyErr_Clear();
101105
UNBIND(x);
102106

@@ -112,11 +116,15 @@ TESTNAME(PyObject *error(const char*))
112116
return error(
113117
"unexpected NULL from PyNumber_Lshift");
114118

115-
uout = F_PY_TO_U(x);
119+
uout = F_PY_TO_U(x);
116120
if (uout != (unsigned TYPENAME)-1 || !PyErr_Occurred())
117121
return error(
118122
"PyLong_AsUnsignedXXX(2**NBITS) didn't "
119123
"complain");
124+
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
125+
return error(
126+
"PyLong_AsUnsignedXXX(2**NBITS) raised "
127+
"something other than OverflowError");
120128
PyErr_Clear();
121129

122130
/* Signed complains about 2**(NBITS-1)?
@@ -132,6 +140,10 @@ TESTNAME(PyObject *error(const char*))
132140
return error(
133141
"PyLong_AsXXX(2**(NBITS-1)) didn't "
134142
"complain");
143+
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
144+
return error(
145+
"PyLong_AsXXX(2**(NBITS-1)) raised "
146+
"something other than OverflowError");
135147
PyErr_Clear();
136148

137149
/* Signed complains about -2**(NBITS-1)-1?;
@@ -153,6 +165,10 @@ TESTNAME(PyObject *error(const char*))
153165
return error(
154166
"PyLong_AsXXX(-2**(NBITS-1)-1) didn't "
155167
"complain");
168+
if (!PyErr_ExceptionMatches(PyExc_OverflowError))
169+
return error(
170+
"PyLong_AsXXX(-2**(NBITS-1)-1) raised "
171+
"something other than OverflowError");
156172
PyErr_Clear();
157173
UNBIND(y);
158174

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ _PyLong_AsByteArray(PyLongObject* v,
786786
if (Py_SIZE(v) < 0) {
787787
ndigits = -(Py_SIZE(v));
788788
if (!is_signed) {
789-
PyErr_SetString(PyExc_TypeError,
789+
PyErr_SetString(PyExc_OverflowError,
790790
"can't convert negative int to unsigned");
791791
return -1;
792792
}

0 commit comments

Comments
 (0)