Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_free_threading/test_str.py
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()
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.
17 changes: 12 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13155,34 +13155,41 @@
goto err;
}
/* copy entries into the new dict, converting string keys to int keys */
Py_BEGIN_CRITICAL_SECTION(x);
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;
goto error;
}
kind = PyUnicode_KIND(key);
data = PyUnicode_DATA(key);
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
if (!newkey)
goto err;
goto error;
res = PyDict_SetItem(new, newkey, value);
Py_DECREF(newkey);
if (res < 0)
goto err;
goto error;
} else if (PyLong_Check(key)) {
/* just keep integer keys */
if (PyDict_SetItem(new, key, value) < 0)
goto err;
goto error;
} else {
PyErr_SetString(PyExc_TypeError, "keys in translate table must "
"be strings or integers");
goto err;
goto error;
}
}
goto done;
error:
Py_CLEAR(new);
done:

Check failure on line 13190 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

label at end of compound statement
Py_END_CRITICAL_SECTION();

Check failure on line 13191 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

syntax error: missing ';' before '}' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13191 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

syntax error: missing ';' before '}' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
return new;
}
return new;
err:
Expand Down
Loading