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
Using __slots__ with str derived classes can cause segfault #38693
Comments
The following results in a segfault on Linux with 2.3b1: class StringDerived(str):
__slots__ = ['a']
def __init__(self, string):
str.__init__(self, string)
self.a = 1
class DerivedAgain(StringDerived):
pass
o = DerivedAgain('hello world')
o.b = 2 Python 2.2.2 raises a TypeError when attempting to I have no use case for deriving from str and defining |
Logged In: YES unicode, list, and dict don't crash, only string. |
Logged In: YES I think the problem is that strings are variable length. I'll add a test later. |
Logged In: YES Ain't testing grand. Found a couple more problems. Patch 2 I'm thinking the slot offset should be fixed when it's set, |
Logged In: YES Neal, I don't think your patch can work, unless you >>> class C(str): __slots__ = ['a']
...
>>> s = C("hello world")
>>> s.a
'\x00\x00\x00\x00\x90\x86\x12\x08\x00\x00'
>>> I'd rather see the 2.2 situation back, where |
Logged In: YES Jeremy, I agree with Guido, I think nonempty slots shouldn't |
Logged In: YES My checkin 2.240 had the side-effect of restoring the Somehow the check for whether slots were allowed added a I don't think the change was intentional. In addition to
the string crash, it created the possiblity for mutable tuples:
>>> class T(tuple):
... __slots__ = "x", "y"
...
>>> t = T((1, 2))
>>> t
(1, 2)
>>> t.x
1
>>> t.y
2
>>> t.y = -1
>>> t
(1, -1) |
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: