[2.7] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989). (#3004)
https://github.com/python/cpython/commit/88ffff5ddbb8692ecb2c661aa29000dbde9... commit: 88ffff5ddbb8692ecb2c661aa29000dbde9eff8c branch: 2.7 author: Shane Harvey <shane.harvey@mongodb.com> committer: Serhiy Storchaka <storchaka@gmail.com> date: 2017-08-05T18:03:01+03:00 summary: [2.7] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989). (#3004) (cherry picked from commit c4c9866064f03646c686d7e08b00aeb203c35c19) files: A Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst M Lib/copy_reg.py M Lib/test/test_copy_reg.py M Misc/ACKS diff --git a/Lib/copy_reg.py b/Lib/copy_reg.py index db1715092c5..8943077593d 100644 --- a/Lib/copy_reg.py +++ b/Lib/copy_reg.py @@ -127,7 +127,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) diff --git a/Lib/test/test_copy_reg.py b/Lib/test/test_copy_reg.py index 8cdb8b7d2ab..17ccbd084d6 100644 --- a/Lib/test/test_copy_reg.py +++ b/Lib/test/test_copy_reg.py @@ -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 @@ def test_slotnames(self): 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() diff --git a/Misc/ACKS b/Misc/ACKS index 662db9f40f7..229a874ffc2 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -543,6 +543,7 @@ David Harrigan Brian Harring Jonathan Hartley Travis B. Hartwell +Shane Harvey Larry Hastings Tim Hatch Shane Hathaway diff --git a/Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst b/Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst new file mode 100644 index 00000000000..09807058b4c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst @@ -0,0 +1,2 @@ +Fix `copy_reg._slotnames()` mangled attribute calculation for classes whose +name begins with an underscore. Patch by Shane Harvey.
participants (1)
-
Serhiy Storchaka