(original) (raw)
Index: Python/traceback.c =================================================================== --- Python/traceback.c (révision 85551) +++ Python/traceback.c (copie de travail) @@ -142,16 +142,18 @@ 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; - 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); @@ -162,8 +164,10 @@ taillen = strlen(tail); syspath = PySys_GetObject("path"); - if (syspath == NULL || !PyList_Check(syspath)) + if (syspath == NULL || !PyList_Check(syspath)) { + Py_DECREF(filebytes); return NULL; + } npath = PyList_Size(syspath); for (i = 0; i < npath; i++) { @@ -174,14 +178,18 @@ } 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,10 +197,13 @@ strcpy(namebuf+len, tail); binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb"); - if (binary != NULL) + if (binary != NULL) { + Py_DECREF(filebytes); return binary; + } PyErr_Clear(); } + Py_DECREF(filebytes); return NULL; } Index: Python/ast.c =================================================================== --- Python/ast.c (révision 85551) +++ Python/ast.c (copie de travail) @@ -102,6 +102,7 @@ ast_error_finish(const char *filename) { PyObject *type, *value, *tback, *errstr, *offset, *loc, *tmp; + PyObject *filename_obj; long lineno; assert(PyErr_Occurred()); @@ -130,7 +131,11 @@ Py_INCREF(Py_None); loc = Py_None; } - tmp = Py_BuildValue("(zlOO)", filename, lineno, offset, loc); + filename_obj = PyUnicode_DecodeFSDefault(filename); + if (filename_obj != NULL) + tmp = Py_BuildValue("(NlOO)", filename_obj, lineno, offset, loc); + else + tmp = NULL; Py_DECREF(loc); if (!tmp) { Py_DECREF(errstr); Index: Python/pythonrun.c =================================================================== --- Python/pythonrun.c (révision 85551) +++ Python/pythonrun.c (copie de travail) @@ -1213,7 +1213,7 @@ d = PyModule_GetDict(m); if (PyDict_GetItemString(d, "__file__") == NULL) { PyObject *f; - f = PyUnicode_FromString(filename); + f = PyUnicode_DecodeFSDefault(filename); if (f == NULL) return -1; if (PyDict_SetItemString(d, "__file__", f) < 0) { @@ -1968,7 +1968,9 @@ { PyObject *v, *w, *errtype, *errtext; PyObject *msg_obj = NULL; + PyObject *filename; char *msg = NULL; + errtype = PyExc_SyntaxError; switch (err->error) { case E_ERROR: @@ -2052,8 +2054,12 @@ errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text), "replace"); } - v = Py_BuildValue("(ziiN)", err->filename, - err->lineno, err->offset, errtext); + filename = PyUnicode_DecodeFSDefault(err->filename); + if (filename != NULL) + v = Py_BuildValue("(NiiN)", filename, + err->lineno, err->offset, errtext); + else + v = NULL; if (v != NULL) { if (msg_obj) w = Py_BuildValue("(OO)", msg_obj, v); Index: Python/compile.c =================================================================== --- Python/compile.c (révision 85551) +++ Python/compile.c (copie de travail) @@ -3942,7 +3942,7 @@ freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); if (!freevars) goto error; - filename = PyUnicode_FromString(c->c_filename); + filename = PyUnicode_DecodeFSDefault(c->c_filename); if (!filename) goto error; Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (révision 85551) +++ Python/bltinmodule.c (copie de travail) @@ -524,6 +524,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) { char *str; + PyObject *filename_obj; char *filename; char *startstr; int mode = -1; @@ -535,12 +536,16 @@ static char *kwlist[] = {"source", "filename", "mode", "flags", "dont_inherit", NULL}; int start[] = {Py_file_input, Py_eval_input, Py_single_input}; + PyObject *result; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oss|ii:compile", - kwlist, &cmd, &filename, &startstr, - &supplied_flags, &dont_inherit)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&s|ii:compile", kwlist, + &cmd, + PyUnicode_FSConverter, &filename_obj, + &startstr, &supplied_flags, + &dont_inherit)) return NULL; + filename = PyBytes_AS_STRING(filename_obj); cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8; if (supplied_flags & @@ -548,7 +553,7 @@ { PyErr_SetString(PyExc_ValueError, "compile(): unrecognised flags"); - return NULL; + goto error; } /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ @@ -565,14 +570,13 @@ else { PyErr_SetString(PyExc_ValueError, "compile() arg 3 must be 'exec', 'eval' or 'single'"); - return NULL; + goto error; } is_ast = PyAST_Check(cmd); if (is_ast == -1) - return NULL; + goto error; if (is_ast) { - PyObject *result; if (supplied_flags & PyCF_ONLY_AST) { Py_INCREF(cmd); result = cmd; @@ -585,20 +589,27 @@ mod = PyAST_obj2mod(cmd, arena, mode); if (mod == NULL) { PyArena_Free(arena); - return NULL; + goto error; } result = (PyObject*)PyAST_Compile(mod, filename, &cf, arena); PyArena_Free(arena); } - return result; + goto finally; } str = source_as_string(cmd, "compile", "string, bytes, AST or code", &cf); if (str == NULL) - return NULL; + goto error; - return Py_CompileStringFlags(str, filename, start[mode], &cf); + result = Py_CompileStringFlags(str, filename, start[mode], &cf); + goto finally; + +error: + result = NULL; +finally: + Py_DECREF(filename_obj); + return result; } PyDoc_STRVAR(compile_doc, Index: Parser/tokenizer.h =================================================================== --- Parser/tokenizer.h (révision 85551) +++ Parser/tokenizer.h (copie de travail) @@ -40,7 +40,7 @@ int level; /* () [] {} Parentheses nesting level */ /* Used to allow free continuations inside them */ /* Stuff for checking on different tab sizes */ - const char *filename; /* For error messages */ + const char *filename; /* encoded to the filesystem encoding */ int altwarning; /* Issue warning if alternate tabs don't match */ int alterror; /* Issue error if alternate tabs don't match */ int alttabsize; /* Alternate tab spacing */