8000 gh-106149: Simplify stack depth calculation. Replace asserts by exceptions. by iritkatriel · Pull Request #107255 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106149: Simplify stack depth calculation. Replace asserts by exceptions. #107255

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

Merged
merged 6 commits into from
Jul 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
remove redundant assert. Explain the next one.
  • Loading branch information
iritkatriel committed Jul 25, 2023
commit fce09194f14c71b3b2bf86ec468667cc434fa2b3
8 changes: 7 additions & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7731,7 +7731,13 @@ optimize_and_assemble_code_unit(struct compiler_unit *u, PyObject *const_cache,
if (stackdepth < 0) {
goto error;
}
assert(stackdepth >= 0);

/* prepare_localsplus adds instructions for generators that push
* and pop an item on the stack. This assertion makes sure there
* is space on the stack for that.
* It should always be true, because at least one expression is
* required to turn a function into a generator.
Comment on lines +7738 to +7739
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this explanation is accurate? This function is a generator:

async def f():
    return

Here is the disassembly of it:

>>> dis.dis(f)
  1           0 RETURN_GENERATOR
              2 POP_TOP
              4 RESUME                   0

  2           6 RETURN_CONST             0 (None)
        >>    8 CALL_INTRINSIC_1         3 (INTRINSIC_STOPITERATION_ERROR)
             10 RERAISE                  1
ExceptionTable:
  4 to 6 -> 8 [0] lasti

I think if RETURN_CONST didn't exist, it would be enough to say "all functions must return a value, which always must go on the stack before it is returned." But with RETURN_CONST that's not true either. It really does seem like the StopIteration handler is what we are relying on here.

*/
assert(!(IS_GENERATOR(code_flags) && stackdepth == 0));

/** Assembly **/
Expand Down
0