GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-132732: Treat `bytes` as constants in `_Py_uop_sym_is_safe_const` by sobolevn · Pull Request #136033 · python/cpython

Conversation

@sobolevn

@sobolevn sobolevn commented Jun 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
b'a' + b'b'  # is a constant evaluation
  • Issue: Constant evaluate/propagate pure ops automatically #132732

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn
pythongh-132732: Treat bytes as constants in `_Py_uop_sym_is_safe_c…
1dce1aa
…onst`
@sobolevn
sobolevn requested review from Fidget-Spinner and brandtbucher June 27, 2025 12:23
@sobolevn
sobolevn requested a review from tomasr8 as a code owner June 27, 2025 12:23
@bedevere-app bedevere-app Bot added the awaiting core review label Jun 27, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Jun 27, 2025
Constant evaluate/propagate pure ops automatically #132732
Closed
@sobolevn sobolevn mentioned this pull request Jun 27, 2025
gh-132732: Automatically constant evaluate pure operations #132733
Merged
@sobolevn sobolevn added the skip news label Jun 27, 2025
tomasr8
tomasr8 approved these changes Jun 27, 2025
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Jun 27, 2025
Fidget-Spinner
Fidget-Spinner reviewed Jun 27, 2025
View reviewed changes
Comment thread Python/optimizer_symbols.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Fidget-Spinner
Fidget-Spinner reviewed Jun 27, 2025
View reviewed changes
Comment thread Python/optimizer_symbols.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

One thing that slightly worries me about this is that comparing str/int and bytes objects raises UnicodeWarning if the -b option was passed on the command line. With this change, additional checks are required if we want to make COMPARE_OP able to constant-evaluate in the optimizer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

One thing that slightly worries me about this is that comparing str/int and bytes objects raises UnicodeWarning if the -b option was passed on the command line. With this change, additional checks are required if we want to make COMPARE_OP able to constant-evaluate in the optimizer.

Ok let's not do this then. We still need to fix the long stuff though so @sobolevn are you open to that or do you want me to handle it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

I opened PR #136040 to fix the compact ints issue.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Jun 27, 2025

Copy link
Copy Markdown
Member Author

With this change, additional checks are required if we want to make COMPARE_OP able to constant-evaluate in the optimizer.

Did you mean checking for _Py_GetConfig()->bytes_warning conditionally? I fear that this might be really slow :(

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

With this change, additional checks are required if we want to make COMPARE_OP able to constant-evaluate in the optimizer.

Did you mean checking for _Py_GetConfig()->bytes_warning conditionally? I fear that this might be really slow :(

You can fetch the value once at the start of optimization time in optimizer_analysis.c, and store it somewhere.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

Or, simpler, just explicitly disallow optimizing the problematic comparisons.

sobolevn reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

If one of the values is of type bytes, require the other to be as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

Or, simpler, just explicitly disallow optimizing the problematic comparisons.

Not just comparisons. BINARY_OP as well. This is a little annoying and more involved than I expected, so I'd say let's just ignore it for now?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

What BINARY_OP will raise BytesWarning?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

What BINARY_OP will raise BytesWarning?

Not BytesWarning. BINARY_OP (str, bytes) will throw.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

That’s the case for many combinations of safe types. We should suppress the error and hit bottom in that case (in the error label).

Fidget-Spinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

Same for 42 / 0, etc. In general, errors in the optimizer should just be suppressed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brandtbucher

brandtbucher commented Jun 27, 2025

Copy link
Copy Markdown
Member

This is just different because it’s a warning, to clarify.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

That’s the case for many combinations of safe types. We should suppress the error and hit bottom in that case (in the error label).

Yeah the optimizer currently doesn't do that. Opened a PR to do so #136048

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Jun 27, 2025

Copy link
Copy Markdown
Member Author

I opened https://discuss.python.org/t/consider-deprecating-and-eventually-removing-b-cli-flag/96903 about possibly removing this warning in the future versions. It does not seem very useful :(

efimov-mikhail reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn
Merge
ef66e03
@sobolevn

sobolevn commented Jun 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

I fixed the merge conflict. Sorry, I am rather new to the JIT internals, and I don't quite understand the COMPARE_OP part. _Py_uop_sym_is_safe_const is never called for COMPARE_OP. So, I don't know how to interpret these comments :)

With this change, additional checks are required if we want to make COMPARE_OP able to constant-evaluate in the optimizer.

Or, simpler, just explicitly disallow optimizing the problematic comparisons.

Is there anything I need to do in this PR to address them?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Jun 27, 2025

Copy link
Copy Markdown
Member

I fixed the merge conflict. Sorry, I am rather new to the JIT internals, and I don't quite understand the COMPARE_OP part. _Py_uop_sym_is_safe_const is never called for COMPARE_OP. So, I don't know how to interpret these comments :)

We plan to follow up shortly with COMPARE_OP here #130415

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Jun 27, 2025

Copy link
Copy Markdown
Member Author

Ok then :)
Let's close it for now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn sobolevn closed this Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Fidget-Spinner Fidget-Spinner Fidget-Spinner left review comments
@tomasr8 tomasr8 tomasr8 approved these changes
@brandtbucher brandtbucher Awaiting requested review from brandtbucher

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting merge skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@sobolevn @brandtbucher @Fidget-Spinner @tomasr8
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.