[Python-checkins] r46225 - sandbox/trunk/newstruct/Modules/_newstruct.c

bob.ippolito python-checkins at python.org
Thu May 25 19:05:04 CEST 2006


Author: bob.ippolito
Date: Thu May 25 19:05:03 2006
New Revision: 46225

Modified:
   sandbox/trunk/newstruct/Modules/_newstruct.c
Log:
use int instead of long when possible, use PyLong_From*LongLong

Modified: sandbox/trunk/newstruct/Modules/_newstruct.c
==============================================================================
--- sandbox/trunk/newstruct/Modules/_newstruct.c	(original)
+++ sandbox/trunk/newstruct/Modules/_newstruct.c	Thu May 25 19:05:03 2006
@@ -284,6 +284,8 @@
 {
 	unsigned int x;
 	memcpy((char *)&x, p, sizeof x);
+	if (x <= INT_MAX)
+		return PyInt_FromLong((long)x);
 	return PyLong_FromUnsignedLong((unsigned long)x);
 }
 
@@ -300,6 +302,8 @@
 {
 	unsigned long x;
 	memcpy((char *)&x, p, sizeof x);
+	if (x <= INT_MAX)
+		return PyInt_FromLong((long)x);
 	return PyLong_FromUnsignedLong(x);
 }
 
@@ -313,6 +317,8 @@
 {
 	PY_LONG_LONG x;
 	memcpy((char *)&x, p, sizeof x);
+	if (x >= INT_MIN && x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
 	return PyLong_FromLongLong(x);
 }
 
@@ -321,6 +327,8 @@
 {
 	unsigned PY_LONG_LONG x;
 	memcpy((char *)&x, p, sizeof x);
+	if (x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
 	return PyLong_FromUnsignedLongLong(x);
 }
 
@@ -584,28 +592,52 @@
 	do {
 		x = (x<<8) | (*p++ & 0xFF);
 	} while (--i > 0);
-	if (f->size >= 4)
-		return PyLong_FromUnsignedLong(x);
-	else
+	if (x <= INT_MAX)
 		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong(x);
 }
 
 static PyObject *
 bu_longlong(const char *p, const formatdef *f)
 {
+#if HAVE_LONG_LONG
+	PY_LONG_LONG x = 0;
+	int i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG_LONG > f->size)
+		x |= -(x & (1L << (8 * f->size - 1)));
+	if (x >= INT_MIN && x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+	return PyLong_FromLongLong(x);
+#else
 	return _PyLong_FromByteArray((const unsigned char *)p,
 				      8,
 				      0, /* little-endian */
 				      1  /* signed */);
+#endif
 }
 
 static PyObject *
 bu_ulonglong(const char *p, const formatdef *f)
 {
+#if HAVE_LONG_LONG
+	unsigned PY_LONG_LONG x = 0;
+	int i = f->size;
+	do {
+		x = (x<<8) | (*p++ & 0xFF);
+	} while (--i > 0);
+	if (x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+	return PyLong_FromUnsignedLongLong(x);
+#else
 	return _PyLong_FromByteArray((const unsigned char *)p,
 				      8,
 				      0, /* little-endian */
 				      0  /* signed */);
+#endif
 }
 
 static PyObject *
@@ -750,28 +782,52 @@
 	do {
 		x = (x<<8) | (p[--i] & 0xFF);
 	} while (i > 0);
-	if (f->size >= 4)
-		return PyLong_FromUnsignedLong(x);
-	else
+	if (x <= INT_MAX)
 		return PyInt_FromLong((long)x);
+	return PyLong_FromUnsignedLong((long)x);
 }
 
 static PyObject *
 lu_longlong(const char *p, const formatdef *f)
 {
+#if HAVE_LONG_LONG
+	PY_LONG_LONG x = 0;
+	int i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (--i > 0);
+	/* Extend the sign bit. */
+	if (SIZEOF_LONG_LONG > f->size)
+		x |= -(x & (1L << (8 * f->size - 1)));
+	if (x >= INT_MIN && x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
+	return PyLong_FromLongLong(x);
+#else
 	return _PyLong_FromByteArray((const unsigned char *)p,
 				      8,
 				      1, /* little-endian */
 				      1  /* signed */);
+#endif
 }
 
 static PyObject *
 lu_ulonglong(const char *p, const formatdef *f)
 {
+#if HAVE_LONG_LONG
+	unsigned PY_LONG_LONG x = 0;
+	int i = f->size;
+	do {
+		x = (x<<8) | (p[--i] & 0xFF);
+	} while (--i > 0);
+	if (x <= INT_MAX)
+		return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
+	return PyLong_FromUnsignedLongLong(x);
+#else
 	return _PyLong_FromByteArray((const unsigned char *)p,
 				      8,
 				      1, /* little-endian */
 				      0  /* signed */);
+#endif
 }
 
 static PyObject *


More information about the Python-checkins mailing list