]> granicus.if.org Git - python/commitdiff
Issue #8715: Create PyUnicode_EncodeFSDefault() function: Encode a Unicode
authorVictor Stinner <victor.stinner@haypocalc.com>
Sat, 15 May 2010 16:27:27 +0000 (16:27 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sat, 15 May 2010 16:27:27 +0000 (16:27 +0000)
object to Py_FileSystemDefaultEncoding with the "surrogateescape" error
handler, return a bytes object. If Py_FileSystemDefaultEncoding is not set,
fall back to UTF-8.

Doc/c-api/unicode.rst
Include/unicodeobject.h
Misc/NEWS
Modules/_io/fileio.c
Modules/_tkinter.c
Modules/grpmodule.c
Modules/pwdmodule.c
Modules/spwdmodule.c
Objects/unicodeobject.c
Python/import.c

index 6e163d6ec7406846e912815e3cdfd032632c9818..4222a054a91308e354fabaa5c643174dcbe5ea08 100644 (file)
@@ -396,6 +396,7 @@ used, passsing :func:PyUnicode_FSConverter as the conversion function:
 
    Use :func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
 
+
 .. cfunction:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
 
    Decode a string using :cdata:`Py_FileSystemDefaultEncoding` and
@@ -404,6 +405,16 @@ used, passsing :func:PyUnicode_FSConverter as the conversion function:
    If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
 
 
+.. cfunction:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
+
+   Encode a Unicode object to :cdata:`Py_FileSystemDefaultEncoding` with the
+   ``'surrogateescape'`` error handler, return a :func:`bytes` object.
+
+   If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
+
+   .. versionadded:: 3.2
+
+
 wchar_t Support
 """""""""""""""
 
index 383187b1ccf58393c9530c93d801fa951ca06356..ddc9000828f4d9ef1fafaa2b10c67448836b16b7 100644 (file)
@@ -1268,6 +1268,16 @@ PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
     Py_ssize_t size              /* size */
     );
 
+/* Encode a Unicode object to Py_FileSystemDefaultEncoding with the
+   "surrogateescape" error handler, return a bytes object.
+
+   If Py_FileSystemDefaultEncoding is not set, fall back to UTF-8.
+*/
+
+PyAPI_FUNC(PyObject*) PyUnicode_EncodeFSDefault(
+    PyObject *unicode
+    );
+
 /* --- Methods & Slots ----------------------------------------------------
 
    These are capable of handling Unicode objects and strings on input
index 31a063de9188de4e6e7e590278a92c61b513f1c9..3da54ab4f73640f9a83db2f2e2c24009928ea568 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #8715: Create PyUnicode_EncodeFSDefault() function: Encode a Unicode
+  object to Py_FileSystemDefaultEncoding with the "surrogateescape" error
+  handler, return a bytes object. If Py_FileSystemDefaultEncoding is not set,
+  fall back to UTF-8.
+
 - Enable shortcuts for common encodings in PyUnicode_AsEncodedString() for any
   error handler, not only the default error handler (strict)
 
index 6ecce1b520d6a07083f83530648f91f0b417043d..4f450da636a813ca732e2294eb5454c602e677a2 100644 (file)
@@ -247,8 +247,7 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds)
             if (u == NULL)
                 return -1;
 
-            stringobj = PyUnicode_AsEncodedString(
-                u, Py_FileSystemDefaultEncoding, "surrogateescape");
+            stringobj = PyUnicode_EncodeFSDefault(u);
             Py_DECREF(u);
             if (stringobj == NULL)
                 return -1;
index 8552575f408c5ce50e7e85cf513d1ab9d19628cb..c7c1530545d01417967041c2c7a1c91c026542df 100644 (file)
@@ -3147,9 +3147,7 @@ PyInit__tkinter(void)
        it also helps Tcl find its encodings. */
     uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
     if (uexe) {
-        cexe = PyUnicode_AsEncodedString(uexe,
-                                         Py_FileSystemDefaultEncoding,
-                                         NULL);
+        cexe = PyUnicode_EncodeFSDefault(uexe);
         if (cexe)
             Tcl_FindExecutable(PyBytes_AsString(cexe));
         Py_XDECREF(cexe);
index d10a79d8002149320c0552fc774e5e317d1f2570..d64c14285734c175faa40cc51c5158620afab38d 100644 (file)
@@ -111,8 +111,7 @@ grp_getgrnam(PyObject *self, PyObject *args)
 
     if (!PyArg_ParseTuple(args, "U:getgrnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg, Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
index 35a387ad521671bf1dba0fa656b9226c596ef830..b303f95f3158fbbf008b989c5fd41a4d9d31c243 100644 (file)
@@ -132,9 +132,7 @@ pwd_getpwnam(PyObject *self, PyObject *args)
 
     if (!PyArg_ParseTuple(args, "U:getpwnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg,
-                                           Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
index da452e9ff0fcec6597d01532681ba479f1f9c88b..96707b4ada829e7c78e8d10cf0599fa62671a723 100644 (file)
@@ -118,9 +118,7 @@ static PyObject* spwd_getspnam(PyObject *self, PyObject *args)
 
     if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
         return NULL;
-    if ((bytes = PyUnicode_AsEncodedString(arg,
-                                           Py_FileSystemDefaultEncoding,
-                                           "surrogateescape")) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
         return NULL;
     if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
         goto out;
index 307027a8119b4f85f6de62d9705d147fe414acd1..b97621b9344639cc3d5e7ecd88086e1eced14c6a 100644 (file)
@@ -1461,6 +1461,18 @@ PyObject *PyUnicode_AsEncodedObject(PyObject *unicode,
     return NULL;
 }
 
+PyObject *PyUnicode_EncodeFSDefault(PyObject *unicode)
+{
+    if (Py_FileSystemDefaultEncoding)
+        return PyUnicode_AsEncodedString(unicode,
+                                         Py_FileSystemDefaultEncoding,
+                                         "surrogateescape");
+    else
+        return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
+                                     PyUnicode_GET_SIZE(unicode),
+                                     "surrogateescape");
+}
+
 PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
                                     const char *encoding,
                                     const char *errors)
@@ -1646,9 +1658,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
         arg = PyUnicode_FromObject(arg);
         if (!arg)
             return 0;
-        output = PyUnicode_AsEncodedObject(arg,
-                                           Py_FileSystemDefaultEncoding,
-                                           "surrogateescape");
+        output = PyUnicode_EncodeFSDefault(arg);
         Py_DECREF(arg);
         if (!output)
             return 0;
index 923888d5df1c4e72f34085f29eac925a18835e0a..d23eb6a9418c1ca479c1a51366423a97b65ecb8f 100644 (file)
@@ -1633,8 +1633,7 @@ find_module(char *fullname, char *subname, PyObject *path, char *buf,
         if (!v)
             return NULL;
         if (PyUnicode_Check(v)) {
-            v = PyUnicode_AsEncodedString(v,
-                Py_FileSystemDefaultEncoding, NULL);
+            v = PyUnicode_EncodeFSDefault(v);
             if (v == NULL)
                 return NULL;
         }
@@ -2752,14 +2751,7 @@ ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
             char *subname;
             PyObject *submod;
             char *p;
-            if (!Py_FileSystemDefaultEncoding) {
-                item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
-                                              PyUnicode_GetSize(item),
-                                              NULL);
-            } else {
-                item8 = PyUnicode_AsEncodedString(item,
-                    Py_FileSystemDefaultEncoding, NULL);
-            }
+            item8 = PyUnicode_EncodeFSDefault(item);
             if (!item8) {
                 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
                 return 0;