From 27c9e43cc09bb6ba136a5a3897232d73df1e878f Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Wed, 15 Dec 2004 21:15:28 +0000 Subject: [PATCH] Instead of always using the Python default encoding for converting string and unicode objects to/from wxStrings. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@31022 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/include/wx/wxPython/wxPython_int.h | 3 +++ wxPython/src/__core_rename.i | 2 ++ wxPython/src/_app.i | 15 +++++++++++ wxPython/src/helpers.cpp | 29 ++++++++++++++++++--- wxPython/wxPython/_core.py | 2 ++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/wxPython/include/wx/wxPython/wxPython_int.h b/wxPython/include/wx/wxPython/wxPython_int.h index e5c69c2699..ff605d8b4b 100644 --- a/wxPython/include/wx/wxPython/wxPython_int.h +++ b/wxPython/include/wx/wxPython/wxPython_int.h @@ -91,6 +91,9 @@ void __wxPyCleanup(); PyObject* __wxPySetDictionary(PyObject*, PyObject* args); PyObject* __wxPyFixStockObjects(PyObject*, PyObject* args); +void wxSetDefaultPyEncoding(const char* encoding); +const char* wxGetDefaultPyEncoding(); + void wxPyEventThunker(wxObject*, wxEvent& event); diff --git a/wxPython/src/__core_rename.i b/wxPython/src/__core_rename.i index ecf3bc3964..d7bf1bf60d 100644 --- a/wxPython/src/__core_rename.i +++ b/wxPython/src/__core_rename.i @@ -601,6 +601,8 @@ %rename(WakeUpIdle) wxWakeUpIdle; %rename(PostEvent) wxPostEvent; %rename(App_CleanUp) wxApp_CleanUp; +%rename(SetDefaultPyEncoding) wxSetDefaultPyEncoding; +%rename(GetDefaultPyEncoding) wxGetDefaultPyEncoding; %rename(EventLoop) wxEventLoop; %rename(AcceleratorEntry) wxAcceleratorEntry; %rename(AcceleratorTable) wxAcceleratorTable; diff --git a/wxPython/src/_app.i b/wxPython/src/_app.i index d90949ce13..3285fef3db 100644 --- a/wxPython/src/_app.i +++ b/wxPython/src/_app.i @@ -355,6 +355,21 @@ DocDeclStrName( + + +DocDeclAStr( + void , wxSetDefaultPyEncoding(const char* encoding), + "SetDefaultPyEncoding(string encoding)", + "Sets the encoding that wxPython will use when it needs to convert a +Python string or unicode object to or from a wxString.", ""); + +DocDeclAStr( + const char* , wxGetDefaultPyEncoding(), + "GetDefaultPyEncoding() -> string", + "Gets the current encoding that wxPython will use when it needs to +convert a Python string or unicode object to or from a wxString.", ""); + + //--------------------------------------------------------------------------- // Include some extra wxApp related python code here diff --git a/wxPython/src/helpers.cpp b/wxPython/src/helpers.cpp index 694c5f4780..ef5fb35e32 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; @@ -1903,7 +1905,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(); @@ -1918,7 +1920,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; } @@ -1943,7 +1949,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); @@ -1957,7 +1963,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? } @@ -1986,6 +1996,17 @@ PyObject* wx2PyString(const wxString& src) } + +void wxSetDefaultPyEncoding(const char* encoding) +{ + strncpy(wxPyDefaultEncoding, encoding, DEFAULTENCODING_SIZE); +} + +const char* wxGetDefaultPyEncoding() +{ + return wxPyDefaultEncoding; +} + //---------------------------------------------------------------------- diff --git a/wxPython/wxPython/_core.py b/wxPython/wxPython/_core.py index 4470530418..5a6bad7b56 100644 --- a/wxPython/wxPython/_core.py +++ b/wxPython/wxPython/_core.py @@ -967,6 +967,8 @@ wxWakeUpIdle = wx._core.WakeUpIdle wxPostEvent = wx._core.PostEvent wxApp_CleanUp = wx._core.App_CleanUp wxGetApp = wx._core.GetApp +wxSetDefaultPyEncoding = wx._core.SetDefaultPyEncoding +wxGetDefaultPyEncoding = wx._core.GetDefaultPyEncoding wxEventLoop = wx._core.EventLoop wxEventLoopPtr = wx._core.EventLoopPtr wxEventLoop_GetActive = wx._core.EventLoop_GetActive -- 2.45.2