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
Incorrect assert in str_subtype_new #68919
Comments
(Using python 3 terminology) str_subtype_new is the function that creates instances of any subtypes of bytes (ie is called by bytes_new if the requested type is not PyBytes_Type -- looks like this function's name comes from python 2). Its approach is to create a bytes object using the same arguments, and then copy the resulting data into the subclass-instance's memory. It does tmp = bytes_new(&PyBytes_Type, args, kwds);
[error checking]
assert(PyBytes_CheckExact(tmp)); The problem is that bytes_new can return a subclass of bytes, if the argument provides a __bytes__ method that returns a bytes-subtype. For example class MyBytes(bytes):
pass
class C(object):
def __bytes__(self):
return MyBytes(b"hello world")
MyBytes(C()) # fails the assert This doesn't seem to cause any issues other than the failing assert in debug builds; it seems like the assert should just be relaxed from PyBytes_CheckExact to PyBytes_Check since that's enough to guarantee that the upcoming manipulation of the "tmp" variable is going to be valid. Also, this would match how unicode_subtype_new behaves. This bug also applies to Python 2, since I think the relevant code is the same, though in that case it applies to str instead of bytes. |
Thank you for your report Kevin and sorry for the delay. I confirm the bug and agree with your solution. The same bug exists for ints and floats. Here are patches that fix crashes. |
New changeset d8e0b54ece62 by Serhiy Storchaka in branch '3.4': New changeset 80efe5cc8934 by Serhiy Storchaka in branch '3.5': New changeset 1c4d256cc370 by Serhiy Storchaka in branch 'default': New changeset 37158c067b25 by Serhiy Storchaka in branch '2.7': |
New changeset 2ea1a3bf448f by Serhiy Storchaka in branch '2.7': |
Awesome, thanks! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: