diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-12-21 14:45:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-21 14:45:16 (GMT) |
commit | e47e698da6bd982da277960c14afa9d9939e3155 (patch) | |
tree | dd6a4bee16d8c66533f6a6c3f71f24f39caafd35 /Python/fileutils.c | |
parent | fbd605151fcf2899b14575f4ddb9ce3c55e684ab (diff) | |
download | cpython-e47e698da6bd982da277960c14afa9d9939e3155.zip cpython-e47e698da6bd982da277960c14afa9d9939e3155.tar.gz cpython-e47e698da6bd982da277960c14afa9d9939e3155.tar.bz2 |
bpo-32030: Add _Py_EncodeUTF8_surrogateescape() (#4960)
Py_EncodeLocale() now uses _Py_EncodeUTF8_surrogateescape(), instead
of using temporary unicode and bytes objects. So Py_EncodeLocale()
doesn't use the Python C API anymore.
Diffstat (limited to 'Python/fileutils.c')
-rw-r--r-- | Python/fileutils.c | 42 |
1 files changed, 4 insertions, 38 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c index c4d495d..eeb5f2e 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -22,6 +22,8 @@ extern int winerror_to_errno(int); extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size, size_t *p_wlen); +extern char* _Py_EncodeUTF8_surrogateescape(const wchar_t *text, + size_t *error_pos); #ifdef O_CLOEXEC /* Does open() support the O_CLOEXEC flag? Possible values: @@ -418,42 +420,6 @@ Py_DecodeLocale(const char* arg, size_t *size) #endif /* __APPLE__ or __ANDROID__ */ } -static char* -_Py_EncodeLocaleUTF8(const wchar_t *text, size_t *error_pos) -{ - Py_ssize_t len; - PyObject *unicode, *bytes = NULL; - char *cpath; - - unicode = PyUnicode_FromWideChar(text, wcslen(text)); - if (unicode == NULL) { - return NULL; - } - - bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape"); - Py_DECREF(unicode); - if (bytes == NULL) { - PyErr_Clear(); - if (error_pos != NULL) { - *error_pos = (size_t)-1; - } - return NULL; - } - - len = PyBytes_GET_SIZE(bytes); - cpath = PyMem_Malloc(len+1); - if (cpath == NULL) { - PyErr_Clear(); - Py_DECREF(bytes); - if (error_pos != NULL) { - *error_pos = (size_t)-1; - } - return NULL; - } - memcpy(cpath, PyBytes_AsString(bytes), len + 1); - Py_DECREF(bytes); - return cpath; -} #if !defined(__APPLE__) && !defined(__ANDROID__) static char* @@ -537,10 +503,10 @@ char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos) { #if defined(__APPLE__) || defined(__ANDROID__) - return _Py_EncodeLocaleUTF8(text, error_pos); + return _Py_EncodeUTF8_surrogateescape(text, error_pos); #else /* __APPLE__ */ if (Py_UTF8Mode == 1) { - return _Py_EncodeLocaleUTF8(text, error_pos); + return _Py_EncodeUTF8_surrogateescape(text, error_pos); } #ifndef MS_WINDOWS |