Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 744c34e

Browse files
committed
Cleanup import.c
* Replace PyUnicode_RPartition() with PyUnicode_FindChar() and PyUnicode_Substring() to avoid the creation of a temporary tuple. * Use PyUnicode_FromFormat() to build a string and avoid the single_dot ('.') singleton Thanks Serhiy Storchaka for your review.
1 parent 03e6061 commit 744c34e

1 file changed

Lines changed: 28 additions & 39 deletions

File tree

Python/import.c

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,6 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
13491349
_Py_IDENTIFIER(_find_and_load);
13501350
_Py_IDENTIFIER(_handle_fromlist);
13511351
_Py_IDENTIFIER(_lock_unlock_module);
1352-
_Py_static_string(single_dot, ".");
13531352
PyObject *abs_name = NULL;
13541353
PyObject *builtins_import = NULL;
13551354
PyObject *final_mod = NULL;
@@ -1471,19 +1470,25 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
14711470
}
14721471

14731472
if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) {
1474-
PyObject *partition = NULL;
1475-
PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1476-
if (borrowed_dot == NULL) {
1473+
Py_ssize_t dot;
1474+
1475+
if (PyUnicode_READY(package) < 0) {
14771476
goto error;
14781477
}
1479-
partition = PyUnicode_RPartition(package, borrowed_dot);
1480-
Py_DECREF(package);
1481-
if (partition == NULL) {
1478+
1479+
dot = PyUnicode_FindChar(package, '.',
1480+
0, PyUnicode_GET_LENGTH(package), -1);
1481+
if (dot == -2) {
14821482
goto error;
14831483
}
1484-
package = PyTuple_GET_ITEM(partition, 0);
1485-
Py_INCREF(package);
1486-
Py_DECREF(partition);
1484+
1485+
if (dot >= 0) {
1486+
PyObject *substr = PyUnicode_Substring(package, 0, dot);
1487+
if (substr == NULL) {
1488+
goto error;
1489+
}
1490+
Py_SETREF(package, substr);
1491+
}
14871492
}
14881493
}
14891494

@@ -1531,17 +1536,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
15311536
goto error;
15321537

15331538
if (PyUnicode_GET_LENGTH(name) > 0) {
1534-
PyObject *borrowed_dot, *seq = NULL;
1535-
1536-
borrowed_dot = _PyUnicode_FromId(&single_dot);
1537-
seq = PyTuple_Pack(2, base, name);
1539+
abs_name = PyUnicode_FromFormat("%U.%U", base, name);
15381540
Py_DECREF(base);
1539-
if (borrowed_dot == NULL || seq == NULL) {
1540-
goto error;
1541-
}
1542-
1543-
abs_name = PyUnicode_Join(borrowed_dot, seq);
1544-
Py_DECREF(seq);
15451541
if (abs_name == NULL) {
15461542
goto error;
15471543
}
@@ -1639,43 +1635,36 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
16391635
if (has_from < 0)
16401636
goto error;
16411637
if (!has_from) {
1642-
if (level == 0 || PyUnicode_GET_LENGTH(name) > 0) {
1643-
PyObject *front = NULL;
1644-
PyObject *partition = NULL;
1645-
PyObject *borrowed_dot = _PyUnicode_FromId(&single_dot);
1638+
Py_ssize_t len = PyUnicode_GET_LENGTH(name);
1639+
if (level == 0 || len > 0) {
1640+
Py_ssize_t dot;
16461641

1647-
if (borrowed_dot == NULL) {
1642+
dot = PyUnicode_FindChar(name, '.', 0, len, 1);
1643+
if (dot == -2) {
16481644
goto error;
16491645
}
16501646

1651-
partition = PyUnicode_Partition(name, borrowed_dot);
1652-
if (partition == NULL) {
1653-
goto error;
1654-
}
1655-
1656-
if (PyUnicode_GET_LENGTH(PyTuple_GET_ITEM(partition, 1)) == 0) {
1647+
if (dot == -1) {
16571648
/* No dot in module name, simple exit */
1658-
Py_DECREF(partition);
16591649
final_mod = mod;
16601650
Py_INCREF(mod);
16611651
goto error;
16621652
}
16631653

1664-
front = PyTuple_GET_ITEM(partition, 0);
1665-
Py_INCREF(front);
1666-
Py_DECREF(partition);
1667-
16681654
if (level == 0) {
1655+
PyObject *front = PyUnicode_Substring(name, 0, dot);
1656+
if (front == NULL) {
1657+
goto error;
1658+
}
1659+
16691660
final_mod = PyObject_CallFunctionObjArgs(builtins_import, front, NULL);
16701661
Py_DECREF(front);
16711662
}
16721663
else {
1673-
Py_ssize_t cut_off = PyUnicode_GET_LENGTH(name) -
1674-
PyUnicode_GET_LENGTH(front);
1664+
Py_ssize_t cut_off = len - dot;
16751665
Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
16761666
PyObject *to_return = PyUnicode_Substring(abs_name, 0,
16771667
abs_name_len - cut_off);
1678-
Py_DECREF(front);
16791668
if (to_return == NULL) {
16801669
goto error;
16811670
}

0 commit comments

Comments
 (0)