bpo-31286, bpo-30024: Fixed stack usage in absolute imports with (#3217) · python/cpython@265fcc5

@@ -2673,28 +2673,34 @@ compiler_import_as(struct compiler *c, identifier name, identifier asname)

26732673

If there is a dot in name, we need to split it and emit a

26742674

IMPORT_FROM for each name.

26752675

*/

2676-

Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,

2677-

PyUnicode_GET_LENGTH(name), 1);

2676+

Py_ssize_t len = PyUnicode_GET_LENGTH(name);

2677+

Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);

26782678

if (dot == -2)

26792679

return 0;

26802680

if (dot != -1) {

26812681

/* Consume the base module name to get the first attribute */

2682-

Py_ssize_t pos = dot + 1;

2683-

while (dot != -1) {

2682+

while (1) {

2683+

Py_ssize_t pos = dot + 1;

26842684

PyObject *attr;

2685-

dot = PyUnicode_FindChar(name, '.', pos,

2686-

PyUnicode_GET_LENGTH(name), 1);

2685+

dot = PyUnicode_FindChar(name, '.', pos, len, 1);

26872686

if (dot == -2)

26882687

return 0;

2689-

attr = PyUnicode_Substring(name, pos,

2690-

(dot != -1) ? dot :

2691-

PyUnicode_GET_LENGTH(name));

2688+

attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);

26922689

if (!attr)

26932690

return 0;

26942691

ADDOP_O(c, IMPORT_FROM, attr, names);

26952692

Py_DECREF(attr);

2696-

pos = dot + 1;

2693+

if (dot == -1) {

2694+

break;

2695+

}

2696+

ADDOP(c, ROT_TWO);

2697+

ADDOP(c, POP_TOP);

26972698

}

2699+

if (!compiler_nameop(c, asname, Store)) {

2700+

return 0;

2701+

}

2702+

ADDOP(c, POP_TOP);

2703+

return 1;

26982704

}

26992705

return compiler_nameop(c, asname, Store);

27002706

}