[Python-checkins] bpo-31107: Fix copyreg mangled slot names calculation. (#2989)

Serhiy Storchaka webhook-mailer at python.org
Fri Aug 4 04:45:06 EDT 2017


https://github.com/python/cpython/commit/c4c9866064f03646c686d7e08b00aeb203c35c19
commit: c4c9866064f03646c686d7e08b00aeb203c35c19
branch: master
author: Shane Harvey <shane.harvey at mongodb.com>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2017-08-04T11:45:00+03:00
summary:

bpo-31107: Fix copyreg mangled slot names calculation. (#2989)

files:
A Misc/NEWS.d/next/Library/2017-08-02-12-48-15.bpo-31107.1t2hn5.rst
M Lib/copyreg.py
M Lib/test/test_copyreg.py
M Misc/ACKS

diff --git a/Lib/copyreg.py b/Lib/copyreg.py
index ed29d719def..bbe1af4e2e7 100644
--- a/Lib/copyreg.py
+++ b/Lib/copyreg.py
@@ -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)
 
diff --git a/Lib/test/test_copyreg.py b/Lib/test/test_copyreg.py
index 52e887cb36c..e3f1cd81aab 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 @@ 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()
diff --git a/Misc/ACKS b/Misc/ACKS
index f1f6c147f49..ec7d481a587 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -598,6 +598,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..3c2a15528d8
--- /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.



More information about the Python-checkins mailing list