Skip to content
Draft
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.
78 changes: 47 additions & 31 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13084,6 +13084,51 @@
must be a string, whose characters will be mapped to None in the result.
[clinic start generated code]*/

static int
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
{
PyObject *key, *value;
Py_ssize_t i = 0;
int res;

Py_BEGIN_CRITICAL_SECTION(x);
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");
goto error;
}
kind = PyUnicode_KIND(key);
data = PyUnicode_DATA(key);
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
if (!newkey)
goto error;
res = PyDict_SetItem(newdict, newkey, value);
Py_DECREF(newkey);
if(res < 0)
goto error;
}
else if (PyLong_Check(key)) {
if (PyDict_SetItem(newdict, key, value) < 0)
goto error;
}
else {
PyErr_SetString(PyExc_TypeError, "keys in translate table must "
"be strings or integers");
goto error;
}
}
Py_END_CRITICAL_SECTION();
return 0;
error:

Check failure on line 13127 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 warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'_py_cs': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'_PyCriticalSection_End': different types for formal and actual parameter 1 [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'function': 'PyThreadState *' differs in levels of indirection from 'int' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'_cs_tstate': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 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 warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'function': incompatible types - from 'int *' to 'PyCriticalSection *' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'_py_cs': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'_PyCriticalSection_End': different types for formal and actual parameter 1 [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'function': 'PyThreadState *' differs in levels of indirection from 'int' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'_cs_tstate': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13128 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 -1;

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

syntax error: 'return' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

syntax error: 'return' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

syntax error: 'return' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

syntax error: 'return' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

expected identifier or ‘(’ before ‘return’

Check failure on line 13129 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

expected identifier or ‘(’ before ‘return’
}

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

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

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

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

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

syntax error: '}' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

syntax error: '}' [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

expected identifier or ‘(’ before ‘}’ token

Check failure on line 13130 in Objects/unicodeobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

expected identifier or ‘(’ before ‘}’ token

static PyObject *
unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
/*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/
Expand Down Expand Up @@ -13145,44 +13190,15 @@
}
}
} 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;
}
}
if(unicode_maketrans_from_dict(x, new) < 0)
goto err;
}
return new;
err:
Expand Down
Loading