[2.7] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989). (#3004)

(cherry picked from commit c4c9866064)
This commit is contained in:
Shane Harvey 2017-08-05 08:03:01 -07:00 committed by Serhiy Storchaka
parent 5fbb8e367d
commit 88ffff5ddb
4 changed files with 18 additions and 1 deletions

View File

@ -127,7 +127,11 @@ def _slotnames(cls):
continue
# mangled names
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:
names.append(name)

View File

@ -17,6 +17,12 @@ class WithWeakref(object):
class WithPrivate(object):
__slots__ = ('__spam',)
class _WithLeadingUnderscoreAndPrivate(object):
__slots__ = ('__spam',)
class ___(object):
__slots__ = ('__spam',)
class WithSingleString(object):
__slots__ = 'spam'
@ -105,6 +111,10 @@ class CopyRegTestCase(unittest.TestCase):
self.assertEqual(copy_reg._slotnames(WithWeakref), [])
expected = ['_WithPrivate__spam']
self.assertEqual(copy_reg._slotnames(WithPrivate), expected)
expected = ['_WithLeadingUnderscoreAndPrivate__spam']
self.assertEqual(copy_reg._slotnames(_WithLeadingUnderscoreAndPrivate),
expected)
self.assertEqual(copy_reg._slotnames(___), ['__spam'])
self.assertEqual(copy_reg._slotnames(WithSingleString), ['spam'])
expected = ['eggs', 'spam']
expected.sort()

View File

@ -543,6 +543,7 @@ David Harrigan
Brian Harring
Jonathan Hartley
Travis B. Hartwell
Shane Harvey
Larry Hastings
Tim Hatch
Shane Hathaway

View File

@ -0,0 +1,2 @@
Fix `copy_reg._slotnames()` mangled attribute calculation for classes whose
name begins with an underscore. Patch by Shane Harvey.