Skip to content

Commit c56d358

Browse files
Add new function
1 parent 7d95af1 commit c56d358

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

Objects/unicodeobject.c

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13084,6 +13084,51 @@ character at the same position in y. If there is a third argument, it
1308413084
must be a string, whose characters will be mapped to None in the result.
1308513085
[clinic start generated code]*/
1308613086

13087+
static int
13088+
unicode_maketrans_from_dict(PyObject *x, PyObject *newdict)
13089+
{
13090+
PyObject *key, *value;
13091+
Py_ssize_t i = 0;
13092+
int res;
13093+
13094+
Py_BEGIN_CRITICAL_SECTION(x);
13095+
while (PyDict_Next(x, &i, &key, &value)) {
13096+
if (PyUnicode_Check(key)) {
13097+
PyObject *newkey;
13098+
int kind;
13099+
const void *data;
13100+
if (PyUnicode_GET_LENGTH(key) != 1) {
13101+
PyErr_SetString(PyExc_ValueError, "string keys in translate "
13102+
"table must be of length 1");
13103+
goto error;
13104+
}
13105+
kind = PyUnicode_KIND(key);
13106+
data = PyUnicode_DATA(key);
13107+
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
13108+
if (!newkey)
13109+
goto error;
13110+
res = PyDict_SetItem(newdict, newkey, value);
13111+
Py_DECREF(newkey);
13112+
if(res < 0)
13113+
goto error;
13114+
}
13115+
else if (PyLong_Check(key)) {
13116+
if (PyDict_SetItem(newdict, key, value) < 0)
13117+
goto error;
13118+
}
13119+
else {
13120+
PyErr_SetString(PyExc_TypeError, "keys in translate table must "
13121+
"be strings or integers");
13122+
goto error;
13123+
}
13124+
}
13125+
Py_END_CRITICAL_SECTION();
13126+
return 0;
13127+
error:
13128+
Py_END_CRITICAL_SECTION();
13129+
return -1;
13130+
}
13131+
1308713132
static PyObject *
1308813133
unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
1308913134
/*[clinic end generated code: output=a925c89452bd5881 input=7bfbf529a293c6c5]*/
@@ -13145,51 +13190,15 @@ unicode_maketrans_impl(PyObject *x, PyObject *y, PyObject *z)
1314513190
}
1314613191
}
1314713192
} else {
13148-
int kind;
13149-
const void *data;
13150-
1315113193
/* x must be a dict */
1315213194
if (!PyDict_CheckExact(x)) {
1315313195
PyErr_SetString(PyExc_TypeError, "if you give only one argument "
1315413196
"to maketrans it must be a dict");
1315513197
goto err;
1315613198
}
1315713199
/* copy entries into the new dict, converting string keys to int keys */
13158-
Py_BEGIN_CRITICAL_SECTION(x);
13159-
while (PyDict_Next(x, &i, &key, &value)) {
13160-
if (PyUnicode_Check(key)) {
13161-
/* convert string keys to integer keys */
13162-
PyObject *newkey;
13163-
if (PyUnicode_GET_LENGTH(key) != 1) {
13164-
PyErr_SetString(PyExc_ValueError, "string keys in translate "
13165-
"table must be of length 1");
13166-
goto error;
13167-
}
13168-
kind = PyUnicode_KIND(key);
13169-
data = PyUnicode_DATA(key);
13170-
newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
13171-
if (!newkey)
13172-
goto error;
13173-
res = PyDict_SetItem(new, newkey, value);
13174-
Py_DECREF(newkey);
13175-
if (res < 0)
13176-
goto error;
13177-
} else if (PyLong_Check(key)) {
13178-
/* just keep integer keys */
13179-
if (PyDict_SetItem(new, key, value) < 0)
13180-
goto error;
13181-
} else {
13182-
PyErr_SetString(PyExc_TypeError, "keys in translate table must "
13183-
"be strings or integers");
13184-
goto error;
13185-
}
13186-
}
13187-
goto done;
13188-
error:
13189-
Py_CLEAR(new);
13190-
done:
13191-
Py_END_CRITICAL_SECTION();
13192-
return new;
13200+
if(unicode_maketrans_from_dict(x, new) < 0)
13201+
goto err;
1319313202
}
1319413203
return new;
1319513204
err:

0 commit comments

Comments
 (0)