diff options
| author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-10-16 13:14:10 +0000 | 
|---|---|---|
| committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-10-16 13:14:10 +0000 | 
| commit | 4c7c8c30235e42c47500b91549c2b6154b61f883 (patch) | |
| tree | 649a94a99ea257c19a3e5ba17fc05a8044459243 /Python/traceback.c | |
| parent | 5a7913eb3bf390a2f3fd28116fc789bf2c7e4b64 (diff) | |
| download | cpython-git-4c7c8c30235e42c47500b91549c2b6154b61f883.tar.gz | |
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.
Diffstat (limited to 'Python/traceback.c')
| -rw-r--r-- | Python/traceback.c | 35 | 
1 files changed, 25 insertions, 10 deletions
diff --git a/Python/traceback.c b/Python/traceback.c index 558755d209..ab10cfd161 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -142,16 +142,19 @@ _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_EncodeFSDefault(filename); +    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); @@ -163,7 +166,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++) { @@ -174,14 +177,18 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *          }          if (!PyUnicode_Check(v))              continue; -        path = _PyUnicode_AsStringAndSize(v, &len); +        path = PyUnicode_EncodeFSDefault(v);          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) @@ -189,11 +196,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  | 
