[Python-checkins] r55842 - in python/trunk: Include/pyport.h Lib/test/test_structmembers.py Misc/NEWS Modules/_testcapimodule.c PC/pyconfig.h Python/structmember.c

martin.v.loewis python-checkins at python.org
Sat Jun 9 09:42:54 CEST 2007


Author: martin.v.loewis
Date: Sat Jun  9 09:42:52 2007
New Revision: 55842

Modified:
   python/trunk/Include/pyport.h
   python/trunk/Lib/test/test_structmembers.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_testcapimodule.c
   python/trunk/PC/pyconfig.h
   python/trunk/Python/structmember.c
Log:
Patch #1733960: Allow T_LONGLONG to accept ints.
Will backport to 2.5.


Modified: python/trunk/Include/pyport.h
==============================================================================
--- python/trunk/Include/pyport.h	(original)
+++ python/trunk/Include/pyport.h	Sat Jun  9 09:42:52 2007
@@ -61,6 +61,9 @@
 #ifdef HAVE_LONG_LONG
 #ifndef PY_LONG_LONG
 #define PY_LONG_LONG long long
+#define PY_LLONG_MIN LLONG_MIN
+#define PY_LLONG_MAX LLONG_MAX
+#define PY_ULLONG_MAX ULLONG_MAX
 #endif
 #endif /* HAVE_LONG_LONG */
 

Modified: python/trunk/Lib/test/test_structmembers.py
==============================================================================
--- python/trunk/Lib/test/test_structmembers.py	(original)
+++ python/trunk/Lib/test/test_structmembers.py	Sat Jun  9 09:42:52 2007
@@ -2,7 +2,8 @@
     CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
     SHRT_MAX, SHRT_MIN, USHRT_MAX, \
     INT_MAX, INT_MIN, UINT_MAX, \
-    LONG_MAX, LONG_MIN, ULONG_MAX
+    LONG_MAX, LONG_MIN, ULONG_MAX, \
+    LLONG_MAX, LLONG_MIN, ULLONG_MAX
 
 import warnings, exceptions, unittest
 from test import test_support
@@ -39,6 +40,23 @@
         ts.T_ULONG=ULONG_MAX
         self.assertEquals(ts.T_ULONG, ULONG_MAX)
 
+        ## T_LONGLONG and T_ULONGLONG may not be present on some platforms
+        if hasattr(ts, 'T_LONGLONG'):
+            ts.T_LONGLONG=LLONG_MAX
+            self.assertEquals(ts.T_LONGLONG, LLONG_MAX)
+            ts.T_LONGLONG=LLONG_MIN
+            self.assertEquals(ts.T_LONGLONG, LLONG_MIN)
+
+            ts.T_ULONGLONG=ULLONG_MAX
+            self.assertEquals(ts.T_ULONGLONG, ULLONG_MAX)
+
+            ## make sure these will accept a plain int as well as a long
+            ts.T_LONGLONG=3
+            self.assertEquals(ts.T_LONGLONG, 3)
+            ts.T_ULONGLONG=4
+            self.assertEquals(ts.T_ULONGLONG, 4)
+
+
 class TestWarnings(unittest.TestCase):
     def has_warned(self, w):
         self.assert_(w.category is RuntimeWarning)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jun  9 09:42:52 2007
@@ -12,6 +12,8 @@
 Core and builtins
 -----------------
 
+- Patch #1733960: Allow T_LONGLONG to accept ints.
+
 - Prevent expandtabs() on string and unicode objects from causing a segfault when
   a large width is passed on 32-bit platforms.
 

Modified: python/trunk/Modules/_testcapimodule.c
==============================================================================
--- python/trunk/Modules/_testcapimodule.c	(original)
+++ python/trunk/Modules/_testcapimodule.c	Sat Jun  9 09:42:52 2007
@@ -772,6 +772,10 @@
 	unsigned long ulong_member;
 	float float_member;
 	double double_member;
