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
bpo-32431: Ensure two bytes objects of zero length compare equal #5021
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. Thanks again to your contribution and we look forward to looking at it! |
Have now signed the CLA, in case it matters. |
I don't think this fix merits a NEWS item, does it? If not, could you add a "skip news" label. |
return 0; | ||
|
||
if (lena == 0 && lenb == 0) | ||
return 1; | ||
|
||
if (a->ob_sval[0] != b->ob_sval[0]) |
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.
Why not just drop this (a->ob_sval[0] != b->ob_sval[0])
check? That would be simpler and also solve your problem.
For any other core devs, there is some debate on the issue tracker about changing these semantics. |
With the current logic in
Objects/bytesobject.c
in the functionbytes_compare_eq
can be the case that zero length bytes object object created in an extension module like this:won't compare equal to
b''
because the memory is not initialized, so the first two bytes won't be equal. Nonetheless, the Python interpreter does returnb''
forprint(repr(val))
, so this behaviour is very confusing. To get the correct behaviour, one would have to initialize the memory:This PR ensures that two zero length byte objects always compare equal irrespecitve of whether the memory has been initialized for either object.
https://bugs.python.org/issue32431