Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-115754: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES)#125581

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
vstinner wants to merge2 commits intopython:mainfromvstinner:empty_bytes2

Conversation

vstinner
Copy link
Member

@vstinnervstinner commentedOct 16, 2024
edited by bedevere-appbot
Loading

Replace PyBytes_FromStringAndSize(NULL, 0)
with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).

Replace PyBytes_FromStringAndSize(NULL, 0)with Py_GetConstant(Py_CONSTANT_EMPTY_BYTES).
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Please wait.

Copy link
Member

@serhiy-storchakaserhiy-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 am not comfortable with this change.

  1. There is nothing wrong in usingPyBytes_FromStringAndSize(NULL, 0). It works, it worked, and it will always work. This is not inefficient or private API.
  2. PyBytes_FromStringAndSize() returns a new reference, whilePy_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.
  3. 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.

@vstinner
Copy link
MemberAuthor

PyBytes_FromStringAndSize() returns a new reference, while Py_GetConstant() returns a borrowed reference.

Py_GetConstant() returns a new reference:https://docs.python.org/dev/c-api/object.html#c.Py_GetConstant

erlend-aasland reacted with thumbs up emoji

@serhiy-storchaka
Copy link
Member

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.

@vstinner
Copy link
MemberAuthor

I would like to get rid ofPyBytes_FromStringAndSize(NULL, size) calls which create anincomplete bytes object, butPyBytes_FromStringAndSize(NULL, 0) doesn't have this issue.

Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) is simpler and a little bit faster thanPyBytes_FromStringAndSize(NULL, 0).

In general, I would prefer to makePy_GetConstant(Py_CONSTANT_EMPTY_BYTES) the reference function to get an empty byte string.

erlend-aasland reacted with thumbs up emoji

@vstinner
Copy link
MemberAuthor

@vstinner
Copy link
MemberAuthor

I failed to convince@serhiy-storchaka, so I just close my PR.

erlend-aasland reacted with confused emoji

@vstinnervstinner deleted the empty_bytes2 branchNovember 6, 2024 15:26
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@serhiy-storchakaserhiy-storchakaserhiy-storchaka left review comments

@erlend-aaslanderlend-aaslanderlend-aasland approved these changes

@corona10corona10Awaiting requested review from corona10corona10 is a code owner

@berkerpeksagberkerpeksagAwaiting requested review from berkerpeksagberkerpeksag is a code owner

@pgansslepganssleAwaiting requested review from pgansslepganssle is a code owner

@abalkinabalkinAwaiting requested review from abalkinabalkin is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@vstinner@serhiy-storchaka@erlend-aasland

[8]ページ先頭

©2009-2025 Movatter.jp