https://github.com/python/cpython/commit/1dfbde9299854c9f056250a658ccf9820bc54df3
commit: 1dfbde9299854c9f056250a658ccf9820bc54df3
branch: main
author: Stan Ulbrych <[email protected]>
committer: rhettinger <[email protected]>
date: 2026-02-23T16:04:16-06:00
summary:

gh-145118: Add `frozendict` support to `str.maketrans()` (gh-145129)

Add support to `str.maketrans`

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-02-22-22-05-09.gh-issue-145118.TaKMJE.rst
M Lib/test/test_str.py
M Objects/unicodeobject.c

diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index 0a8dddb026f6c8..4f57499af70f4d 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -454,6 +454,13 @@ def test_maketrans_translate(self):
         self.assertEqual("[a\xe9]".translate(str.maketrans({'a': '<\u20ac>'})),
                          "[<\u20ac>\xe9]")
 
+        # with frozendict
+        tbl = self.type2test.maketrans(frozendict({'s': 'S', 'T': 't'}))
+        self.assertEqual(tbl, {ord('s'): 'S', ord('T'): 't'})
+        self.assertEqual('sTan'.translate(tbl), 'Stan')
+        tbl = self.type2test.maketrans(frozendict({'a': None, 'b': '<i>'}))
+        self.checkequalnofix('<i><i><i>c', 'abababc', 'translate', tbl)
+
         # invalid Unicode characters
         invalid_char = 0x10ffff+1
         for before in "a\xe9\u20ac\U0010ffff":
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-22-22-05-09.gh-issue-145118.TaKMJE.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-22-22-05-09.gh-issue-145118.TaKMJE.rst
new file mode 100644
index 00000000000000..fccc3bc2a1804e
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-22-22-05-09.gh-issue-145118.TaKMJE.rst
@@ -0,0 +1 @@
+:meth:`str.maketrans` now accepts :class:`frozendict`.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index fdcbcf51cb62c2..988e5f95573fe1 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -13149,7 +13149,7 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, 
PyObject *z)
         const void *data;
 
         /* x must be a dict */
-        if (!PyDict_CheckExact(x)) {
+        if (!PyAnyDict_CheckExact(x)) {
             PyErr_SetString(PyExc_TypeError, "if you give only one argument "
                             "to maketrans it must be a dict");
             goto err;

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to