Kaydet (Commit) c4c98660 authored tarafından Shane Harvey's avatar Shane Harvey Kaydeden (comit) Serhiy Storchaka

bpo-31107: Fix copyreg mangled slot names calculation. (#2989)

üst 778928b0
...@@ -128,7 +128,11 @@ def _slotnames(cls): ...@@ -128,7 +128,11 @@ def _slotnames(cls):
continue continue
# mangled names # mangled names
elif name.startswith('__') and not name.endswith('__'): elif name.startswith('__') and not name.endswith('__'):
names.append('_%s%s' % (c.__name__, name)) stripped = c.__name__.lstrip('_')
if stripped:
names.append('_%s%s' % (stripped, name))
else:
names.append(name)
else: else:
names.append(name) names.append(name)
......
...@@ -16,6 +16,12 @@ class WithWeakref(object): ...@@ -16,6 +16,12 @@ class WithWeakref(object):
class WithPrivate(object): class WithPrivate(object):
__slots__ = ('__spam',) __slots__ = ('__spam',)
class _WithLeadingUnderscoreAndPrivate(object):
__slots__ = ('__spam',)
class ___(object):
__slots__ = ('__spam',)
class WithSingleString(object): class WithSingleString(object):
__slots__ = 'spam' __slots__ = 'spam'
...@@ -104,6 +110,10 @@ class CopyRegTestCase(unittest.TestCase): ...@@ -104,6 +110,10 @@ class CopyRegTestCase(unittest.TestCase):
self.assertEqual(copyreg._slotnames(WithWeakref), []) self.assertEqual(copyreg._slotnames(WithWeakref), [])
expected = ['_WithPrivate__spam'] expected = ['_WithPrivate__spam']
self.assertEqual(copyreg._slotnames(WithPrivate), expected) self.assertEqual(copyreg._slotnames(WithPrivate), expected)
expected = ['_WithLeadingUnderscoreAndPrivate__spam']
self.assertEqual(copyreg._slotnames(_WithLeadingUnderscoreAndPrivate),
expected)
self.assertEqual(copyreg._slotnames(___), ['__spam'])
self.assertEqual(copyreg._slotnames(WithSingleString), ['spam']) self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
expected = ['eggs', 'spam'] expected = ['eggs', 'spam']
expected.sort() expected.sort()
......
...@@ -598,6 +598,7 @@ David Harrigan ...@@ -598,6 +598,7 @@ David Harrigan
Brian Harring Brian Harring
Jonathan Hartley Jonathan Hartley
Travis B. Hartwell Travis B. Hartwell
Shane Harvey
Larry Hastings Larry Hastings
Tim Hatch Tim Hatch
Shane Hathaway Shane Hathaway
......
Fix `copyreg._slotnames()` mangled attribute calculation for classes whose
name begins with an underscore. Patch by Shane Harvey.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment