X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ab897a69db8ef70debb8841dcfd66cafb06b910c..42f8298f6f42d5d63bb3caf65682b7d9d9f8b702:/wxPython/src/helpers.cpp diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index c10641926d..512b685baa 100644 --- a/wxPython/src/helpers.cpp +++ b/wxPython/src/helpers.cpp @@ -78,6 +78,8 @@ wxPyThreadStateArray* wxPyTStates = NULL; wxMutex* wxPyTMutex = NULL; #endif +#define DEFAULTENCODING_SIZE 64 +static char wxPyDefaultEncoding[DEFAULTENCODING_SIZE] = "ascii"; static PyObject* wxPython_dict = NULL; static PyObject* wxPyAssertionError = NULL; @@ -864,10 +866,8 @@ bool wxPyCheckForApp() { //--------------------------------------------------------------------------- - -void wxPyClientData_dtor(wxPyClientData* self) { - if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python - // may have already garbage collected the object... +void wxPyUserData_dtor(wxPyUserData* self) { + if (! wxPyDoingCleanup) { bool blocked = wxPyBeginBlockThreads(); Py_DECREF(self->m_obj); self->m_obj = NULL; @@ -875,16 +875,21 @@ void wxPyClientData_dtor(wxPyClientData* self) { } } -void wxPyUserData_dtor(wxPyUserData* self) { - if (! wxPyDoingCleanup) { - bool blocked = wxPyBeginBlockThreads(); - Py_DECREF(self->m_obj); + +void wxPyClientData_dtor(wxPyClientData* self) { + if (! wxPyDoingCleanup) { // Don't do it during cleanup as Python + // may have already garbage collected the object... + if (self->m_incRef) { + bool blocked = wxPyBeginBlockThreads(); + Py_DECREF(self->m_obj); + wxPyEndBlockThreads(blocked); + } self->m_obj = NULL; - wxPyEndBlockThreads(blocked); } } + // This is called when an OOR controled object is being destroyed. Although // the C++ object is going away there is no way to force the Python object // (and all references to it) to die too. This causes problems (crashes) in @@ -907,8 +912,9 @@ void wxPyOORClientData_dtor(wxPyOORClientData* self) { } - // Only if there is more than one reference to the object - if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 ) { + // Only if there is more than one reference to the object and we are + // holding the OOR reference: + if ( !wxPyDoingCleanup && self->m_obj->ob_refcnt > 1 && self->m_incRef) { // bool isInstance = wxPyInstance_Check(self->m_obj); // TODO same here //wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!")); @@ -1184,7 +1190,8 @@ bool wxPyInputStream::eof() { } wxPyInputStream::~wxPyInputStream() { - /* do nothing */ + if (m_wxis) + delete m_wxis; } @@ -1429,14 +1436,20 @@ size_t wxPyCBInputStream::OnSysWrite(const void *buffer, size_t bufsize) { return 0; } + wxFileOffset wxPyCBInputStream::OnSysSeek(wxFileOffset off, wxSeekMode mode) { bool blocked = wxPyBeginBlockThreads(); -#if defined( __WINCE__) || defined(_LARGE_FILES) || wxHAS_HUGE_FILES - // wxFileOffset is a 64-bit value... - PyObject* arglist = Py_BuildValue("(Li)", off, mode); -#else - PyObject* arglist = Py_BuildValue("(ii)", off, mode); -#endif + PyObject* arglist = PyTuple_New(2); + + if (sizeof(wxFileOffset) > sizeof(long)) + // wxFileOffset is a 64-bit value... + PyTuple_SET_ITEM(arglist, 0, PyLong_FromLongLong(off)); + else + PyTuple_SET_ITEM(arglist, 0, PyInt_FromLong(off)); + + PyTuple_SET_ITEM(arglist, 1, PyInt_FromLong(mode)); + + PyObject* result = PyEval_CallObject(m_seek, arglist); Py_DECREF(arglist); Py_XDECREF(result); @@ -1452,11 +1465,9 @@ wxFileOffset wxPyCBInputStream::OnSysTell() const { Py_DECREF(arglist); wxFileOffset o = 0; if (result != NULL) { -#if defined( __WINCE__) || defined(_LARGE_FILES) || wxHAS_HUGE_FILES if (PyLong_Check(result)) o = PyLong_AsLongLong(result); else -#endif o = PyInt_AsLong(result); Py_DECREF(result); }; @@ -1466,7 +1477,7 @@ wxFileOffset wxPyCBInputStream::OnSysTell() const { //---------------------------------------------------------------------- -IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxObject); +IMPLEMENT_ABSTRACT_CLASS(wxPyCallback, wxEvtHandler); wxPyCallback::wxPyCallback(PyObject* func) { m_func = func; @@ -1898,7 +1909,7 @@ wxString* wxString_in_helper(PyObject* source) { #if wxUSE_UNICODE PyObject* uni = source; if (PyString_Check(source)) { - uni = PyUnicode_FromObject(source); + uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict"); if (PyErr_Occurred()) return NULL; } target = new wxString(); @@ -1913,7 +1924,11 @@ wxString* wxString_in_helper(PyObject* source) { #else // Convert to a string object if it isn't already, then to wxString PyObject* str = source; - if (!PyString_Check(source)) { + if (PyUnicode_Check(source)) { + str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); + if (PyErr_Occurred()) return NULL; + } + else if (!PyString_Check(source)) { str = PyObject_Str(source); if (PyErr_Occurred()) return NULL; } @@ -1938,7 +1953,7 @@ wxString Py2wxString(PyObject* source) // Convert to a unicode object, if not already, then to a wxString PyObject* uni = source; if (!PyUnicode_Check(source)) { - uni = PyUnicode_FromObject(source); + uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict"); if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? } size_t len = PyUnicode_GET_SIZE(uni); @@ -1952,7 +1967,11 @@ wxString Py2wxString(PyObject* source) #else // Convert to a string object if it isn't already, then to wxString PyObject* str = source; - if (!PyString_Check(source)) { + if (PyUnicode_Check(source)) { + str = PyUnicode_AsEncodedString(source, wxPyDefaultEncoding, "strict"); + if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? + } + else if (!PyString_Check(source)) { str = PyObject_Str(source); if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? } @@ -1981,6 +2000,17 @@ PyObject* wx2PyString(const wxString& src) } + +void wxSetDefaultPyEncoding(const char* encoding) +{ + strncpy(wxPyDefaultEncoding, encoding, DEFAULTENCODING_SIZE); +} + +const char* wxGetDefaultPyEncoding() +{ + return wxPyDefaultEncoding; +} + //----------------------------------------------------------------------