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, ...@@ -742,31 +742,33 @@ static void __Pyx_AddTraceback(const char *funcname, int c_line,
static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
const char *funcname, int c_line, const char *funcname, int c_line,
int py_line, const char *filename) { int py_line, const char *filename) {
PyCodeObject *py_code = 0; PyCodeObject *py_code = NULL;
PyObject *py_srcfile = 0; PyObject *py_funcname = NULL;
PyObject *py_funcname = 0;
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
PyObject *py_srcfile = NULL;
py_srcfile = PyString_FromString(filename); py_srcfile = PyString_FromString(filename);
#else
py_srcfile = PyUnicode_FromString(filename);
#endif
if (!py_srcfile) goto bad; if (!py_srcfile) goto bad;
#endif
if (c_line) { if (c_line) {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line); py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line);
if (!py_funcname) goto bad;
#else #else
py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, $cfilenm_cname, c_line); 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 #endif
} }
else { else {
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
py_funcname = PyString_FromString(funcname); py_funcname = PyString_FromString(funcname);
#else if (!py_funcname) goto bad;
py_funcname = PyUnicode_FromString(funcname);
#endif #endif
} }
if (!py_funcname) goto bad; #if PY_MAJOR_VERSION < 3
py_code = __Pyx_PyCode_New( py_code = __Pyx_PyCode_New(
0, /*int argcount,*/ 0, /*int argcount,*/
0, /*int posonlyargcount,*/ 0, /*int posonlyargcount,*/
...@@ -786,11 +788,16 @@ static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( ...@@ -786,11 +788,16 @@ static PyCodeObject* __Pyx_CreateCodeObjectForTraceback(
$empty_bytes /*PyObject *lnotab*/ $empty_bytes /*PyObject *lnotab*/
); );
Py_DECREF(py_srcfile); 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; return py_code;
bad: bad:
Py_XDECREF(py_srcfile);
Py_XDECREF(py_funcname); Py_XDECREF(py_funcname);
#if PY_MAJOR_VERSION < 3
Py_XDECREF(py_srcfile);
#endif
return NULL; 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