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 — bpo-39573: Use Py_SIZE() in s_set() of cfield.c by shihai1991 · Pull Request #18419 · python/cpython

Conversation

@shihai1991

@shihai1991 shihai1991 commented Feb 9, 2020 •
edited by bedevere-bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

https://bugs.python.org/issue39573

Sorry, something went wrong.

Uh oh!

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

All reactions
@shihai1991
Use Py_SIZE() in s_set() of cfield.c
02f0de2
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Feb 9, 2020
@bedevere-bot bedevere-bot added the awaiting review label Feb 9, 2020
@shihai1991 shihai1991 changed the title Use Py_SIZE() in s_set() of cfield.c bpo-39573: Use Py_SIZE() in s_set() of cfield.c Feb 9, 2020
@shihai1991
shihai1991 requested a review from vstinner February 9, 2020 05:49
@vstinner vstinner added the skip news label Feb 9, 2020
vstinner
vstinner requested changes Feb 9, 2020
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

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.

This change is unrelated to https://bugs.python.org/issue39573 If you consider that s_set() behavior is a bug, please open a separated issue.

Your change is backward incompatible. Example:

import ctypes

class MyStruct(ctypes.Structure):
    _fields_ = (
        ('char', ctypes.c_char * 5),
    )

s = MyStruct()
print(ascii(s.char), bytes(s))
s.char = b'a\0b\0'
print(ascii(s.char), bytes(s))

Current behavior:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00\x00\x00\x00'

=> only b'a\0' (2 bytes) are copied

With your PR:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00b\x00\x00'

=> only b'a\0b\0' (4 bytes) are copied

Sorry, something went wrong.

Uh oh!

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

shihai1991 reacted with hooray emoji
All reactions
  • 🎉 1 reaction
@bedevere-bot bedevere-bot added awaiting changes and removed awaiting review labels Feb 9, 2020
@bedevere-bot

bedevere-bot commented Feb 9, 2020

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Feb 9, 2020

Copy link
Copy Markdown
Member

cc @corona10

corona10 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.

@shihai1991

shihai1991 commented Feb 9, 2020 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

This change is unrelated to https://bugs.python.org/issue39573 If you consider that s_set() behavior is a bug, please open a separated issue.

Your change is backward incompatible. Example:

import ctypes

class MyStruct(ctypes.Structure):
    _fields_ = (
        ('char', ctypes.c_char * 5),
    )

s = MyStruct()
print(ascii(s.char), bytes(s))
s.char = b'a\0b\0'
print(ascii(s.char), bytes(s))

Current behavior:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00\x00\x00\x00'

=> only b'a\0' (2 bytes) are copied

With your PR:

b'' b'\x00\x00\x00\x00\x00'
b'a' b'a\x00b\x00\x00'

=> only b'a\0b\0' (4 bytes) are copied

Thanks for your quick feedback, victor.

This change is unrelated to https://bugs.python.org/issue39573

I found you do some macro porting operations, so I thought this macro porting operation relate to 39573 is fine(not the cause of a bug).

Your change is backward incompatible.

Great example, current tests not help me found this backward incompatible and your example have answer this question(/* XXX Why not Py_SIZE(value)? */).

I have debug it in my vm, the reason is:
value = b'a\0b\0'
data = PyBytes_AS_STRING(value) ==> "a"
strlen(data) ==> 1
Py_SIZE(value) ==> 4

image

Sorry for this noisy PR, guys!

All reactions

Sorry, something went wrong.

Uh oh!

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

@shihai1991 shihai1991 closed this Feb 9, 2020
@shihai1991

shihai1991 commented Feb 9, 2020

Copy link
Copy Markdown
Member Author

To prevent other developers break this backward incompatible again, i add victor's example in #18424

All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner vstinner mentioned this pull request Mar 12, 2024
[C API] Avoid accessing PyObject and PyVarObject members directly: add Py_SET_TYPE() and Py_IS_TYPE(), disallow Py_TYPE(obj)=type #83754
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner requested changes

Uh oh!

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

Assignees

No one assigned

Labels

awaiting changes 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

@shihai1991 @bedevere-bot @vstinner @the-knights-who-say-ni
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.