[Python-checkins] r46450 - python/trunk/Modules/_struct.c

bob.ippolito python-checkins at python.org
Sat May 27 13:47:13 CEST 2006


Author: bob.ippolito
Date: Sat May 27 13:47:12 2006
New Revision: 46450

Modified:
   python/trunk/Modules/_struct.c
Log:
Remove the range checking and int usage #defines from _struct and strip out the now-dead code

Modified: python/trunk/Modules/_struct.c
==============================================================================
--- python/trunk/Modules/_struct.c	(original)
+++ python/trunk/Modules/_struct.c	Sat May 27 13:47:12 2006
@@ -17,17 +17,6 @@
 typedef int Py_ssize_t;
 #endif
 
-
-/* PY_USE_INT_WHEN_POSSIBLE is a flag that changes the 
-   struct API to return int instead of long when possible. This is
-   often a significant performance improvement. */
-#define PY_USE_INT_WHEN_POSSIBLE 1
-
-/* PY_STRUCT_RANGE_CHECKING performs range checking on all arguments
-   to be packed. This will break some incorrect code that happened
-   to accidentally do the right thing anyway (such as binhex). */
-#define PY_STRUCT_RANGE_CHECKING 1
-
 /* The translation function for each format character is table driven */
 typedef struct _formatdef {
 	char format;
@@ -232,7 +221,6 @@
 	return PyFloat_FromDouble(x);
 }
 
-#ifdef PY_STRUCT_RANGE_CHECKING
 /* Helper to format the range error exceptions */
 static int
 _range_error(char format, Py_ssize_t size, int is_unsigned)
@@ -261,7 +249,6 @@
 	}
 	return -1;
 }
-#endif
 
 
 
@@ -331,10 +318,8 @@
 {
 	unsigned int x;
 	memcpy((char *)&x, p, sizeof x);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong((long)x);
-#endif
 	return PyLong_FromUnsignedLong((unsigned long)x);
 }
 
@@ -351,10 +336,8 @@
 {
 	unsigned long x;
 	memcpy((char *)&x, p, sizeof x);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong((long)x);
-#endif
 	return PyLong_FromUnsignedLong(x);
 }
 
@@ -368,10 +351,8 @@
 {
 	PY_LONG_LONG x;
 	memcpy((char *)&x, p, sizeof x);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x >= LONG_MIN && x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
-#endif
 	return PyLong_FromLongLong(x);
 }
 
@@ -380,10 +361,8 @@
 {
 	unsigned PY_LONG_LONG x;
 	memcpy((char *)&x, p, sizeof x);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
-#endif
 	return PyLong_FromUnsignedLongLong(x);
 }
 
@@ -497,7 +476,7 @@
 	int y;
 	if (get_long(v, &x) < 0)
 		return -1;
-#if defined(PY_STRUCT_RANGE_CHECKING) && (SIZEOF_LONG > SIZEOF_INT)
+#if (SIZEOF_LONG > SIZEOF_INT)
 	if (x < INT_MIN || x > INT_MAX)
 		return _range_error(f->format, sizeof(y), 0);
 #endif
@@ -512,13 +491,9 @@
 	unsigned long x;
 	unsigned int y;
 	if (get_ulong(v, &x) < 0)
-#ifdef PY_STRUCT_RANGE_CHECKING
 		return _range_error(f->format, sizeof(y), 1);
-#else
-		return -1;
-#endif
 	y = (unsigned int)x;
-#if defined(PY_STRUCT_RANGE_CHECKING) && (SIZEOF_LONG > SIZEOF_INT)
+#if (SIZEOF_LONG > SIZEOF_INT)
 	if (x > UINT_MAX)
 		return _range_error(f->format, sizeof(y), 1);
 #endif
@@ -541,11 +516,7 @@
 {
 	unsigned long x;
 	if (get_ulong(v, &x) < 0)
-#ifdef PY_STRUCT_RANGE_CHECKING
 		return _range_error(f->format, sizeof(x), 1);
-#else
-		return -1;
-#endif
 	memcpy(p, (char *)&x, sizeof x);
 	return 0;
 }
@@ -663,20 +634,15 @@
 	do {
 		x = (x<<8) | (*p++ & 0xFF);
 	} while (--i > 0);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong((long)x);
-#else
-	if (SIZEOF_LONG > f->size)
-		return PyInt_FromLong((long)x);
-#endif
 	return PyLong_FromUnsignedLong(x);
 }
 
 static PyObject *
 bu_longlong(const char *p, const formatdef *f)
 {
-#if HAVE_LONG_LONG
+#ifdef HAVE_LONG_LONG
 	PY_LONG_LONG x = 0;
 	Py_ssize_t i = f->size;
 	do {
@@ -685,10 +651,8 @@
 	/* Extend the sign bit. */
 	if (SIZEOF_LONG_LONG > f->size)
 		x |= -(x & (1L << (8 * f->size - 1)));
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x >= LONG_MIN && x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
-#endif
 	return PyLong_FromLongLong(x);
 #else
 	return _PyLong_FromByteArray((const unsigned char *)p,
@@ -701,16 +665,14 @@
 static PyObject *
 bu_ulonglong(const char *p, const formatdef *f)
 {
-#if HAVE_LONG_LONG
+#ifdef HAVE_LONG_LONG
 	unsigned PY_LONG_LONG x = 0;
 	Py_ssize_t i = f->size;
 	do {
 		x = (x<<8) | (*p++ & 0xFF);
 	} while (--i > 0);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
-#endif
 	return PyLong_FromUnsignedLongLong(x);
 #else
 	return _PyLong_FromByteArray((const unsigned char *)p,
@@ -740,15 +702,13 @@
 	if (get_long(v, &x) < 0)
 		return -1;
 	i = f->size;
-#ifdef PY_STRUCT_RANGE_CHECKING
 	if (i != SIZEOF_LONG && (
 		(i == 2 && (x < -32768 || x > 32767))
-#if SIZEOF_LONG != 4
+#if (SIZEOF_LONG != 4)
 		|| (i == 4) && (x < -2147483648L || x > -2147483647L)
 #endif
 		))
 		return _range_error(f->format, i, 0);
-#endif
 	do {
 		p[--i] = (char)x;
 		x >>= 8;
@@ -764,10 +724,8 @@
 	if (get_ulong(v, &x) < 0)
 		return -1;
 	i = f->size;
-#ifdef PY_STRUCT_RANGE_CHECKING
 	if (i != SIZEOF_LONG && x >= (1U << (((unsigned int)i) * 8)))
 		return _range_error(f->format, f->size, 1);
-#endif
 	do {
 		p[--i] = (char)x;
 		x >>= 8;
@@ -875,20 +833,15 @@
 	do {
 		x = (x<<8) | (p[--i] & 0xFF);
 	} while (i > 0);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong((long)x);
-#else
-	if (SIZEOF_LONG > f->size)
-		return PyInt_FromLong((long)x);
-#endif
 	return PyLong_FromUnsignedLong((long)x);
 }
 
 static PyObject *
 lu_longlong(const char *p, const formatdef *f)
 {
-#if HAVE_LONG_LONG
+#ifdef HAVE_LONG_LONG
 	PY_LONG_LONG x = 0;
 	Py_ssize_t i = f->size;
 	do {
@@ -897,10 +850,8 @@
 	/* Extend the sign bit. */
 	if (SIZEOF_LONG_LONG > f->size)
 		x |= -(x & (1L << (8 * f->size - 1)));
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x >= LONG_MIN && x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
-#endif
 	return PyLong_FromLongLong(x);
 #else
 	return _PyLong_FromByteArray((const unsigned char *)p,
@@ -913,16 +864,14 @@
 static PyObject *
 lu_ulonglong(const char *p, const formatdef *f)
 {
-#if HAVE_LONG_LONG
+#ifdef HAVE_LONG_LONG
 	unsigned PY_LONG_LONG x = 0;
 	Py_ssize_t i = f->size;
 	do {
 		x = (x<<8) | (p[--i] & 0xFF);
 	} while (i > 0);
-#ifdef PY_USE_INT_WHEN_POSSIBLE
 	if (x <= LONG_MAX)
 		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
-#endif
 	return PyLong_FromUnsignedLongLong(x);
 #else
 	return _PyLong_FromByteArray((const unsigned char *)p,
@@ -952,15 +901,13 @@
 	if (get_long(v, &x) < 0)
 		return -1;
 	i = f->size;
-#ifdef PY_STRUCT_RANGE_CHECKING
 	if (i != SIZEOF_LONG && (
 		(i == 2 && (x < -32768 || x > 32767))
-#if SIZEOF_LONG != 4
+#if (SIZEOF_LONG != 4)
 		|| (i == 4) && (x < -2147483648L || x > -2147483647L)
 #endif
 		))
 		return _range_error(f->format, i, 0);
-#endif
 	do {
 		*p++ = (char)x;
 		x >>= 8;
@@ -976,10 +923,8 @@
 	if (get_ulong(v, &x) < 0)
 		return -1;
 	i = f->size;
-#ifdef PY_STRUCT_RANGE_CHECKING
 	if (i != SIZEOF_LONG && x >= (1U << (((unsigned int)i) * 8)))
 		return _range_error(f->format, f->size, 1);
-#endif
 	do {
 		*p++ = (char)x;
 		x >>= 8;


More information about the Python-checkins mailing list