Skip to content

GH-94438: Fix RuntimeWarning for jump tests in test_sys_settrace #111341

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 4 commits into from
Oct 26, 2023

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Oct 26, 2023

A couple of new tests were introduced to test_sys_settrace in #111237, however, two of them triggered a RuntimeWarning:

./python -m test test_sys_settrace -m test_jump_is_not_none_forwards
Using random seed: 53646886
0:00:00 load avg: 0.44 Run 1 test sequentially
0:00:00 load avg: 0.44 [1/1] test_sys_settrace
/home/gaogaotiantian/programs/mycpython/Lib/test/test_sys_settrace.py:1947: RuntimeWarning: assigning None to 1 unbound local
  frame.f_lineno = self.firstLine + self.jumpTo

== Tests result: SUCCESS ==

This is because the jump happens before all the local variables are assigned:

    @jump_test(1, 4, [5])
    def test_jump_is_not_none_forwards(output):
        x = None
        if x is not None:
            output.append(3)
        else:
            output.append(5)

The test jumps on line 1 - before x is assigned to None. So when frame.f_lineno is being set, a warning is generated to alert the users that the unbound local variables are being set to None.

This does not affect the validity or the result of the test, but I don't think it's intended, and it is not the best practice. Especially considering that the decorator can already take care of such warnings.

In this PR, two different fixes were applied to the problematic tests to maximize the diversity:

  • Add the warning to expected result
  • Start from line 2 so x is assigned.

A warning checker is also introduced so this won't happen in the future - if a test is not expecting a warning, then a warning is treated as an error. This might not be the best practice for all tests globally, but in this specific case, we do have "expected warnings" and it should count.

@gaogaotiantian
Copy link
Member Author

cc: @savannahostrowski

Copy link
Member

@savannahostrowski savannahostrowski left a comment

Choose a reason for hiding this comment

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

My intention was to have both of these jump after x was defined. I wonder if we should just update to both jump on line 2 and then have a separate test case for warnings?

@gaogaotiantian
Copy link
Member Author

My intention was to have both of these jump after x was defined. I wonder if we should just update to both jump on line 2 and then have a separate test case for warnings?

We can do that, but the test case with warning tests what you want as well. Your test case does not rely on the variable x - it just needs some specific bytecode in the function (if I understand your test case correctly). Jumping from line 1 will just cover all the stuff you want to test, and it gives a little bit extra coverage for jumping before a variable is initialized. You wrote the test so I will definitely change it to jumping from line 2 if you want - just share my thoughts on this.

@savannahostrowski
Copy link
Member

That makes sense - thanks for catching this. Good learning for me too!

Copy link
Member

@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! This should be backported, yeah?

@gaogaotiantian
Copy link
Member Author

Looks good, thanks! This should be backported, yeah?

Yes as the original test was backported.

@brandtbucher brandtbucher added the needs backport to 3.12 only security fixes label Oct 26, 2023
@brandtbucher brandtbucher changed the title Fix RuntimeWarning for jump tests in test_sys_settrace GH-94438: Fix RuntimeWarning for jump tests in test_sys_settrace Oct 26, 2023
@bedevere-app bedevere-app bot mentioned this pull request Oct 25, 2023
3 tasks
@brandtbucher brandtbucher merged commit a254120 into python:main Oct 26, 2023
@miss-islington-app
Copy link

Thanks @gaogaotiantian for the PR, and @brandtbucher for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 26, 2023
@bedevere-app
Copy link

bedevere-app bot commented Oct 26, 2023

GH-111369 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Oct 26, 2023
@gaogaotiantian
Copy link
Member Author

Just to mention that the original test was backported to 3.11 as well.

@brandtbucher
Copy link
Member

If I recall correctly, we only introduced these warnings in 3.12 (I think it coincided with the LOAD_FAST/LOAD_FAST_CHECK split). So no need to backport further than that?

@gaogaotiantian
Copy link
Member Author

Yeah just checked the blame, the warning was added in 3.12.

@gaogaotiantian gaogaotiantian deleted the fix-sys-settrace-test branch October 26, 2023 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants