Skip to content

Commit 4795ba8

Browse files
ShaneHarveyserhiy-storchaka
authored andcommitted
[3.6] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989) (#3003)
(cherry picked from commit c4c9866)
1 parent a0cb7db commit 4795ba8

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

Lib/copyreg.py

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

Lib/test/test_copyreg.py

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

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

@@ -104,6 +110,10 @@ def test_slotnames(self):
104110
self.assertEqual(copyreg._slotnames(WithWeakref), [])
105111
expected = ['_WithPrivate__spam']
106112
self.assertEqual(copyreg._slotnames(WithPrivate), expected)
113+
expected = ['_WithLeadingUnderscoreAndPrivate__spam']
114+
self.assertEqual(copyreg._slotnames(_WithLeadingUnderscoreAndPrivate),
115+
expected)
116+
self.assertEqual(copyreg._slotnames(___), ['__spam'])
107117
self.assertEqual(copyreg._slotnames(WithSingleString), ['spam'])
108118
expected = ['eggs', 'spam']
109119
expected.sort()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ David Harrigan
592592
Brian Harring
593593
Jonathan Hartley
594594
Travis B. Hartwell
595+
Shane Harvey
595596
Larry Hastings
596597
Tim Hatch
597598
Shane Hathaway
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix `copyreg._slotnames()` mangled attribute calculation for classes whose
2+
name begins with an underscore. Patch by Shane Harvey.

0 commit comments

Comments
 (0)