Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/copyreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ class found there. (This assumes classes don't modify their
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)

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_copyreg.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class WithWeakref(object):
class WithPrivate(object):
__slots__ = ('__spam',)

class _WithLeadingUnderscoreAndPrivate(object):
__slots__ = ('__spam',)

class ___(object):
__slots__ = ('__spam',)

class WithSingleString(object):
__slots__ = 'spam'

Expand Down Expand Up @@ -104,6 +110,10 @@ def test_slotnames(self):
self.assertEqual(copyreg._slotnames(WithWeakref), [])
expected = ['_WithPrivate__spam']
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'])
expected = ['eggs', 'spam']
expected.sort()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix `copyreg._slotnames()` mangled attribute calculation for classes whose
name begins with an underscore. Patch by Shane Harvey.