diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-10-19 01:22:07 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-10-19 01:22:07 (GMT) |
commit | 15244f7b126b2eab94a0755a82c431a4933330f4 (patch) | |
tree | 358c60d119546f862a90692fd71feaa9c774d78c /Python/traceback.c | |
parent | 089144e511ca4b388ce8b29ecf2965a8215ff238 (diff) | |
download | cpython-15244f7b126b2eab94a0755a82c431a4933330f4.zip cpython-15244f7b126b2eab94a0755a82c431a4933330f4.tar.gz cpython-15244f7b126b2eab94a0755a82c431a4933330f4.tar.bz2 |
Recorded merge of revisions 85569-85570 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r85569 | victor.stinner | 2010-10-16 15:14:10 +0200 (sam., 16 oct. 2010) | 4 lines
Issue #9713, #10114: Parser functions (eg. PyParser_ASTFromFile) expects
filenames encoded to the filesystem encoding with surrogateescape error handler
(to support undecodable bytes), instead of UTF-8 in strict mode.
........
r85570 | victor.stinner | 2010-10-16 15:42:53 +0200 (sam., 16 oct. 2010) | 4 lines
Fix ast_error_finish() and err_input(): filename can be NULL
Fix my previous commit (r85569).
........
Diffstat (limited to 'Python/traceback.c')
-rw-r--r-- | Python/traceback.c | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/Python/traceback.c b/Python/traceback.c index e74d442..2f1c213 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -143,16 +143,20 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject * Py_ssize_t npath; size_t taillen; PyObject *syspath; - const char* path; + PyObject *path; const char* tail; + PyObject *filebytes; const char* filepath; Py_ssize_t len; + PyObject* result; - filepath = _PyUnicode_AsString(filename); - if (filepath == NULL) { + filebytes = PyUnicode_AsEncodedObject(filename, + Py_FileSystemDefaultEncoding, "surrogateescape"); + if (filebytes == NULL) { PyErr_Clear(); return NULL; } + filepath = PyBytes_AS_STRING(filebytes); /* Search tail of filename in sys.path before giving up */ tail = strrchr(filepath, SEP); @@ -164,7 +168,7 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject * syspath = PySys_GetObject("path"); if (syspath == NULL || !PyList_Check(syspath)) - return NULL; + goto error; npath = PyList_Size(syspath); for (i = 0; i < npath; i++) { @@ -175,14 +179,20 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject * } if (!PyUnicode_Check(v)) continue; - path = _PyUnicode_AsStringAndSize(v, &len); + + path = PyUnicode_AsEncodedObject(v, Py_FileSystemDefaultEncoding, + "surrogateescape"); if (path == NULL) { PyErr_Clear(); continue; } - if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) + len = PyBytes_GET_SIZE(path); + if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { + Py_DECREF(path); continue; /* Too long */ - strcpy(namebuf, path); + } + strcpy(namebuf, PyBytes_AS_STRING(path)); + Py_DECREF(path); if (strlen(namebuf) != len) continue; /* v contains '\0' */ if (len > 0 && namebuf[len-1] != SEP) @@ -190,11 +200,19 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject * strcpy(namebuf+len, tail); binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb"); - if (binary != NULL) - return binary; + if (binary != NULL) { + result = binary; + goto finally; + } PyErr_Clear(); } - return NULL; + goto error; + +error: + result = NULL; +finally: + Py_DECREF(filebytes); + return result; } int |