+#ifdef HAVE_LONG_LONG
+	PY_LONG_LONG longlong_member;
+	unsigned PY_LONG_LONG ulonglong_member;
+#endif
 } all_structmembers;
 
 typedef struct {
@@ -790,23 +794,40 @@
 	{"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL},
 	{"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL},
 	{"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL},
+#ifdef HAVE_LONG_LONG
+	{"T_LONGLONG", T_LONGLONG, offsetof(test_structmembers, structmembers.longlong_member), 0, NULL},
+	{"T_ULONGLONG", T_ULONGLONG, offsetof(test_structmembers, structmembers.ulonglong_member), 0, NULL},
+#endif
 	{NULL}
 };
 
 
 static PyObject *test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs){
 	static char *keywords[]={"T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", "T_INT", "T_UINT",
-		"T_LONG", "T_ULONG", "T_FLOAT", "T_DOUBLE", NULL};
+		"T_LONG", "T_ULONG", "T_FLOAT", "T_DOUBLE",
+		#ifdef HAVE_LONG_LONG	
+		"T_LONGLONG", "T_ULONGLONG",
+		#endif
+		NULL};
+	static char *fmt="|bBhHiIlkfd"
+		#ifdef HAVE_LONG_LONG
+		"LK"
+		#endif
+		;
 	test_structmembers *ob=PyObject_New(test_structmembers, type);
 	if (ob==NULL)
 		return NULL;
 	memset(&ob->structmembers, 0, sizeof(all_structmembers));
-	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|bBhHiIlkfd", keywords,
+	if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords,
 		&ob->structmembers.byte_member, &ob->structmembers.ubyte_member,
 		&ob->structmembers.short_member, &ob->structmembers.ushort_member,
 		&ob->structmembers.int_member, &ob->structmembers.uint_member, 
 		&ob->structmembers.long_member, &ob->structmembers.ulong_member,
-		&ob->structmembers.float_member, &ob->structmembers.double_member)){
+		&ob->structmembers.float_member, &ob->structmembers.double_member
+		#ifdef HAVE_LONG_LONG
+		,&ob->structmembers.longlong_member, &ob->structmembers.ulonglong_member
+		#endif
+		)){
 		Py_DECREF(ob);
 		return NULL;
 		}
@@ -889,6 +910,9 @@
 	PyModule_AddObject(m, "FLT_MIN", PyFloat_FromDouble(FLT_MIN));
 	PyModule_AddObject(m, "DBL_MAX", PyFloat_FromDouble(DBL_MAX));
 	PyModule_AddObject(m, "DBL_MIN", PyFloat_FromDouble(DBL_MIN));
+	PyModule_AddObject(m, "LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
+	PyModule_AddObject(m, "LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
+	PyModule_AddObject(m, "ULLONG_MAX", PyLong_FromUnsignedLongLong(PY_ULLONG_MAX));
 	PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyInt_FromSsize_t(PY_SSIZE_T_MAX));
 	PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyInt_FromSsize_t(PY_SSIZE_T_MIN));
 

Modified: python/trunk/PC/pyconfig.h
==============================================================================
--- python/trunk/PC/pyconfig.h	(original)
+++ python/trunk/PC/pyconfig.h	Sat Jun  9 09:42:52 2007
@@ -247,6 +247,9 @@
 #define COMPILER "[gcc]"
 #define hypot _hypot
 #define PY_LONG_LONG long long
+#define PY_LLONG_MIN LLONG_MIN
+#define PY_LLONG_MAX LLONG_MAX
+#define PY_ULLONG_MAX ULLONG_MAX
 #endif /* GNUC */
 
 /* ------------------------------------------------------------------------*/
@@ -272,6 +275,9 @@
 #define HAVE_LONG_LONG 1
 #ifndef PY_LONG_LONG
 #	define PY_LONG_LONG __int64
+#	define PY_LLONG_MAX LLONG_MAX
+#	define PY_LLONG_MIN LLONG_MIN
+#	define PY_ULLONG_MIN ULLONG_MIN
 #endif
 
 /* For Windows the Python core is in a DLL by default.  Test

Modified: python/trunk/Python/structmember.c
==============================================================================
--- python/trunk/Python/structmember.c	(original)
+++ python/trunk/Python/structmember.c	Sat Jun  9 09:42:52 2007
@@ -293,31 +293,25 @@
 		}
 		break;
 #ifdef HAVE_LONG_LONG
-	case T_LONGLONG:
-		if (!PyLong_Check(v)) {
-			PyErr_BadArgument();
+	case T_LONGLONG:{
+		PY_LONG_LONG value;
+		*(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
+		if ((value == -1) && PyErr_Occurred())
 			return -1;
-		} else {
-                        PY_LONG_LONG value;
-                        *(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
-                        if ((value == -1) && PyErr_Occurred()) {
-                                return -1;
-                        }
-                }
-                break;
-	case T_ULONGLONG:
-                if (!PyLong_Check(v)) {
-                        PyErr_BadArgument();
-                        return -1;
-                } else {
-                        unsigned PY_LONG_LONG value;
-                        *(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
-                        if ((value == (unsigned PY_LONG_LONG)-1) &&
-			    PyErr_Occurred()) {
-                                return -1;
-                        }
-                }
-                break;
+		break;
+		}
+	case T_ULONGLONG:{
+		unsigned PY_LONG_LONG value;
+		/* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
+			doesn't ??? */
+		if (PyLong_Check(v))
+			*(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
+		else
+			*(unsigned PY_LONG_LONG*)addr = value = PyInt_AsLong(v);
+		if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred())
+			return -1;
+		break;
+		}
 #endif /* HAVE_LONG_LONG */
 	default:
 		PyErr_Format(PyExc_SystemError,


More information about the Python-checkins mailing list