Skip to content

gh-117535: Do not try to get the source line for file "sys" in warnings #117550

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
wants to merge 2 commits into from

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Apr 4, 2024

"sys" is a makeup filename in

    except ValueError:
        globals = sys.__dict__
        filename = "sys"
        lineno = 1

so we should never try to get the source code for it. Otherwise if there happens to be a sys file in sys.path, the first line will be displayed.

We can use something else than sys to be special, but there's always the issue with name conflict. I don't believe there's a reasonable case where sys is actually needed as the source file so this should be safe.

Not sure if this is worth a regression test - it's relatively rare and the problem is kind of benign. I can add some if people think it's required.

@gaogaotiantian
Copy link
Member Author

The warnings.py file has been relatively inactive and I don't believe there's a code owner. @serhiy-storchaka could you take a look at this as you've worked on the file before? The code change is trivial. Thanks!

@serhiy-storchaka
Copy link
Member

I think this is not the right place. It should be handled in the same way as names like <stdin> or <string> are handled, i.e. in linecache.getline().

@gaogaotiantian
Copy link
Member Author

Hmm, I kind of disagree. sys is only used as the file name in warnings, unlike <stdin> and <string> which is used more frequently. Also both <stdin> and <string> will appear as the co_filename of a code object that is dynamically built, but sys won't. sys is purely a makeup name in warnings module and should be handled in that module only.

@serhiy-storchaka
Copy link
Member

Well, then we should use something else than sys to be special. <string> and <stdin> are examples. We can use <sys> or <python> for example.

@gaogaotiantian
Copy link
Member Author

We can, but that's actually more risky. We use the string sys in warnings:

sys:1: RuntimeWarning: coroutine 'coroutine' was never awaited

Simply changing it to <sys> will break backward compatibility

<sys>:1: RuntimeWarning: coroutine 'coroutine' was never awaited

So we need to secretly translate it back to sys after we check the linecache, which is more code in more places.

The worst part is, we allow users to overwrite the format part, and their formatting will be broken without knowing the fact.

From the reason above, I don't believe changing it to <sys> is a better idea than ignoring it when we check linecache.

@ronaldoussoren
Copy link
Contributor

ronaldoussoren commented Apr 7, 2024

The "sys" default was introduced in 11a8966, related issue #77556. Neither the issue nor the related PR sheds light on why this particular default was chosen.

I agree with @serhiy-storchaka that the default name is not a good one because it doesn't clearly communicate that the name does not refer to a real file.

Changing "sys" to "<sys>" or "<python>" should be mostly risk free in a feature release (e.g. for main) because warnings filters use a module name and not a file name.

@serhiy-storchaka
Copy link
Member

@ronaldoussoren, I fixed formatting in your comment, because unquoted <sys> is treated as a raw HTML, and displayed as an empty string. It should be quoted: `<sys>`. I did the same mistake in my comment.

@gaogaotiantian
Copy link
Member Author

I'm not worried about the filter for warnings, mostly breaking people's test cases. Changing the output message could potentially break tests, considering this is the format for about 6 years.

However, I don't think changing sys to <sys> is the end of the world. I just have some concerns about breaking tests - whether it's worth it. Do we believe <sys> is the right name? I don't think <sys> is ever used anywhere in the current CPython code base and that'll be something new to the users. Strings with <> could be an indicator that it's not a real file which is widely acknowledged, is sys a relatively good name here?

Also it's just a question. I don't have a very good alternate here.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I approve it because it is the best solution that can be backported. But I think that we should do better in "main".

@gaogaotiantian
Copy link
Member Author

Sure that makes sense. So what do we want for this PR? Do we want to merge + backport it, then leave the issue open and figure out a better idea for main, or do we want to make a decision about main now and fix both main and older version together?

@gaogaotiantian
Copy link
Member Author

Hi @serhiy-storchaka , how do you want to proceed this issue? This is a fix that can be backported, and you proposed to change the filename to <sys> on main. Do we want to apply this first then change it to <sys>, or make this a backport only PR?

@serhiy-storchaka
Copy link
Member

If we apply this first, the following PR will need to revert these changes completely. I think it is better to make this a backport only PR.

@gaogaotiantian
Copy link
Member Author

I made a PR #118014 for 3.12. 3.11 is security fix only now so I won't bother. I'll make another one to replace "sys" with "<sys>" and fix the tests if there are broken ones.

@gaogaotiantian
Copy link
Member Author

#118018 is the PR for main, which replaced sys with <sys>. After both PRs are closed, I'll close this one.

@gaogaotiantian gaogaotiantian deleted the fix-sys-warning branch April 19, 2024 04:52
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.

3 participants