int(long(-sys.maxint-1)) fails on Linux

Christian Tismer tismer at appliedbiometrics.com
Thu Jul 22 07:56:46 EDT 1999


Chad Netzer wrote:
> 
> Compiling python 1.5.2 on Linux Mandrake 6.0,  the "make test" step fails
> because it gets an OverflowError during test_types.py on the following line:
> 
> int(long(-sys.maxint-1))

This went wrong with 1.5 and 1.5.1, and it should be gone
for 1.5.2 .

Have a look at the (Tim Peters) implementation.

Especially at the end:
	if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
		goto overflow;
	return (long)x * sign;

where x is unsigned long.
This cannot get wrong, unless you have one of three problems:

1) The mandrake compiler is buggy
2) You don't have 1.5.2 but 1.5.1
3) Oliver Andrich made a mistake with the package

Your comparison to 1.5.1 is invalid since that definately had a bug.

If you can deduce that case 1) must be true, then I'd like
to send you a test program which I just wrote, to simulate
the above without Python. I'd like to see the generated
assembly code, then.

If it is 2), then please stand in the corner for costing my time.

For 3), Oliver is guilty, and he deserves so many hugs about
his RPM stuff that this is just something that happens.

ciao - pirx

________________________________
long
PyLong_AsLong(vv)
	PyObject *vv;
{
	/* This version by Tim Peters */
	register PyLongObject *v;
	unsigned long x, prev;
	int i, sign;

	if (vv == NULL || !PyLong_Check(vv)) {
		PyErr_BadInternalCall();
		return -1;
	}
	v = (PyLongObject *)vv;
	i = v->ob_size;
	sign = 1;
	x = 0;
	if (i < 0) {
		sign = -1;
		i = -(i);
	}
	while (--i >= 0) {
		prev = x;
		x = (x << SHIFT) + v->ob_digit[i];
		if ((x >> SHIFT) != prev)
			goto overflow;
	}
	/* Haven't lost any bits, but if the sign bit is set we're in
	 * trouble *unless* this is the min negative number.  So,
	 * trouble iff sign bit set && (positive || some bit set other
	 * than the sign bit).
	 */
	if ((long)x < 0 && (sign > 0 || (x << 1) != 0))
		goto overflow;
	return (long)x * sign;

 overflow:
	PyErr_SetString(PyExc_OverflowError,
			"long int too long to convert");
	return -1;
}
________________________________

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list