Skip to content

Commit 88ffff5

Browse files
ShaneHarveyserhiy-storchaka
authored andcommitted
[2.7] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989). (#3004)
(cherry picked from commit c4c9866)
1 parent 5fbb8e3 commit 88ffff5

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

Lib/copy_reg.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ class found there. (This assumes classes don't modify their
127127
continue
128128
# mangled names
129129
elif name.startswith('__') and not name.endswith('__'):
130-
names.append('_%s%s' % (c.__name__, name))
130+
stripped = c.__name__.lstrip('_')
131+
if stripped:
132+
names.append('_%s%s' % (stripped, name))
133+
else:
134+
names.append(name)
131135
else:
132136
names.append(name)
133137

Lib/test/test_copy_reg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class WithWeakref(object):
1717
class WithPrivate(object):
1818
__slots__ = ('__spam',)
1919

20+
class _WithLeadingUnderscoreAndPrivate(object):
21+
__slots__ = ('__spam',)
22+
23+
class ___(object):
24+
__slots__ = ('__spam',)
25+
2026
class WithSingleString(object):
2127
__slots__ = 'spam'
2228

@@ -105,6 +111,10 @@ def test_slotnames(self):
105111
self.assertEqual(copy_reg._slotnames(WithWeakref), [])
106112
expected = ['_WithPrivate__spam']
107113
self.assertEqual(copy_reg._slotnames(WithPrivate), expected)
114+
expected = ['_WithLeadingUnderscoreAndPrivate__spam']
115+
self.assertEqual(copy_reg._slotnames(_WithLeadingUnderscoreAndPrivate),
116+
expected)
117+
self.assertEqual(copy_reg._slotnames(___), ['__spam'])
108118
self.assertEqual(copy_reg._slotnames(WithSingleString), ['spam'])
109119
expected = ['eggs', 'spam']
110120
expected.sort()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ David Harrigan
543543
Brian Harring
544544
Jonathan Hartley
545545
Travis B. Hartwell
546+
Shane Harvey
546547
Larry Hastings
547548
Tim Hatch
548549
Shane Hathaway
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix `copy_reg._slotnames()` mangled attribute calculation for classes whose
2+
name begins with an underscore. Patch by Shane Harvey.

0 commit comments

Comments
 (0)