Skip to content

Commit

Permalink
A minor fix for 64-bit platforms: when __len__() returns Python int
Browse files Browse the repository at this point in the history
containing a value that doesn't fit in a C int, raise OverflowError
rather than truncating silently (and having 50% chance of hitting the
"it should be >= 0" error).
  • Loading branch information
gvanrossum committed Sep 19, 2005
1 parent 3a703b6 commit ba3e6ec
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,17 @@ instance_length(PyInstanceObject *inst)
if (res == NULL)
return -1;
if (PyInt_Check(res)) {
outcome = PyInt_AsLong(res);
long temp = PyInt_AsLong(res);
outcome = (int)temp;
#if SIZEOF_INT < SIZEOF_LONG
/* Overflow check -- range of PyInt is more than C int */
if (outcome != temp) {
PyErr_SetString(PyExc_OverflowError,
"__len__() should return 0 <= outcome < 2**32");
outcome = -1;
}
else
#endif
if (outcome < 0)
PyErr_SetString(PyExc_ValueError,
"__len__() should return >= 0");
Expand Down

0 comments on commit ba3e6ec

Please sign in to comment.