Skip to content

Commit 1dfbde9

Browse files
gh-145118: Add frozendict support to str.maketrans() (gh-145129)
Add support to `str.maketrans`
1 parent c7d7105 commit 1dfbde9

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

Lib/test/test_str.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,13 @@ def test_maketrans_translate(self):
454454
self.assertEqual("[a\xe9]".translate(str.maketrans({'a': '<\u20ac>'})),
455455
"[<\u20ac>\xe9]")
456456

457+
# with frozendict
458+
tbl = self.type2test.maketrans(frozendict({'s': 'S', 'T': 't'}))
459+
self.assertEqual(tbl, {ord('s'): 'S', ord('T'): 't'})
460+
self.assertEqual('sTan'.translate(tbl), 'Stan')
461+
tbl = self.type2test.maketrans(frozendict({'a': None, 'b': '<i>'}))
462+
self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl)
463+
457464
# invalid Unicode characters
458465
invalid_char = 0x10ffff+1
459466
for before in "a\xe9\u20ac\U0010ffff":
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`str.maketrans` now accepts :class:`frozendict`.

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13149,7 +13149,7 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
1314913149
const void *data;
1315013150

1315113151
/* x must be a dict */
13152-
if (!PyDict_CheckExact(x)) {
13152+
if (!PyAnyDict_CheckExact(x)) {
1315313153
PyErr_SetString(PyExc_TypeError, "if you give only one argument "
1315413154
"to maketrans it must be a dict");
1315513155
goto err;

0 commit comments

Comments
 (0)