From d1ec23fe46f2d95655fbeb7b78fbd5e9f1d2c6d3 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Thu, 30 Mar 2017 16:53:33 +0200 Subject: [PATCH] [PY3] Rewriting of Py_EncodeLocale and Py_DecodeLocale (python < 3.5) --- src/SUITApp/SUITApp_init_python.cxx | 28 ++++++------------------- tools/PyInterp/src/PyInterp_Interp.cxx | 29 -------------------------- tools/PyInterp/src/PyInterp_Utils.h | 13 ++++++++++++ 3 files changed, 19 insertions(+), 51 deletions(-) diff --git a/src/SUITApp/SUITApp_init_python.cxx b/src/SUITApp/SUITApp_init_python.cxx index 7f5d7021c..3724dd562 100644 --- a/src/SUITApp/SUITApp_init_python.cxx +++ b/src/SUITApp/SUITApp_init_python.cxx @@ -24,31 +24,15 @@ #include #if PY_VERSION_HEX < 0x03050000 +static char* +Py_EncodeLocale(const wchar_t *arg, size_t *size) +{ + return _Py_wchar2char(arg, size); +} static wchar_t* Py_DecodeLocale(const char *arg, size_t *size) { - wchar_t *res; - unsigned char *in; - wchar_t *out; - size_t argsize = strlen(arg) + 1; - - if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) - return NULL; - res = (wchar_t*) PyMem_RawMalloc(argsize*sizeof(wchar_t)); - if (!res) - return NULL; - - in = (unsigned char*)arg; - out = res; - while(*in) - if(*in < 128) - *out++ = *in++; - else - *out++ = 0xdc00 + *in++; - *out = 0; - if (size != NULL) - *size = out - res; - return res; + return _Py_char2wchar(arg, size); } #endif diff --git a/tools/PyInterp/src/PyInterp_Interp.cxx b/tools/PyInterp/src/PyInterp_Interp.cxx index 4b8f8fe76..27703c2a0 100644 --- a/tools/PyInterp/src/PyInterp_Interp.cxx +++ b/tools/PyInterp/src/PyInterp_Interp.cxx @@ -40,35 +40,6 @@ #define TOP_HISTORY_PY "--- top of history ---" #define BEGIN_HISTORY_PY "--- begin of history ---" -#if PY_VERSION_HEX < 0x03050000 -static wchar_t* -Py_DecodeLocale(const char *arg, size_t *size) -{ - wchar_t *res; - unsigned char *in; - wchar_t *out; - size_t argsize = strlen(arg) + 1; - - if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t)) - return NULL; - res = (wchar_t*) PyMem_RawMalloc(argsize*sizeof(wchar_t)); - if (!res) - return NULL; - - in = (unsigned char*)arg; - out = res; - while(*in) - if(*in < 128) - *out++ = *in++; - else - *out++ = 0xdc00 + *in++; - *out = 0; - if (size != NULL) - *size = out - res; - return res; -} -#endif - /* The following functions are used to hook the Python interpreter output. diff --git a/tools/PyInterp/src/PyInterp_Utils.h b/tools/PyInterp/src/PyInterp_Utils.h index d47ff1846..7ad22d962 100644 --- a/tools/PyInterp/src/PyInterp_Utils.h +++ b/tools/PyInterp/src/PyInterp_Utils.h @@ -31,6 +31,19 @@ #include #endif +#if PY_VERSION_HEX < 0x03050000 +static char* +Py_EncodeLocale(const wchar_t *arg, size_t *size) +{ + return _Py_wchar2char(arg, size); +} +static wchar_t* +Py_DecodeLocale(const char *arg, size_t *size) +{ + return _Py_char2wchar(arg, size); +} +#endif + /** * \class PyLockWrapper * \brief Python GIL wrapper. -- 2.39.2