New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exceptions.c PyCode_New -> PyCode_NewEmpty #4479
Conversation
With reference to cython#4365 (comment) 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?)
Cython/Utility/Exceptions.c
Outdated
#if PY_MAJOR_VERSION < 3 | ||
PyObject *py_srcfile = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, it's not always been done before, but I'd rather see the NULL-pointer spelled as NULL
.
PyObject *py_srcfile = 0; | |
PyObject *py_srcfile = NULL; |
Cython/Utility/Exceptions.c
Outdated
#else | ||
py_code = PyCode_NewEmpty(filename, funcname, py_line); | ||
#endif | ||
Py_XDECREF(py_funcname); // XDECREF is it's only set on Py3 if cline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Py_XDECREF(py_funcname); // XDECREF is it's only set on Py3 if cline | |
Py_XDECREF(py_funcname); // XDECREF since it's only set on Py3 if cline |
Cython/Utility/Exceptions.c
Outdated
#endif | ||
} | ||
#if PY_MAJOR_VERSION < 3 | ||
if (!py_funcname) goto bad; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be moved into the else
case now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was covering two cases (which is why I left it there). With that said, it's probably clearer attached to each case individually, so I moved it.
…H-4479) With reference to #4365 (comment) 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?)
With reference to #4365 (comment)
in Python 3 PyCode_NewEmpty does everything we need to set exception traceback code objects.
The downside is for the "cline" case, where it creates an extra unicode object, gets the
char*
from that, and thenPyCode_NewEmpty
will use that to create another unicode object (in principle this could probably mostly be avoided usingsnprintf
or similar, but it doesn't seem worth it given that all these code objects are cached). For all other cases it should end up largely the same.It's mainly an improvement on the Py3.11 case (where the
PyCode_New
replacement is likely fairly slow). For simplicity I've done it on all Python 3 versions, but it could also be done for Python 3.11 only.