[Python-checkins] r79688 - python/branches/py3k/Modules/_struct.c

mark.dickinson python-checkins at python.org
Sat Apr 3 17:26:31 CEST 2010


Author: mark.dickinson
Date: Sat Apr  3 17:26:31 2010
New Revision: 79688

Log:
Internal refactoring in struct.pack:  make all integer conversions go through get_pylong.

Modified:
   python/branches/py3k/Modules/_struct.c

Modified: python/branches/py3k/Modules/_struct.c
==============================================================================
--- python/branches/py3k/Modules/_struct.c	(original)
+++ python/branches/py3k/Modules/_struct.c	Sat Apr  3 17:26:31 2010
@@ -89,7 +89,8 @@
 #pragma options align=reset
 #endif
 
-/* Helper to get a PyLongObject.  Caller should decref. */
+/* Helper for integer format codes: converts an arbitrary Python object to a
+   PyLongObject if possible, otherwise fails.  Caller should decref. */
 
 static PyObject *
 get_pylong(PyObject *v)
@@ -113,13 +114,13 @@
 {
 	long x;
 
-	if (!PyLong_Check(v)) {
-		PyErr_SetString(StructError,
-				"required argument is not an integer");
+	v = get_pylong(v);
+	if (v == NULL)
 		return -1;
-	}
+	assert(PyLong_Check(v));
 	x = PyLong_AsLong(v);
-	if (x == -1 && PyErr_Occurred()) {
+	Py_DECREF(v);
+	if (x == (long)-1 && PyErr_Occurred()) {
 		if (PyErr_ExceptionMatches(PyExc_OverflowError))
 			PyErr_SetString(StructError,
 					"argument out of range");
@@ -137,11 +138,10 @@
 {
 	unsigned long x;
 
-	if (!PyLong_Check(v)) {
-		PyErr_SetString(StructError,
-				"required argument is not an integer");
+	v = get_pylong(v);
+	if (v == NULL)
 		return -1;
-	}
+	assert(PyLong_Check(v));
 	x = PyLong_AsUnsignedLong(v);
 	if (x == (unsigned long)-1 && PyErr_Occurred()) {
 		if (PyErr_ExceptionMatches(PyExc_OverflowError))
@@ -161,13 +161,13 @@
 get_longlong(PyObject *v, PY_LONG_LONG *p)
 {
 	PY_LONG_LONG x;
-	if (!PyLong_Check(v)) {
-		PyErr_SetString(StructError,
-				"required argument is not an integer");
+
+	v = get_pylong(v);
+	if (v == NULL)
 		return -1;
-	}
+	assert(PyLong_Check(v));
 	x = PyLong_AsLongLong(v);
-	if (x == -1 && PyErr_Occurred()) {
+	if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) {
 		if (PyErr_ExceptionMatches(PyExc_OverflowError))
 			PyErr_SetString(StructError,
 					"argument out of range");
@@ -183,13 +183,13 @@
 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
 {
 	unsigned PY_LONG_LONG x;
-	if (!PyLong_Check(v)) {
-		PyErr_SetString(StructError,
-				"required argument is not an integer");
+
+	v = get_pylong(v);
+	if (v == NULL)
 		return -1;
-	}
+	assert(PyLong_Check(v));
 	x = PyLong_AsUnsignedLongLong(v);
-	if (x == -1 && PyErr_Occurred()) {
+	if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) {
 		if (PyErr_ExceptionMatches(PyExc_OverflowError))
 			PyErr_SetString(StructError,
 					"argument out of range");


More information about the Python-checkins mailing list