diff Modules/structmodule.c @ 17343:de9858530a55 legacy-trunk

Repair portability of sign extension when reading signed ints on boxes where sizeof(long)==8. This *was* broken on boxes where signed right shifts didn't sign-extend, but not elsewhere. Unfortunately, apart from the Cray T3E I don't know of such a box, and Guido has so far refused to buy me any Cray machines for home Python testing <wink>. More immediately interesting would be if someone could please test this on *any* sizeof(long)==8 box, to make sure I didn't break it.
author Tim Peters <tim.peters@gmail.com>
date Sun, 08 Apr 2001 23:39:38 +0000
parents 25ddd3dc7a7a
children 104223b3226a 8e187740aa96
line wrap: on
line diff
--- a/Modules/structmodule.c	Sun Apr 08 15:05:16 2001 +0000
+++ b/Modules/structmodule.c	Sun Apr 08 23:39:38 2001 +0000
@@ -653,11 +653,9 @@
 	do {
 		x = (x<<8) | (*p++ & 0xFF);
 	} while (--i > 0);
-	i = 8*(sizeof(long) - f->size);
-	if (i) {
-		x <<= i;
-		x >>= i;
-	}
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG > f->size)
+		x |= -(x & (1L << (8*f->size - 1)));
 	return PyInt_FromLong(x);
 }
 
@@ -767,11 +765,9 @@
 	do {
 		x = (x<<8) | (p[--i] & 0xFF);
 	} while (i > 0);
-	i = 8*(sizeof(long) - f->size);
-	if (i) {
-		x <<= i;
-		x >>= i;
-	}
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG > f->size)
+		x |= -(x & (1L << (8*f->size - 1)));
 	return PyInt_FromLong(x);
 }