Index: Python/bltinmodule.c =================================================================== --- Python/bltinmodule.c (révision 85516) +++ Python/bltinmodule.c (copie de travail) @@ -524,6 +524,7 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) { char *str; + PyObject *filename_obj, *filename_bytes; char *filename; char *startstr; int mode = -1; @@ -535,12 +536,23 @@ 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, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOs|ii:compile", + kwlist, &cmd, &filename_obj, &startstr, &supplied_flags, &dont_inherit)) return NULL; + if (!PyUnicode_Check(filename_obj)) { + PyErr_Format(PyExc_TypeError, + "expect a string for the filename, not %s", + Py_TYPE(filename_obj)->tp_name); + return NULL; + } + + filename_bytes = PyUnicode_AsEncodedString(filename_obj, "utf-8", "surrogateescape"); + filename = PyBytes_AS_STRING(filename_bytes); + cf.cf_flags = supplied_flags | PyCF_SOURCE_IS_UTF8; if (supplied_flags & @@ -548,7 +560,7 @@ { PyErr_SetString(PyExc_ValueError, "compile(): unrecognised flags"); - return NULL; + goto error; } /* XXX Warn if (supplied_flags & PyCF_MASK_OBSOLETE) != 0? */ @@ -565,14 +577,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 +596,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_bytes); + return result; } PyDoc_STRVAR(compile_doc,