GeezerVison is showing github.com — gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) by vstinner · Pull Request #125581 · python/cpython
Conversation
vstinner
commented
Oct 16, 2024
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Replace PyBytes_FromStringAndSize(NULL, 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Replace PyBytes_FromStringAndSize(NULL, 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not comfortable with this change.
- There is nothing wrong in using
PyBytes_FromStringAndSize(NULL, 0). It works, it worked, and it will always work. This is not inefficient or private API.
PyBytes_FromStringAndSize() returns a new reference, while Py_GetConstant() returns a borrowed reference. Even if an empty bytes object is immutable, there is a value of always using balanced incref/decref. It makes the intention clearer and the code more error-proof. When there are several exit points and some return a new reference and other return borrowed reference, you can immediately say that there is a bug. If it is not a bug, you will waste time and mental efforts in vain on analyzing the code.
- This creates a tiny obstacle for future backportings.
In summary, this is a cosmetic change which makes the code more difficult to maintain without significant gain.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
erlend-aasland reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Oh, nice. That eliminates most of my objections. There is still an issue with reasoning such change. This look like change for the sake of change.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I would like to get rid of PyBytes_FromStringAndSize(NULL, size) calls which create an incomplete bytes object, but PyBytes_FromStringAndSize(NULL, 0) doesn't have this issue.
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) is simpler and a little bit faster than PyBytes_FromStringAndSize(NULL, 0).
In general, I would prefer to make Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) the reference function to get an empty byte string.
|
erlend-aasland reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
erlend-aasland reacted with confused emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|