Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41103bf

Browse files
committed
Ensure that code object names (co_name) are unicode.
Verify that they print properly too.
1 parent a5d16a3 commit 41103bf

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

Objects/codeobject.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ PyCode_New(int argcount, int kwonlyargcount,
6565
PyErr_BadInternalCall();
6666
return NULL;
6767
}
68+
if (PyString_Check(name)) {
69+
name = PyUnicode_FromString(PyString_AS_STRING(name));
70+
if (name == NULL)
71+
return NULL;
72+
} else {
73+
Py_INCREF(name);
74+
}
6875
intern_strings(names);
6976
intern_strings(varnames);
7077
intern_strings(freevars);
@@ -106,6 +113,7 @@ PyCode_New(int argcount, int kwonlyargcount,
106113
co->co_lnotab = lnotab;
107114
co->co_zombieframe = NULL;
108115
}
116+
Py_DECREF(name);
109117
return co;
110118
}
111119

@@ -288,17 +296,14 @@ code_repr(PyCodeObject *co)
288296
{
289297
int lineno = -1;
290298
char *filename = "???";
291-
char *name = "???";
292299

293300
if (co->co_firstlineno != 0)
294301
lineno = co->co_firstlineno;
295302
if (co->co_filename && PyString_Check(co->co_filename))
296303
filename = PyString_AS_STRING(co->co_filename);
297-
if (co->co_name && PyString_Check(co->co_name))
298-
name = PyString_AS_STRING(co->co_name);
299304
return PyUnicode_FromFormat(
300-
"<code object %.100s at %p, file \"%.300s\", line %d>",
301-
name, co, filename, lineno);
305+
"<code object %.100U at %p, file \"%.300s\", line %d>",
306+
co->co_name, co, filename, lineno);
302307
}
303308

304309
static PyObject *

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ compiler_dictcomp(struct compiler *c, expr_ty e)
29912991
{
29922992
static identifier name;
29932993
if (!name) {
2994-
name = PyString_FromString("<dictcomp>");
2994+
name = PyUnicode_FromString("<dictcomp>");
29952995
if (!name)
29962996
return 0;
29972997
}

0 commit comments

Comments
 (0)