Commit 59e6c294 authored by da-woods's avatar da-woods Committed by GitHub

Exceptions.c: use PyCode_NewEmpty() in Py3 instead of PyCode_New() (GH-4479)

With reference to https://github.com/cython/cython/issues/4365#issuecomment-977023011
in Python 3 PyCode_NewEmpty does everything we need to set
exception traceback code objects.

This change is probably only a real improvement on Py3.11 onwards
(where the replacement for PyCode_new is currently pretty slow).
On earlier version it'll probably be fairly similar (maybe one extra allocation for the cline case?)
parent 002e7c6f
......@@ -742,31 +742,33 @@ static void __Pyx_AddTraceback(const char *funcname, int c_line,
static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
const char *funcname, int c_line,
int py_line, const char *filename) {
PyCodeObject *py_code = 0;
PyObject *py_srcfile = 0;
PyObject *py_funcname = 0;
PyCodeObject *py_code = NULL;
PyObject *py_funcname = NULL;
#if PY_MAJOR_VERSION < 3
PyObject *py_srcfile = NULL;
py_srcfile = PyString_FromString(filename);
#else
py_srcfile = PyUnicode_FromString(filename);
#endif
if (!py_srcfile) goto bad;
#endif
if (c_line) {
#if PY_MAJOR_VERSION < 3
py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
if (!py_funcname) goto bad;
#else
py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
if (!py_funcname) goto bad;
funcname = PyUnicode_AsUTF8(py_funcname);
if (!funcname) goto bad;
#endif
}
else {
#if PY_MAJOR_VERSION < 3
py_funcname = PyString_FromString(funcname);
#else
py_funcname = PyUnicode_FromString(funcname);
if (!py_funcname) goto bad;
#endif
}
if (!py_funcname) goto bad;
#if PY_MAJOR_VERSION < 3
py_code = __Pyx_PyCode_New(
0, /*int argcount,*/
0, /*int posonlyargcount,*/
......@@ -786,11 +788,16 @@ static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
$empty_bytes /*PyObject *lnotab*/
);
Py_DECREF(py_srcfile);
Py_DECREF(py_funcname);
#else
py_code = PyCode_NewEmpty(filename, funcname, py_line);
#endif
Py_XDECREF(py_funcname); // XDECREF since it's only set on Py3 if cline
return py_code;
bad:
Py_XDECREF(py_srcfile);
Py_XDECREF(py_funcname);
#if PY_MAJOR_VERSION < 3
Py_XDECREF(py_srcfile);
#endif
return NULL;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment