bh: thuban/libraries/thuban wxproj.cpp, 1.5, 1.6 swigPtrConvertHack.h, 1.1, 1.2
cvs@intevation.de
cvs at intevation.de
Tue Apr 5 19:27:00 CEST 2005
Author: bh
Update of /thubanrepository/thuban/libraries/thuban
In directory doto:/tmp/cvs-serv24411/libraries/thuban
Modified Files:
wxproj.cpp swigPtrConvertHack.h
Log Message:
Use wxPython.h by default but provide a workaround when it isn't
available.
* setup.py (wxproj_extension): New variable. Assign the Extension
instance for Lib.wxproj to this variable instead of putting it
directly into the listso that it can be accessed by other code
later.
(thuban_build_ext.user_options): Added boolean option
--use-wx-python-swig-hack.
(thuban_build_ext.initialize_options): Initialize the new option
to False.
(thuban_build_ext.finalize_options): If the new option was given,
define the preprocesser macro USE_WX_PYTHON_SWIG_HACK.
(thuban_build_ext): Update the doc-string
* libraries/thuban/wxproj.cpp: Normally we use
wx/wxPython/wxPython.h now. Only if USE_WX_PYTHON_SWIG_HACK is
defined, use swigPtrConvertHack.h instead.
* libraries/thuban/swigPtrConvertHack.h: Remove the code that was
copied over from wxPython.h.
(decode_pointer_new): New. Equivalent of decode_pointer for
wxPython 2.5.
(wxPyConvertSwigPtr): Modified to cope with wxPython 2.5 as well.
Index: wxproj.cpp
===================================================================
RCS file: /thubanrepository/thuban/libraries/thuban/wxproj.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- wxproj.cpp 16 Feb 2005 21:14:47 -0000 1.5
+++ wxproj.cpp 5 Apr 2005 17:26:58 -0000 1.6
@@ -1,4 +1,4 @@
-/* Copyright (c) 2001, 2002, 2003, 2004 by Intevation GmbH
+/* Copyright (c) 2001, 2002, 2003, 2004, 2005 by Intevation GmbH
* Authors:
* Bernhard Herzog <bh at intevation.de>
*
@@ -31,11 +31,17 @@
#include <wx/wx.h>
-/* For wx2.5, we cannot just extract the address of the object from the
- * Python string repr; we need to go through the Swig mechanisms
+/* By default we use the wxPython.h API to access wxPython's objects at
+ * the C++ level. On some systems that's not available, so we offer a
+ * work-around with swigPtrConvertHack.h, but we only do so if the
+ * compiler explicitly defines USE_WX_PYTHON_SWIG_HACK.
*/
+#ifndef USE_WX_PYTHON_SWIG_HACK
+#include <wx/wxPython/wxPython.h>
+#else
#include "swigPtrConvertHack.h"
-//#include <wx/wxPython/wxPython.h>
+#endif
+
/* We only need this prototype from the above (big) include:
*
* bool wxPyConvertSwigPtr( PyObject* obj, void **ptr, const wxChar* className);
Index: swigPtrConvertHack.h
===================================================================
RCS file: /thubanrepository/thuban/libraries/thuban/swigPtrConvertHack.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- swigPtrConvertHack.h 16 Feb 2005 21:14:47 -0000 1.1
+++ swigPtrConvertHack.h 5 Apr 2005 17:26:58 -0000 1.2
@@ -3,34 +3,8 @@
#include <wx/wx.h>
-
-#if wxCHECK_VERSION(2,5,3)
-
-#define wxPyUSE_EXPORTED_API
-#include "wxPython_int.h"
-static bool wxPyCoreAPI_IMPORT()
-{
- wxPyCoreAPIPtr=(wxPyCoreAPI*)PyCObject_Import("wx._core_", "_wxPyCoreAPI");
-
- if (! wxPyCoreAPIPtr)
- wxPyCoreAPIPtr=(wxPyCoreAPI*)PyCObject_Import("_core_", "_wxPyCoreAPI");
- return wxPyCoreAPIPtr != NULL;
-}
-
-// Used by the macros below to fetch the API pointer, importing it first if
-// needed. This should never need to be called directly.
-inline wxPyCoreAPI* wxPyGetCoreAPIPtr()
-{
- if (wxPyCoreAPIPtr == NULL) wxPyCoreAPI_IMPORT();
- // wxASSERT_MSW(wxPyCoreAPIPtr != NULL, wxT("wxPyCoreAPIPtr is NULL!!!")); // uncomment when needed for debugging
- return wxPyCoreAPIPtr;
-}
-
-#define wxPyConvertSwigPtr(a,b,c) (wxPyGetCoreAPIPtr()->p_wxPyConvertSwigPtr(a,b,c))
-
-#else
-
/* helper function to extract the pointer value from a swig ptr string
+ * as used in wxPython 2.4
*/
static void *
decode_pointer(char *string)
@@ -56,22 +30,94 @@
return (void*)p;
}
-/* Return the object wrapped by the SWIG shadow object shadow */
-bool wxPyConvertSwigPtr( PyObject* obj, void **ptr, const wxChar* className)
+/* helper function to extract the pointer value from a swig ptr string
+ * as used in wxPython 2.5
+ *
+ * The hexvalues describe the bytes making up the pointer in memory,
+ * i.e. when the pointer is considered as an array of bytes (we assume
+ * char==byte in the implementation). In the older version it was
+ * always a bigendian representation.
+ */
+static void*
+decode_pointer_new(char *string, unsigned int length)
{
- PyObject * string;
+ void * p = 0;
+ unsigned char *pp = (unsigned char*)&p;
+ unsigned int count = 0;
+
+ /* sanity check: the string must be at least as long as twice the
+ * size of the pointer (two hex digits per byte) plus one for the
+ * initial underscore */
+ if (length < 2 * sizeof(p) + 1)
+ return NULL;
+
+ /* Pointer values must start with leading underscore */
+ if (*string == '_')
+ {
+ string++;
+ length--;
+
+ /* the remaining length must be at least twice as long as the
+ * size of the pointer. */
+
+ /* now decode the hex values. We decode exactly two bytes for
+ * each of the bytes in the pointer */
+ while (count < 2 * sizeof(p))
+ {
+ if ((*string >= '0') && (*string <= '9'))
+ *pp = (*pp << 4) + (*string - '0');
+ else if ((*string >= 'a') && (*string <= 'f'))
+ *pp = (*pp << 4) + ((*string - 'a') + 10);
+ else
+ break;
+ string++;
+ count++;
+ if (count % 2 == 0)
+ pp++;
+ }
+ }
+ return p;
+}
+
+/* Return the object wrapped by the SWIG shadow object shadow
+ *
+ * The pointer to the real object can be decoded from the "this"
+ * attribute of the python object. In wxPython 2.4 it's a string
+ * containing a hex representation of the pointer value. In wxPython
+ * 2.5 it's a special python object which can nevertheless be converted
+ * to a string with a hex representation of the pointer value which is a
+ * bit different than the one for 2.4.
+ */
+bool
+wxPyConvertSwigPtr(PyObject* obj, void **ptr, const wxChar* className)
+{
+ PyObject * string = NULL;
+ PyObject * thisobject = NULL;
+
*ptr = 0;
- string = PyObject_GetAttrString(obj, "this");
- if (string && PyString_Check(string))
+ thisobject = PyObject_GetAttrString(obj, "this");
+ if (thisobject)
{
- *ptr = decode_pointer(PyString_AsString(string));
+ if (!PyString_Check(thisobject))
+ {
+ string = PyObject_Str(thisobject);
+ if (string)
+ {
+ *ptr = decode_pointer_new(PyString_AsString(string),
+ PyString_Size(string));
+ Py_DECREF(string);
+ }
+ }
+ else
+ {
+ *ptr = decode_pointer(PyString_AsString(thisobject));
+ }
}
- Py_XDECREF(string);
+
+ Py_XDECREF(thisobject);
return *ptr != 0;
}
-
-#endif
#endif
More information about the Thuban-devel
mailing list
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)