]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31107: Fix copyreg mangled slot names calculation. (GH-2989) (#3003)
authorShane Harvey <shane.harvey@mongodb.com>
Sat, 5 Aug 2017 15:01:10 +0000 (08:01 -0700)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 5 Aug 2017 15:01:10 +0000 (18:01 +0300)
(cherry picked from commit c4c9866064f03646c686d7e08b00aeb203c35c19)

Lib/copyreg.py
Lib/test/test_copyreg.py
Misc/ACKS
Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst [new file with mode: 0644]

index 67f5bb029ee8f12e414130a7c5ef19c82199b2ff..cef9984b1928c9239a62b8f2121eed44e866ff17 100644 (file)
@@ -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)
 
index 52e887cb36c41e73d863d396258e0cbb16c3ebcd..e3f1cd81aab205259f8d5a0afa73c5e263858229 100644 (file)
@@ -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()
index 7bc13ff0c9e55aed3d3aa966b6d1385bf0140384..20e174384ba2c3a7fe145fd1941cbad078c3204a 100644 (file)
--- 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 (file)
index 0000000..3c2a155
--- /dev/null
@@ -0,0 +1,2 @@
+Fix `copyreg._slotnames()` mangled attribute calculation for classes whose
+name begins with an underscore. Patch by Shane Harvey.