]> granicus.if.org Git - python/commitdiff
Invert the checks in get_[u]long and get_[u]longlong. The intent was
authorThomas Heller <theller@ctypes.org>
Thu, 24 Jan 2008 19:15:02 +0000 (19:15 +0000)
committerThomas Heller <theller@ctypes.org>
Thu, 24 Jan 2008 19:15:02 +0000 (19:15 +0000)
to not accept float types; the result was that integer-like objects
were not accepted.

Ported from release25-maint.

Lib/ctypes/test/test_numbers.py
Modules/_ctypes/cfield.c

index ff2da86ea0259640e932f331df73eda0642e7311..584574132708fe5c3e530f380f82a0594933b70a 100644 (file)
@@ -105,15 +105,31 @@ class NumberTestCase(unittest.TestCase):
     def test_floats(self):
         # c_float and c_double can be created from
         # Python int, long and float
+        class FloatLike(object):
+            def __float__(self):
+                return 2.0
+        f = FloatLike()
         for t in float_types:
             self.failUnlessEqual(t(2.0).value, 2.0)
             self.failUnlessEqual(t(2).value, 2.0)
             self.failUnlessEqual(t(2L).value, 2.0)
+            self.failUnlessEqual(t(f).value, 2.0)
 
     def test_integers(self):
-        # integers cannot be constructed from floats
+        class FloatLike(object):
+            def __float__(self):
+                return 2.0
+        f = FloatLike()
+        class IntLike(object):
+            def __int__(self):
+                return 2
+        i = IntLike()
+        # integers cannot be constructed from floats,
+        # but from integer-like objects
         for t in signed_types + unsigned_types:
             self.assertRaises(TypeError, t, 3.14)
+            self.assertRaises(TypeError, t, f)
+            self.failUnlessEqual(t(i).value, 2)
 
     def test_sizes(self):
         for t in signed_types + unsigned_types + float_types + bool_types:
index 03aacbd2eb32806843c57c643109cd4072cf29b1..155e75efa44f91097b6d8152f0c7629077856139 100644 (file)
@@ -346,10 +346,9 @@ static int
 get_long(PyObject *v, long *p)
 {
        long x;
-       if (!PyInt_Check(v) && !PyLong_Check(v)) {
-               PyErr_Format(PyExc_TypeError,
-                            "int expected instead of %s instance",
-                            v->ob_type->tp_name);
+       if (PyFloat_Check(v)) {
+               PyErr_SetString(PyExc_TypeError,
+                               "int expected instead of float");
                return -1;
        }
        x = PyInt_AsUnsignedLongMask(v);
@@ -365,10 +364,9 @@ static int
 get_ulong(PyObject *v, unsigned long *p)
 {
        unsigned long x;
-       if (!PyInt_Check(v) && !PyLong_Check(v)) {
-               PyErr_Format(PyExc_TypeError,
-                            "int expected instead of %s instance",
-                            v->ob_type->tp_name);
+       if (PyFloat_Check(v)) {
+               PyErr_SetString(PyExc_TypeError,
+                               "int expected instead of float");
                return -1;
        }
        x = PyInt_AsUnsignedLongMask(v);
@@ -386,11 +384,10 @@ static int
 get_longlong(PyObject *v, PY_LONG_LONG *p)
 {
        PY_LONG_LONG x;
-       if (!PyInt_Check(v) && !PyLong_Check(v)) {
-               PyErr_Format(PyExc_TypeError,
-                            "int expected instead of %s instance",
-                            v->ob_type->tp_name);
-               return -1;
+       if (PyFloat_Check(v)) {
+               PyErr_SetString(PyExc_TypeError,
+                               "int expected instead of float");
+               return -1;
        }
        x = PyInt_AsUnsignedLongLongMask(v);
        if (x == -1 && PyErr_Occurred())
@@ -405,12 +402,11 @@ static int
 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
 {
        unsigned PY_LONG_LONG x;
-       if (!PyInt_Check(v) && !PyLong_Check(v)) {
-               PyErr_Format(PyExc_TypeError,
-                            "int expected instead of %s instance",
-                            v->ob_type->tp_name);
-               return -1;
-       }
+       if (PyFloat_Check(v)) {
+               PyErr_SetString(PyExc_TypeError,
+                               "int expected instead of float");
+               return -1;
+       }
        x = PyInt_AsUnsignedLongLongMask(v);
        if (x == -1 && PyErr_Occurred())
                return -1;