From 4795ba857ef2a89e6b477285df961672106a1792 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Sat, 5 Aug 2017 08:01:10 -0700 Subject: [PATCH] [3.6] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989) (#3003) (cherry picked from commit c4c9866064f03646c686d7e08b00aeb203c35c19) --- Lib/copyreg.py | 6 +++++- Lib/test/test_copyreg.py | 10 ++++++++++ Misc/ACKS | 1 + .../Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst | 2 ++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst diff --git a/Lib/copyreg.py b/Lib/copyreg.py index 67f5bb029e..cef9984b19 100644 --- a/Lib/copyreg.py +++ b/Lib/copyreg.py @@ -128,7 +128,11 @@ def _slotnames(cls): 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_copyreg.py b/Lib/test/test_copyreg.py index 52e887cb36..e3f1cd81aa 100644 --- a/Lib/test/test_copyreg.py +++ b/Lib/test/test_copyreg.py @@ -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' @@ -104,6 +110,10 @@ class CopyRegTestCase(unittest.TestCase): 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() diff --git a/Misc/ACKS b/Misc/ACKS index 7bc13ff0c9..20e174384b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -592,6 +592,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 0000000000..3c2a15528d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst @@ -0,0 +1,2 @@ +Fix `copyreg._slotnames()` mangled attribute calculation for classes whose +name begins with an underscore. Patch by Shane Harvey. -- 2.50.1