+//---------------------------------------------------------------------------
+
+static unsigned int _wxPyNestCount = 0;
+
+static PyThreadState* myPyThreadState_Get() {
+ PyThreadState* current;
+ current = PyThreadState_Swap(NULL);
+ PyThreadState_Swap(current);
+ return current;
+}
+
+
+HELPEREXPORT bool wxPyRestoreThread() {
+ // NOTE: The Python API docs state that if a thread already has the
+ // interpreter lock and calls PyEval_RestoreThread again a deadlock
+ // occurs, so I put in this code as a guard condition since there are
+ // many possibilites for nested events and callbacks in wxPython. If
+ // The current thread is our thread, then we can assume that we
+ // already have the lock. (I hope!)
+ //
+#ifdef WXP_WITH_THREAD
+ _wxPyNestCount += 1;
+ if (wxPyEventThreadState != myPyThreadState_Get()) {
+ PyEval_RestoreThread(wxPyEventThreadState);
+ return TRUE;
+ }
+ else
+#endif
+ return FALSE;
+}
+
+
+HELPEREXPORT void wxPySaveThread(bool doSave) {
+#ifdef WXP_WITH_THREAD
+ if (doSave) {
+ wxPyEventThreadState = PyEval_SaveThread();
+ }
+ _wxPyNestCount -= 1;
+#endif
+}
+
+//---------------------------------------------------------------------------
+
+
+IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject);
+
+wxPyCallback::wxPyCallback(PyObject* func) {
+ m_func = func;
+ Py_INCREF(m_func);
+}
+
+wxPyCallback::wxPyCallback(const wxPyCallback& other) {
+ m_func = other.m_func;
+ Py_INCREF(m_func);
+}
+
+wxPyCallback::~wxPyCallback() {
+ bool doSave = wxPyRestoreThread();
+ Py_DECREF(m_func);
+ wxPySaveThread(doSave);
+}
+
+