(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);
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 */