bpo-31107: Fix copyreg mangled slot names calculation. (#2989)
diff --git a/Lib/copyreg.py b/Lib/copyreg.py
index ed29d71..bbe1af4 100644
--- a/Lib/copyreg.py
+++ b/Lib/copyreg.py
@@ -128,7 +128,11 @@
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 52e887c..e3f1cd8 100644
--- a/Lib/test/test_copyreg.py
+++ b/Lib/test/test_copyreg.py
@@ -16,6 +16,12 @@
class WithPrivate(object):
__slots__ = ('__spam',)
+class _WithLeadingUnderscoreAndPrivate(object):
+ __slots__ = ('__spam',)
+
+class ___(object):
+ __slots__ = ('__spam',)
+
class WithSingleString(object):
__slots__ = 'spam'
@@ -104,6 +110,10 @@
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()