diff Objects/unicodeobject.c @ 65348:453fd8182c2f

Use locale encoding if Py_FileSystemDefaultEncoding is not set * PyUnicode_EncodeFSDefault(), PyUnicode_DecodeFSDefaultAndSize() and PyUnicode_DecodeFSDefault() use the locale encoding instead of UTF-8 if Py_FileSystemDefaultEncoding is NULL * redecode_filenames() functions and _Py_code_object_list (issue #9630) are no more needed: remove them
author Victor Stinner <victor.stinner@haypocalc.com>
date Fri, 15 Oct 2010 12:04:23 +0000
parents fd044cda97d8
children 3794c9b50824
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Fri Oct 15 11:16:59 2010 +0000
+++ b/Objects/unicodeobject.c	Fri Oct 15 12:04:23 2010 +0000
@@ -1597,11 +1597,22 @@
                                          "surrogateescape");
     }
     else {
-        /* if you change the default encoding, update also
-           PyUnicode_DecodeFSDefaultAndSize() and redecode_filenames() */
-        return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
-                                    PyUnicode_GET_SIZE(unicode),
-                                    "surrogateescape");
+        /* locale encoding with surrogateescape */
+        wchar_t *wchar;
+        char *bytes;
+        PyObject *bytes_obj;
+
+        wchar = PyUnicode_AsWideCharString(unicode, NULL);
+        if (wchar == NULL)
+            return NULL;
+        bytes = _Py_wchar2char(wchar);
+        PyMem_Free(wchar);
+        if (bytes == NULL)
+            return NULL;
+
+        bytes_obj = PyBytes_FromString(bytes);
+        PyMem_Free(bytes);
+        return bytes_obj;
     }
 }
 
@@ -1769,9 +1780,22 @@
                                 "surrogateescape");
     }
     else {
-        /* if you change the default encoding, update also
-           PyUnicode_EncodeFSDefault() and redecode_filenames() */
-        return PyUnicode_DecodeUTF8(s, size, "surrogateescape");
+        /* locale encoding with surrogateescape */
+        wchar_t *wchar;
+        PyObject *unicode;
+
+        if (s[size] != '\0' || size != strlen(s)) {
+            PyErr_SetString(PyExc_TypeError, "embedded NUL character");
+            return NULL;
+        }
+
+        wchar = _Py_char2wchar(s);
+        if (wchar == NULL)
+            return NULL;
+
+        unicode = PyUnicode_FromWideChar(wchar, -1);
+        PyMem_Free(wchar);
+        return unicode;
     }
 }