]> git.saurik.com Git - wxWidgets.git/commitdiff
Instead of always using the Python default encoding for converting
authorRobin Dunn <robin@alldunn.com>
Wed, 15 Dec 2004 21:15:28 +0000 (21:15 +0000)
committerRobin Dunn <robin@alldunn.com>
Wed, 15 Dec 2004 21:15:28 +0000 (21:15 +0000)
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
wxPython/src/__core_rename.i
wxPython/src/_app.i
wxPython/src/helpers.cpp
wxPython/wxPython/_core.py

index e5c69c269925facba5bf46e53390ae7d6dd55ab6..ff605d8b4b566526da550cbb5c2102d6a61de9ca 100644 (file)
@@ -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);
 
index ecf3bc3964bf06129304b520f2eb89e12f2bbe48..d7bf1bf60dd50ab4d5288d8b56ad2d2936ed469a 100644 (file)
 %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;
index d90949ce139445b7385f7c04958e8c59f116ac25..3285fef3db12fb1590df3d361660b2f6cab3fcf4 100644 (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
 
index 694c5f478010dd2689e994ecf95855b638bd1f5a..ef5fb35e32d9c09e9e1e3dfa56d70bcb9612edfc 100644 (file)
@@ -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;
+}
+
 //----------------------------------------------------------------------
 
 
index 44705304182c4e46799f6b6523cb8713146a38b2..5a6bad7b56de55e8f3f05206152b80c1729a01e6 100644 (file)
@@ -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