Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

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
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ David Harrigan
Brian Harring
Jonathan Hartley
Travis B. Hartwell
Shane Harvey
Larry Hastings
Tim Hatch
Shane Hathaway
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.