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
This commit is contained in:
Robin Dunn
2004-12-15 21:15:28 +00:00
parent e2ac17aa7a
commit 27c9e43cc0
5 changed files with 47 additions and 4 deletions

View File

@@ -91,6 +91,9 @@ void __wxPyCleanup();
PyObject* __wxPySetDictionary(PyObject*, PyObject* args); PyObject* __wxPySetDictionary(PyObject*, PyObject* args);
PyObject* __wxPyFixStockObjects(PyObject*, PyObject* args); PyObject* __wxPyFixStockObjects(PyObject*, PyObject* args);
void wxSetDefaultPyEncoding(const char* encoding);
const char* wxGetDefaultPyEncoding();
void wxPyEventThunker(wxObject*, wxEvent& event); void wxPyEventThunker(wxObject*, wxEvent& event);

View File

@@ -601,6 +601,8 @@
%rename(WakeUpIdle) wxWakeUpIdle; %rename(WakeUpIdle) wxWakeUpIdle;
%rename(PostEvent) wxPostEvent; %rename(PostEvent) wxPostEvent;
%rename(App_CleanUp) wxApp_CleanUp; %rename(App_CleanUp) wxApp_CleanUp;
%rename(SetDefaultPyEncoding) wxSetDefaultPyEncoding;
%rename(GetDefaultPyEncoding) wxGetDefaultPyEncoding;
%rename(EventLoop) wxEventLoop; %rename(EventLoop) wxEventLoop;
%rename(AcceleratorEntry) wxAcceleratorEntry; %rename(AcceleratorEntry) wxAcceleratorEntry;
%rename(AcceleratorTable) wxAcceleratorTable; %rename(AcceleratorTable) wxAcceleratorTable;

View File

@@ -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 // Include some extra wxApp related python code here

View File

@@ -78,6 +78,8 @@ wxPyThreadStateArray* wxPyTStates = NULL;
wxMutex* wxPyTMutex = NULL; wxMutex* wxPyTMutex = NULL;
#endif #endif
#define DEFAULTENCODING_SIZE 64
static char wxPyDefaultEncoding[DEFAULTENCODING_SIZE] = "ascii";
static PyObject* wxPython_dict = NULL; static PyObject* wxPython_dict = NULL;
static PyObject* wxPyAssertionError = NULL; static PyObject* wxPyAssertionError = NULL;
@@ -1903,7 +1905,7 @@ wxString* wxString_in_helper(PyObject* source) {
#if wxUSE_UNICODE #if wxUSE_UNICODE
PyObject* uni = source; PyObject* uni = source;
if (PyString_Check(source)) { if (PyString_Check(source)) {
uni = PyUnicode_FromObject(source); uni = PyUnicode_FromEncodedObject(source, wxPyDefaultEncoding, "strict");
if (PyErr_Occurred()) return NULL; if (PyErr_Occurred()) return NULL;
} }
target = new wxString(); target = new wxString();
@@ -1918,7 +1920,11 @@ wxString* wxString_in_helper(PyObject* source) {
#else #else
// Convert to a string object if it isn't already, then to wxString // Convert to a string object if it isn't already, then to wxString
PyObject* str = source; 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); str = PyObject_Str(source);
if (PyErr_Occurred()) return NULL; 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 // Convert to a unicode object, if not already, then to a wxString
PyObject* uni = source; PyObject* uni = source;
if (!PyUnicode_Check(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? if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear?
} }
size_t len = PyUnicode_GET_SIZE(uni); size_t len = PyUnicode_GET_SIZE(uni);
@@ -1957,7 +1963,11 @@ wxString Py2wxString(PyObject* source)
#else #else
// Convert to a string object if it isn't already, then to wxString // Convert to a string object if it isn't already, then to wxString
PyObject* str = source; 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); str = PyObject_Str(source);
if (PyErr_Occurred()) return wxEmptyString; // TODO: should we PyErr_Clear? 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;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@@ -967,6 +967,8 @@ wxWakeUpIdle = wx._core.WakeUpIdle
wxPostEvent = wx._core.PostEvent wxPostEvent = wx._core.PostEvent
wxApp_CleanUp = wx._core.App_CleanUp wxApp_CleanUp = wx._core.App_CleanUp
wxGetApp = wx._core.GetApp wxGetApp = wx._core.GetApp
wxSetDefaultPyEncoding = wx._core.SetDefaultPyEncoding
wxGetDefaultPyEncoding = wx._core.GetDefaultPyEncoding
wxEventLoop = wx._core.EventLoop wxEventLoop = wx._core.EventLoop
wxEventLoopPtr = wx._core.EventLoopPtr wxEventLoopPtr = wx._core.EventLoopPtr
wxEventLoop_GetActive = wx._core.EventLoop_GetActive wxEventLoop_GetActive = wx._core.EventLoop_GetActive