Index: Lib/test/test_struct.py =================================================================== --- Lib/test/test_struct.py (revision 78756) +++ Lib/test/test_struct.py (working copy) @@ -6,7 +6,7 @@ DeprecationWarning) from functools import wraps -from test.test_support import run_unittest +from test.test_support import run_unittest, check_warnings import sys ISBIGENDIAN = sys.byteorder == "big" @@ -291,17 +291,29 @@ class NotAnIntOS: def __int__(self): - return 10585 + return 85 def __long__(self): return -163L - for badobject in ("a string", 3+42j, randrange, - NotAnIntNS(), NotAnIntOS()): - self.assertRaises(struct.error, - struct.pack, format, + for badobject in ("a string", 3+42j, randrange): + self.assertRaises((TypeError, struct.error), + struct.pack, self.format, badobject) + # check that attempts to convert non-integers produce + # a DeprecationWarning + for nonint in NotAnIntNS(), NotAnIntOS(): + with check_warnings() as w: + warnings.simplefilter( + action="always", + category=DeprecationWarning + ) + self.assertEqual(struct.pack(self.format, nonint), + struct.pack(self.format, int(nonint))) + self.assertEqual(w.category, DeprecationWarning) + self.assertIn("got non-integer", str(w.message)) + byteorders = '', '@', '=', '<', '>', '!' for code in integer_codes: for byteorder in byteorders: Index: Modules/_struct.c =================================================================== --- Modules/_struct.c (revision 78756) +++ Modules/_struct.c (working copy) @@ -17,7 +17,10 @@ typedef int Py_ssize_t; #endif -#define FLOAT_COERCE "integer argument expected, got float" +/* warning messages */ +#define FLOAT_COERCE_WARN "integer argument expected, got float" +#define NON_INTEGER_WARN "integer argument expected, got non-integer " \ + "(implicit conversion using __int__ is deprecated)" /* The translation function for each format character is table driven */ @@ -104,21 +107,57 @@ static PyObject * get_pylong(PyObject *v) { + PyObject *r; assert(v != NULL); - if (PyInt_Check(v)) - return PyLong_FromLong(PyInt_AS_LONG(v)); - if (PyLong_Check(v)) { + if (!PyInt_Check(v) && !PyLong_Check(v)) { + PyNumberMethods *m; + /* Not an integer; try to use __int__ to convert to an + integer. This behaviour is deprecated, and is removed in + Python 3.x. */ + m = Py_TYPE(v)->tp_as_number; + if (m != NULL && m->nb_int != NULL) { + /* Special case warning message for floats, for + backwards compatibility. */ + if (PyFloat_Check(v)) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + FLOAT_COERCE_WARN, 1)) + return NULL; + } + else { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + NON_INTEGER_WARN, 1)) + return NULL; + } + v = m->nb_int(v); + if (v == NULL) + return NULL; + if (!PyInt_Check(v) && !PyLong_Check(v)) { + PyErr_SetString(PyExc_TypeError, + "__int__ method returned non-integer"); + } + } + else { + PyErr_SetString(StructError, + "cannot convert argument to integer"); + return NULL; + } + } + else + /* Ensure we own a reference to v. */ Py_INCREF(v); - return v; + + if (PyInt_Check(v)) { + r = PyLong_FromLong(PyInt_AS_LONG(v)); + Py_DECREF(v); } - if (PyFloat_Check(v)) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 1)<0) - return NULL; - return PyNumber_Long(v); + else if (PyLong_Check(v)) { + assert(PyLong_Check(v)); + r = v; } - PyErr_SetString(StructError, - "cannot convert argument to long"); - return NULL; + else + assert(0); /* shouldn't ever get here */ + + return r; } /* Helper to convert a Python object to a C long. Sets an exception