[C++-sig] strange behavior with respect to numeric and Booleanoverloads

troy d. straszheim troy at resophonic.com
Tue Mar 17 19:06:06 CET 2009


I have a quasi-fix for this to the library itself, (see diff below) 
buuut it breaks a certain case:  if you have a wrapped c++ function that 
takes a bool, and you try to pass it an int, you get a

     ArgumentError: Python argument types in
         builtin_converters_ext.rewrap_const_reference_bool(int)
     did not match C++ signature:
         rewrap_const_reference_bool(bool)

so you have to call the function with an explicit cast to bool:

c++:

   void takes_a_bool(bool b) { ... };

   def("py_takesbool", takes_a_bool);

py:

   py_takesbool(bool(33))

to get things to work.   This breaks a lot of tests, I'm digging further.


Index: converter/builtin_converters.cpp
===================================================================
--- converter/builtin_converters.cpp	(revision 51812)
+++ converter/builtin_converters.cpp	(working copy)
@@ -99,8 +99,13 @@
            if (number_methods == 0)
                return 0;

-          return (PyInt_Check(obj) || PyLong_Check(obj))
-              ? &number_methods->nb_int : 0;
+          return (
+#if PY_VERSION_HEX >= 0x02040000
+		  !PyBool_Check(obj) &&
+#endif
+		  (PyInt_Check(obj) || PyLong_Check(obj)))
+
+	    ? &number_methods->nb_int : 0;
        }
        static PyTypeObject const* get_pytype() { return &PyInt_Type;}
    };
@@ -135,7 +140,11 @@
            if (number_methods == 0)
                return 0;

-          return (PyInt_Check(obj) || PyLong_Check(obj))
+          return (
+#if PY_VERSION_HEX >= 0x02040000
+		  !PyBool_Check(obj) &&
+#endif
+		  (PyInt_Check(obj) || PyLong_Check(obj)))
                ? &py_object_identity : 0;
        }
        static PyTypeObject const* get_pytype() { return &PyInt_Type;}
@@ -226,7 +235,11 @@
    {
        static unaryfunc* get_slot(PyObject* obj)
        {
+#if PY_VERSION_HEX >= 0x02040000
+	return obj == Py_None || PyBool_Check(obj) ? &py_object_identity : 0;
+#else
            return obj == Py_None || PyInt_Check(obj) ? 
&py_object_identity : 0;
+#endif
        }

        static bool extract(PyObject* intermediate)


More information about the Cplusplus-sig mailing list