GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-145142: Make str.maketrans safe under free-threading by VanshAgarwal24036 · Pull Request #145157 · python/cpython

gh-145142: Make str.maketrans safe under free-threading #145157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
colesbury merged 13 commits into python:main from VanshAgarwal24036:gh-145142-dict-next-critical-section
Feb 27, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-145142: Make str.maketrans safe under free-threading #145157

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
0745e41
gh-145142: Make str.maketrans safe under free-threading
VanshAgarwal24036 Feb 23, 2026
3fdf54c
Add new line at end of test file
VanshAgarwal24036 Feb 23, 2026
9a75245
Update changes
VanshAgarwal24036 Feb 24, 2026
5071b9d
Add new line at end of news file
VanshAgarwal24036 Feb 24, 2026
b34fd09
Remove unnecessary paranthesis in news file
VanshAgarwal24036 Feb 24, 2026
044c347
Correct build error
VanshAgarwal24036 Feb 24, 2026
b7e9d1b
Update
VanshAgarwal24036 Feb 24, 2026
bfc812a
Add new function
VanshAgarwal24036 Feb 24, 2026
f7c3c45
Update helper fuction
VanshAgarwal24036 Feb 24, 2026
4e715b0
Update function
VanshAgarwal24036 Feb 24, 2026
b60b67c
Perform suggested changes
VanshAgarwal24036 Feb 24, 2026
28af6d8
Fix placement of clinic code
colesbury Feb 27, 2026
4994663
Merge branch 'main' into gh-145142-dict-next-critical-section
colesbury Feb 27, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

16 changes: 16 additions & 0 deletions 16 Lib/test/test_free_threading/test_str.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ def reader_func():
for reader in readers:
reader.join()

def test_maketrans_dict_concurrent_modification(self):
for _ in range(5):
d = {2000: 'a'}

def work(dct):
for i in range(100):
str.maketrans(dct)
dct[2000 + i] = chr(i % 16)
dct.pop(2000 + i, None)

threading_helper.run_concurrently(
work,
nthreads=5,
args=(d,),
)


if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Core_and_Builtins/2026-02-23-23-18-28.gh-issue-145142.T-XbVe.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in the free-threaded build when the dictionary argument to
:meth:`str.maketrans` is concurrently modified.
76 changes: 45 additions & 31 deletions 76 Objects/unicodeobject.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -13060,6 +13060,45 @@ unicode_swapcase_impl(PyObject *self)
return case_operation(self, do_swapcase);
}

static int
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
Comment thread

@eendebakpt eendebakpt Feb 27, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
unicode_maketrans_from_dict_lock_held(PyObject *x, PyObject *newdict)

Naming convention for methods that require locking.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
{
PyObject *key, *value;
Py_ssize_t i = 0;
int res;
while (PyDict_Next(x, &i, &key, &value)) {
if (PyUnicode_Check(key)) {
PyObject *newkey;
int kind;
const void *data;
if (PyUnicode_GET_LENGTH(key) != 1) {
PyErr_SetString(PyExc_ValueError, "string keys in translate"
"table must be of length 1");
return -1;
}
kind = PyUnicode_KIND(key);
data = PyUnicode_DATA(key);
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
if (!newkey)
return -1;
res = PyDict_SetItem(newdict, newkey, value);
Py_DECREF(newkey);
if (res < 0)
return -1;
}
else if (PyLong_Check(key)) {
if (PyDict_SetItem(newdict, key, value) < 0)
return -1;
}
else {
PyErr_SetString(PyExc_TypeError, "keys in translate table must"
"be strings or integers");
return -1;
}
}
return 0;
}

/*[clinic input]

@staticmethod
Expand Down Expand Up @@ -13145,44 +13184,19 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
}
}
} else {
int kind;
const void *data;

/* x must be a dict */
if (!PyAnyDict_CheckExact(x)) {
PyErr_SetString(PyExc_TypeError, "if you give only one argument "
"to maketrans it must be a dict");
goto err;
}
/* copy entries into the new dict, converting string keys to int keys */
while (PyDict_Next(x, &i, &key, &value)) {
if (PyUnicode_Check(key)) {
/* convert string keys to integer keys */
PyObject *newkey;
if (PyUnicode_GET_LENGTH(key) != 1) {
PyErr_SetString(PyExc_ValueError, "string keys in translate "
"table must be of length 1");
goto err;
}
kind = PyUnicode_KIND(key);
data = PyUnicode_DATA(key);
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
if (!newkey)
goto err;
res = PyDict_SetItem(new, newkey, value);
Py_DECREF(newkey);
if (res < 0)
goto err;
} else if (PyLong_Check(key)) {
/* just keep integer keys */
if (PyDict_SetItem(new, key, value) < 0)
goto err;
} else {
PyErr_SetString(PyExc_TypeError, "keys in translate table must "
"be strings or integers");
goto err;
}
}
int errcode;
Py_BEGIN_CRITICAL_SECTION(x);
errcode = unicode_maketrans_from_dict(x, new);
Py_END_CRITICAL_SECTION();
if (errcode < 0)
goto err;
}
return new;
err:
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.