Commit 59e62db0 authored by Victor Stinner's avatar Victor Stinner

Enable shortcuts for common encodings in PyUnicode_AsEncodedString() for any

error handler, not only the default error handler (strict)
parent b744ba1d
...@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1? ...@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Enable shortcuts for common encodings in PyUnicode_AsEncodedString() for any
error handler, not only the default error handler (strict)
- Issue #8610: Load file system codec at startup, and display a fatal error on - Issue #8610: Load file system codec at startup, and display a fatal error on
failure. Set the file system encoding to utf-8 (instead of None) if getting failure. Set the file system encoding to utf-8 (instead of None) if getting
the locale encoding failed, or if nl_langinfo(CODESET) function is missing. the locale encoding failed, or if nl_langinfo(CODESET) function is missing.
......
...@@ -1476,31 +1476,39 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode, ...@@ -1476,31 +1476,39 @@ PyObject *PyUnicode_AsEncodedString(PyObject *unicode,
encoding = PyUnicode_GetDefaultEncoding(); encoding = PyUnicode_GetDefaultEncoding();
/* Shortcuts for common default encodings */ /* Shortcuts for common default encodings */
if (errors == NULL) { if (strcmp(encoding, "utf-8") == 0)
if (strcmp(encoding, "utf-8") == 0) return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
return PyUnicode_AsUTF8String(unicode); PyUnicode_GET_SIZE(unicode),
else if (strcmp(encoding, "latin-1") == 0) errors);
return PyUnicode_AsLatin1String(unicode); else if (strcmp(encoding, "latin-1") == 0)
return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
errors);
#if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T) #if defined(MS_WINDOWS) && defined(HAVE_USABLE_WCHAR_T)
else if (strcmp(encoding, "mbcs") == 0) else if (strcmp(encoding, "mbcs") == 0)
return PyUnicode_AsMBCSString(unicode); return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
errors);
#endif #endif
else if (strcmp(encoding, "ascii") == 0) else if (strcmp(encoding, "ascii") == 0)
return PyUnicode_AsASCIIString(unicode); return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
/* During bootstrap, we may need to find the encodings PyUnicode_GET_SIZE(unicode),
package, to load the file system encoding, and require the errors);
file system encoding in order to load the encodings /* During bootstrap, we may need to find the encodings
package. package, to load the file system encoding, and require the
file system encoding in order to load the encodings
Break out of this dependency by assuming that the path to package.
the encodings module is ASCII-only. XXX could try wcstombs
instead, if the file system encoding is the locale's Break out of this dependency by assuming that the path to
encoding. */ the encodings module is ASCII-only. XXX could try wcstombs
else if (Py_FileSystemDefaultEncoding && instead, if the file system encoding is the locale's
strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 && encoding. */
!PyThreadState_GET()->interp->codecs_initialized) else if (Py_FileSystemDefaultEncoding &&
return PyUnicode_AsASCIIString(unicode); strcmp(encoding, Py_FileSystemDefaultEncoding) == 0 &&
} !PyThreadState_GET()->interp->codecs_initialized)
return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
PyUnicode_GET_SIZE(unicode),
errors);
/* Encode via the codec registry */ /* Encode via the codec registry */
v = PyCodec_Encode(unicode, encoding, errors); v = PyCodec_Encode(unicode, encoding, errors);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment