Skip to content
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

Merged
merged 2 commits into from Nov 25, 2021

Conversation

da-woods
Copy link
Contributor

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 then PyCode_NewEmpty will use that to create another unicode object (in principle this could probably mostly be avoided using snprintf 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.

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?)
#if PY_MAJOR_VERSION < 3
PyObject *py_srcfile = 0;
Copy link
Contributor

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.

Suggested change
PyObject *py_srcfile = 0;
PyObject *py_srcfile = NULL;

#else
py_code = PyCode_NewEmpty(filename, funcname, py_line);
#endif
Py_XDECREF(py_funcname); // XDECREF is it's only set on Py3 if cline
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

#endif
}
#if PY_MAJOR_VERSION < 3
if (!py_funcname) goto bad;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@scoder scoder added this to the 3.0 milestone Nov 25, 2021
@scoder scoder merged commit 59e6c29 into cython:master Nov 25, 2021
@da-woods da-woods deleted the exception-pycode branch November 25, 2021 08:40
scoder pushed a commit that referenced this pull request Dec 5, 2021
…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?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants