[Python-checkins] r81194 - in python/branches/py3k: 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

victor.stinner python-checkins at python.org
Sat May 15 18:27:28 CEST 2010


Author: victor.stinner
Date: Sat May 15 18:27:27 2010
New Revision: 81194
Log:
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.
Modified:
 python/branches/py3k/Doc/c-api/unicode.rst
 python/branches/py3k/Include/unicodeobject.h
 python/branches/py3k/Misc/NEWS
 python/branches/py3k/Modules/_io/fileio.c
 python/branches/py3k/Modules/_tkinter.c
 python/branches/py3k/Modules/grpmodule.c
 python/branches/py3k/Modules/pwdmodule.c
 python/branches/py3k/Modules/spwdmodule.c
 python/branches/py3k/Objects/unicodeobject.c
 python/branches/py3k/Python/import.c
Modified: python/branches/py3k/Doc/c-api/unicode.rst
==============================================================================
--- python/branches/py3k/Doc/c-api/unicode.rst	(original)
+++ python/branches/py3k/Doc/c-api/unicode.rst	Sat May 15 18:27:27 2010
@@ -396,6 +396,7 @@
 
 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 @@
 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
 """""""""""""""
 
Modified: python/branches/py3k/Include/unicodeobject.h
==============================================================================
--- python/branches/py3k/Include/unicodeobject.h	(original)
+++ python/branches/py3k/Include/unicodeobject.h	Sat May 15 18:27:27 2010
@@ -1268,6 +1268,16 @@
 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
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat May 15 18:27:27 2010
@@ -12,6 +12,11 @@
 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)
 
Modified: python/branches/py3k/Modules/_io/fileio.c
==============================================================================
--- python/branches/py3k/Modules/_io/fileio.c	(original)
+++ python/branches/py3k/Modules/_io/fileio.c	Sat May 15 18:27:27 2010
@@ -247,8 +247,7 @@
 if (u == NULL)
 return -1;
 
- stringobj = PyUnicode_AsEncodedString(
- u, Py_FileSystemDefaultEncoding, "surrogateescape");
+ stringobj = PyUnicode_EncodeFSDefault(u);
 Py_DECREF(u);
 if (stringobj == NULL)
 return -1;
Modified: python/branches/py3k/Modules/_tkinter.c
==============================================================================
--- python/branches/py3k/Modules/_tkinter.c	(original)
+++ python/branches/py3k/Modules/_tkinter.c	Sat May 15 18:27:27 2010
@@ -3147,9 +3147,7 @@
 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);
Modified: python/branches/py3k/Modules/grpmodule.c
==============================================================================
--- python/branches/py3k/Modules/grpmodule.c	(original)
+++ python/branches/py3k/Modules/grpmodule.c	Sat May 15 18:27:27 2010
@@ -111,8 +111,7 @@
 
 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;
Modified: python/branches/py3k/Modules/pwdmodule.c
==============================================================================
--- python/branches/py3k/Modules/pwdmodule.c	(original)
+++ python/branches/py3k/Modules/pwdmodule.c	Sat May 15 18:27:27 2010
@@ -132,9 +132,7 @@
 
 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;
Modified: python/branches/py3k/Modules/spwdmodule.c
==============================================================================
--- python/branches/py3k/Modules/spwdmodule.c	(original)
+++ python/branches/py3k/Modules/spwdmodule.c	Sat May 15 18:27:27 2010
@@ -118,9 +118,7 @@
 
 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;
Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Sat May 15 18:27:27 2010
@@ -1461,6 +1461,18 @@
 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 @@
 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;
Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c	(original)
+++ python/branches/py3k/Python/import.c	Sat May 15 18:27:27 2010
@@ -1633,8 +1633,7 @@
 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 @@
 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;


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /