Skip to content

Commit 573b44c

Browse files
committed
Issue 22189: Add missing methods to UserString
1 parent ab89f9c commit 573b44c

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

‎Lib/collections/__init__.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@ def __int__(self): return int(self.data)
10601060
def __float__(self): return float(self.data)
10611061
def __complex__(self): return complex(self.data)
10621062
def __hash__(self): return hash(self.data)
1063+
def __getnewargs__(self):
1064+
return (self.data[:],)
10631065

10641066
def __eq__(self, string):
10651067
if isinstance(string, UserString):
@@ -1104,9 +1106,13 @@ def __mul__(self, n):
11041106
__rmul__ = __mul__
11051107
def __mod__(self, args):
11061108
return self.__class__(self.data % args)
1109+
def __rmod__(self, format):
1110+
return self.__class__(format % args)
11071111

11081112
# the following methods are defined in alphabetical order:
11091113
def capitalize(self): return self.__class__(self.data.capitalize())
1114+
def casefold(self):
1115+
return self.__class__(self.data.casefold())
11101116
def center(self, width, *args):
11111117
return self.__class__(self.data.center(width, *args))
11121118
def count(self, sub, start=0, end=_sys.maxsize):
@@ -1129,6 +1135,8 @@ def find(self, sub, start=0, end=_sys.maxsize):
11291135
return self.data.find(sub, start, end)
11301136
def format(self, *args, **kwds):
11311137
return self.data.format(*args, **kwds)
1138+
def format_map(self, mapping):
1139+
return self.data.format_map(mapping)
11321140
def index(self, sub, start=0, end=_sys.maxsize):
11331141
return self.data.index(sub, start, end)
11341142
def isalpha(self): return self.data.isalpha()
@@ -1138,6 +1146,7 @@ def isdigit(self): return self.data.isdigit()
11381146
def isidentifier(self): return self.data.isidentifier()
11391147
def islower(self): return self.data.islower()
11401148
def isnumeric(self): return self.data.isnumeric()
1149+
def isprintable(self): return self.data.isprintable()
11411150
def isspace(self): return self.data.isspace()
11421151
def istitle(self): return self.data.istitle()
11431152
def isupper(self): return self.data.isupper()
@@ -1146,6 +1155,7 @@ def ljust(self, width, *args):
11461155
return self.__class__(self.data.ljust(width, *args))
11471156
def lower(self): return self.__class__(self.data.lower())
11481157
def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
1158+
maketrans = str.maketrans
11491159
def partition(self, sep):
11501160
return self.data.partition(sep)
11511161
def replace(self, old, new, maxsplit=-1):

‎Lib/test/test_collections.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import re
1313
import sys
1414
import types
15-
from collections import UserDict
15+
from collections import UserDict, UserString, UserList
1616
from collections import ChainMap
1717
from collections import deque
1818
from collections.abc import Awaitable, Coroutine, AsyncIterator, AsyncIterable
@@ -24,6 +24,26 @@
2424
from collections.abc import ByteString
2525

2626

27+
class TestUserObjects(unittest.TestCase):
28+
def _superset_test(self, a, b):
29+
self.assertGreaterEqual(
30+
set(dir(a)),
31+
set(dir(b)),
32+
'{a} should have all the methods of {b}'.format(
33+
a=a.__name__,
34+
b=b.__name__,
35+
),
36+
)
37+
def test_str_protocol(self):
38+
self._superset_test(UserString, str)
39+
40+
def test_list_protocol(self):
41+
self._superset_test(UserList, list)
42+
43+
def test_dict_protocol(self):
44+
self._superset_test(UserDict, dict)
45+
46+
2747
################################################################################
2848
### ChainMap (helper class for configparser and the string module)
2949
################################################################################
@@ -1848,7 +1868,8 @@ def test_main(verbose=None):
18481868
NamedTupleDocs = doctest.DocTestSuite(module=collections)
18491869
test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
18501870
TestCollectionABCs, TestCounter, TestChainMap,
1851-
TestOrderedDict, GeneralMappingTests, SubclassMappingTests]
1871+
TestOrderedDict, GeneralMappingTests, SubclassMappingTests,
1872+
TestUserObjects]
18521873
support.run_unittest(*test_classes)
18531874
support.run_doctest(collections, verbose)
18541875

‎Misc/NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ Library
6464
- Issue 24230: The tempfile module now accepts bytes for prefix, suffix and dir
6565
parameters and returns bytes in such situations (matching the os module APIs).
6666

67+
- Issue #22189: collections.UserString now supports __getnewargs__(),
68+
__rmod__(), casefold(), format_map(), isprintable(), and maketrans().
69+
Patch by Joe Jevnik.
70+
6771
- Issue 24244: Prevents termination when an invalid format string is
6872
encountered on Windows in strftime.
6973

0 commit comments

Comments
 (0)