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

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

index db1715092c5dcd0eb1e1a22c0957ee04851b6076..8943077593d8870517b82b592e8cf38bb6b38794 100644 (file)
@@ -127,7 +127,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 8cdb8b7d2ab41fde8bc6b65288b4b18fee18c6f0..17ccbd084d6366f256a63a545e172ea6270e26b9 100644 (file)
@@ -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 @@ class CopyRegTestCase(unittest.TestCase):
         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()
index 662db9f40f7826f4a798b36119c2f8591dadc935..229a874ffc2bcbd36785609c35e4bce20af2a8ad 100644 (file)
--- 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 (file)
index 0000000..0980705
--- /dev/null
@@ -0,0 +1,2 @@
+Fix `copy_reg._slotnames()` mangled attribute calculation for classes whose
+name begins with an underscore. Patch by Shane Harvey.