From 41103bf6f2f5787ddf7106fb3e5a0a98bb6b1c7d Mon Sep 17 00:00:00 2001 From: Neal Norwitz Date: Fri, 24 Aug 2007 23:12:06 +0000 Subject: Ensure that code object names (co_name) are unicode. Verify that they print properly too. --- Objects/codeobject.c | 15 ++++++++++----- Python/compile.c | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0a7c141..7bd292a 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -65,6 +65,13 @@ PyCode_New(int argcount, int kwonlyargcount, PyErr_BadInternalCall(); return NULL; } + if (PyString_Check(name)) { + name = PyUnicode_FromString(PyString_AS_STRING(name)); + if (name == NULL) + return NULL; + } else { + Py_INCREF(name); + } intern_strings(names); intern_strings(varnames); intern_strings(freevars); @@ -106,6 +113,7 @@ PyCode_New(int argcount, int kwonlyargcount, co->co_lnotab = lnotab; co->co_zombieframe = NULL; } + Py_DECREF(name); return co; } @@ -288,17 +296,14 @@ code_repr(PyCodeObject *co) { int lineno = -1; char *filename = "???"; - char *name = "???"; if (co->co_firstlineno != 0) lineno = co->co_firstlineno; if (co->co_filename && PyString_Check(co->co_filename)) filename = PyString_AS_STRING(co->co_filename); - if (co->co_name && PyString_Check(co->co_name)) - name = PyString_AS_STRING(co->co_name); return PyUnicode_FromFormat( - "", - name, co, filename, lineno); + "", + co->co_name, co, filename, lineno); } static PyObject * diff --git a/Python/compile.c b/Python/compile.c index 8ae4036..d2bfe86 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2991,7 +2991,7 @@ compiler_dictcomp(struct compiler *c, expr_ty e) { static identifier name; if (!name) { - name = PyString_FromString(""); + name = PyUnicode_FromString(""); if (!name) return 0; } -- cgit v0.12