Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error handling of RERAISE is strange #122759

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

Closed
sobolevn opened this issue Aug 6, 2024 · 1 comment
Closed

Error handling of RERAISE is strange #122759

sobolevn opened this issue Aug 6, 2024 · 1 comment
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Aug 6, 2024

Bug report

There's a strange pattern used in RERAISE opcode:

cpython/Python/bytecodes.c

Lines 1174 to 1189 in 4767a6e

tier1 inst(RERAISE, (values[oparg], exc_st -- values[oparg])) {
PyObject *exc = PyStackRef_AsPyObjectBorrow(exc_st);
assert(oparg >= 0 && oparg <= 2);
if (oparg) {
PyObject *lasti = PyStackRef_AsPyObjectBorrow(values[0]);
if (PyLong_Check(lasti)) {
frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame)) + PyLong_AsLong(lasti);
assert(!_PyErr_Occurred(tstate));
}
else {
assert(PyLong_Check(lasti));
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
ERROR_NO_POP();
}
}

Especially these lines:

cpython/Python/bytecodes.c

Lines 1180 to 1187 in 4767a6e

if (PyLong_Check(lasti)) {
frame->instr_ptr = _PyCode_CODE(_PyFrame_GetCode(frame)) + PyLong_AsLong(lasti);
assert(!_PyErr_Occurred(tstate));
}
else {
assert(PyLong_Check(lasti));
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
ERROR_NO_POP();

It looks like the assert call is not needed here:

  • In debug builds it will always crash, since PyLong_Check(lasti) will always be false
  • In non-debug builds it will set SystemError as it should be (at least, I think so). That's the only line that matches r'assert\(.*\);\n\s+_PyErr'

I have a PR ready.

Linked PRs

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Aug 6, 2024
@sobolevn sobolevn self-assigned this Aug 6, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue Aug 6, 2024
@hugovk
Copy link
Member

hugovk commented Aug 8, 2024

Triage: closing because the linked PR is merged, please re-open if still needed.

@hugovk hugovk closed this as completed Aug 8, 2024
blhsing pushed a commit to blhsing/cpython that referenced this issue Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants