diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-05-14 18:07:39 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-05-14 18:07:39 (GMT) |
commit | 203406c3681cd0bfe53f44f2d731d05d8d71cd57 (patch) | |
tree | 0f6210e54ac905ada3b955f2f3e8d046de115108 /Modules | |
parent | 26a968d8feceb1a331b800d3e0c0e28a164b9971 (diff) | |
download | cpython-203406c3681cd0bfe53f44f2d731d05d8d71cd57.zip cpython-203406c3681cd0bfe53f44f2d731d05d8d71cd57.tar.gz cpython-203406c3681cd0bfe53f44f2d731d05d8d71cd57.tar.bz2 |
Revert r81171 (posix_listdir(), posix_readlink(): avoid temporary PyBytes object)
PyUnicode_DecodeFSDefault*() doesn't use surrogateescape error handler, and so
PyUnicode_FromEncodedObject(v, Py_FileSystemDefaultEncoding, "surrogateescape")
cannot be replaced by PyUnicode_DecodeFSDefault().
It's a bad idea to try to fix surrogates things in Python 3.1...
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 9ce8966..590a47b 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -2515,17 +2515,33 @@ posix_listdir(PyObject *self, PyObject *args) (NAMLEN(ep) == 1 || (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) continue; - if (arg_is_unicode) - v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); - else - v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); + v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); if (v == NULL) { - Py_CLEAR(d); + Py_DECREF(d); + d = NULL; break; } + if (arg_is_unicode) { + PyObject *w; + + w = PyUnicode_FromEncodedObject(v, + Py_FileSystemDefaultEncoding, + "surrogateescape"); + Py_DECREF(v); + if (w != NULL) + v = w; + else { + /* Encoding failed to decode ASCII bytes. + Raise exception. */ + Py_DECREF(d); + d = NULL; + break; + } + } if (PyList_Append(d, v) != 0) { Py_DECREF(v); - Py_CLEAR(d); + Py_DECREF(d); + d = NULL; break; } Py_DECREF(v); @@ -4660,10 +4676,22 @@ posix_readlink(PyObject *self, PyObject *args) return posix_error_with_allocated_filename(opath); release_bytes(opath); - if (arg_is_unicode) - return PyUnicode_DecodeFSDefaultAndSize(buf, n); - else - return PyBytes_FromStringAndSize(buf, n); + v = PyBytes_FromStringAndSize(buf, n); + if (arg_is_unicode) { + PyObject *w; + + w = PyUnicode_FromEncodedObject(v, + Py_FileSystemDefaultEncoding, + "surrogateescape"); + if (w != NULL) { + Py_DECREF(v); + v = w; + } + else { + v = NULL; + } + } + return v; } #endif /* HAVE_READLINK */ |