Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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 16 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.
76 changes: 45 additions & 31 deletions 76 Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -13366,6 +13366,45 @@ unicode_swapcase_impl(PyObject *self)
return case_operation(self, do_swapcase);
}

static int
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
{
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 @@ -13451,44 +13490,19 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
}
}
} else {
int kind;
const void *data;

/* x must be a dict */
if (!PyDict_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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.