+#if PYTHON_API_VERSION >= 1011
+
+// Prior to Python 2.2 PyMethod_GetClass returned the class object
+// in which the method was defined. Starting with 2.2 it returns
+// "class that asked for the method" which seems totally bogus to me
+// but apprently if fixes some obscure problem waiting to happen in
+// Python. Since the API was not documented Guido and the gang felt
+// safe in changing it. Needless to say that totally screwed up the
+// logic below in wxPyCallbackHelper::findCallback, hence this icky
+// code to find the class where the method is actuallt defined...
+
+static
+PyObject* PyFindClassWithAttr(PyObject *klass, PyObject *name)
+{
+ int i, n;
+
+ if (PyType_Check(klass)) { // new style classes
+ // This code is borrowed/adapted from _PyType_Lookup in typeobject.c
+ // (TODO: This part is not tested yet, so I'm not sure it is correct...)
+ PyTypeObject* type = (PyTypeObject*)klass;
+ PyObject *mro, *res, *base, *dict;
+ /* Look in tp_dict of types in MRO */
+ mro = type->tp_mro;
+ assert(PyTuple_Check(mro));
+ n = PyTuple_GET_SIZE(mro);
+ for (i = 0; i < n; i++) {
+ base = PyTuple_GET_ITEM(mro, i);
+ if (PyClass_Check(base))
+ dict = ((PyClassObject *)base)->cl_dict;
+ else {
+ assert(PyType_Check(base));
+ dict = ((PyTypeObject *)base)->tp_dict;
+ }
+ assert(dict && PyDict_Check(dict));
+ res = PyDict_GetItem(dict, name);
+ if (res != NULL)
+ return base;
+ }
+ return NULL;
+ }
+
+ else if (PyClass_Check(klass)) { // old style classes
+ // This code is borrowed/adapted from class_lookup in classobject.c
+ PyClassObject* cp = (PyClassObject*)klass;
+ PyObject *value = PyDict_GetItem(cp->cl_dict, name);
+ if (value != NULL) {
+ return (PyObject*)cp;
+ }
+ n = PyTuple_Size(cp->cl_bases);
+ for (i = 0; i < n; i++) {
+ PyObject* base = PyTuple_GetItem(cp->cl_bases, i);
+ PyObject *v = PyFindClassWithAttr(base, name);
+ if (v != NULL)
+ return v;
+ }
+ return NULL;
+ }
+}
+#endif
+
+
+static
+PyObject* PyMethod_GetDefiningClass(PyObject* method, const char* name)
+{
+ PyObject* mgc = PyMethod_GET_CLASS(method);
+
+#if PYTHON_API_VERSION <= 1010 // prior to Python 2.2, the easy way
+ return mgc;
+#else // 2.2 and after, the hard way...
+
+ PyObject* nameo = PyString_FromString(name);
+ PyObject* klass = PyFindClassWithAttr(mgc, nameo);
+ Py_DECREF(nameo);
+ return klass;
+#endif
+}
+
+
+