Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Jun 3, 2020

_PyEval_EvalCode() now uses co->co_name for error messages if
qualname is NULL (ex: when PyEval_EvalCodeEx() is called).

https://bugs.python.org/issue40679

_PyEval_EvalCode() now uses co->co_name for error messages if qualname is NULL (ex: when PyEval_EvalCodeEx() is called).
PyCodeObject* co = (PyCodeObject*)_co;
PyFrameObject *f;
PyCodeObject *co = (PyCodeObject*)_co;
PyObject *code_name = qualname ? qualname : co->co_name;
Copy link
Member

Choose a reason for hiding this comment

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

Rather than putting this "fix" in _PyEval_EvalCode(), what about the idea of documenting that the qualname (or code_name) argument can't be NULL (and asserting that), and updating the callers instead? There are only a couple callers, and it will put the messiness closer to the source of the issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

qualname is NULL in PyEval_EvalCodeEx. I don't think that it would be possible to pass co->co_name as qualname in such case.

Copy link
Member Author

Choose a reason for hiding this comment

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

qualname parameter of PyEval_EvalCode() is passed to PyCoro_New() for example. Extract of its code:

 if (name != NULL) gen->gi_name = name; else gen->gi_name = ((PyCodeObject *)gen->gi_code)->co_name; Py_INCREF(gen->gi_name); if (qualname != NULL) gen->gi_qualname = qualname; else gen->gi_qualname = gen->gi_name; Py_INCREF(gen->gi_qualname); 

Note: In PyEval_EvalCodeEx(), name is also NULL.

Or maybe we can set name and qualname to a more useful value than NULL in PyEval_EvalCodeEx(), I don't know.

Copy link
Member

@cjerdonek cjerdonek Jun 3, 2020

Choose a reason for hiding this comment

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

Yeah, that's why I was saying "(or code_name)" in my comment (i.e. _PyEval_EvalCode()'s argument can be changed to code_name). I'm not sure the "fix" is needed outside of PyEval_EvalCodeEx(), which is why I was suggesting it may be better there.

Copy link
Member

Choose a reason for hiding this comment

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

(I don't know the implications for PyCoro_New() though.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Even if it's "private" (_Py) prefix, _PyEval_EvalCodeWithName() is exposed in the C API and can be called with qualname=NULL. So I don't think that it's worth it to attempt to leave _PyEval_EvalCode() unchanged: I consider that _PyEval_EvalCode() is the right place to fix the issue.

Anyway, I wrote PR #20615 which tries to change less code.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for discussing with me. I approved the other PR, but either is fine (not sure which you prefer at this point).

@vstinner
Copy link
Member Author

vstinner commented Jun 4, 2020

I close this PR in favor of PR #20615.

@vstinner vstinner closed this Jun 4, 2020
@vstinner vstinner deleted the code_name branch June 4, 2020 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment