summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2022-11-15 12:38:09 (GMT)
committerThomas Wouters <thomas@python.org>2022-11-15 12:38:09 (GMT)
commit73943cbc4c5e97f71b76150c549d07e8ed00066b (patch)
tree748ea6203fa3d36112565a67065e3a1bdc550b40 /Python
parentbd58b89654a98cbfb92a6e054ac285bd32cdf58f (diff)
parentf13f466474ed53529acd3f209070431fbae14323 (diff)
downloadcpython-73943cbc4c5e97f71b76150c549d07e8ed00066b.zip
cpython-73943cbc4c5e97f71b76150c549d07e8ed00066b.tar.gz
cpython-73943cbc4c5e97f71b76150c549d07e8ed00066b.tar.bz2
Merge the 3.12.0a2 release into main.
Diffstat (limited to 'Python')
-rw-r--r--Python/Python-ast.c217
-rw-r--r--Python/ast_unparse.c2
-rw-r--r--Python/compile.c173
-rw-r--r--Python/import.c15
-rw-r--r--Python/pystate.c16
-rw-r--r--Python/sysmodule.c2
6 files changed, 215 insertions, 210 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index b57aca3..d113c47 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -263,6 +263,10 @@ void _PyAST_Fini(PyInterpreterState *interp)
Py_CLEAR(state->vararg);
Py_CLEAR(state->withitem_type);
+ if (_PyInterpreterState_Get() == _PyInterpreterState_Main()) {
+ Py_CLEAR(_Py_CACHED_OBJECT(str_replace_inf));
+ }
+
#if !defined(NDEBUG)
state->initialized = -1;
#else
@@ -1019,9 +1023,11 @@ static int obj2ast_object(struct ast_state *Py_UNUSED(state), PyObject* obj, PyO
*out = NULL;
return -1;
}
- Py_INCREF(obj);
+ *out = Py_NewRef(obj);
+ }
+ else {
+ *out = NULL;
}
- *out = obj;
return 0;
}
@@ -5479,8 +5485,7 @@ obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Module' node")) {
goto failed;
}
@@ -5516,8 +5521,7 @@ obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
if (type_ignores == NULL) goto failed;
for (i = 0; i < len; i++) {
type_ignore_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Module' node")) {
goto failed;
}
@@ -5565,8 +5569,7 @@ obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Interactive' node")) {
goto failed;
}
@@ -5644,8 +5647,7 @@ obj2ast_mod(struct ast_state *state, PyObject* obj, mod_ty* out, PyArena* arena)
if (argtypes == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'FunctionType' node")) {
goto failed;
}
@@ -5841,8 +5843,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'FunctionDef' node")) {
goto failed;
}
@@ -5878,8 +5879,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (decorator_list == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'FunctionDef' node")) {
goto failed;
}
@@ -6002,8 +6002,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncFunctionDef' node")) {
goto failed;
}
@@ -6039,8 +6038,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (decorator_list == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncFunctionDef' node")) {
goto failed;
}
@@ -6146,8 +6144,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (bases == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ClassDef' node")) {
goto failed;
}
@@ -6183,8 +6180,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (keywords == NULL) goto failed;
for (i = 0; i < len; i++) {
keyword_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ClassDef' node")) {
goto failed;
}
@@ -6220,8 +6216,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ClassDef' node")) {
goto failed;
}
@@ -6257,8 +6252,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (decorator_list == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ClassDef' node")) {
goto failed;
}
@@ -6338,8 +6332,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (targets == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Delete' node")) {
goto failed;
}
@@ -6390,8 +6383,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (targets == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Assign' node")) {
goto failed;
}
@@ -6662,8 +6654,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'For' node")) {
goto failed;
}
@@ -6699,8 +6690,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'For' node")) {
goto failed;
}
@@ -6804,8 +6794,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncFor' node")) {
goto failed;
}
@@ -6841,8 +6830,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncFor' node")) {
goto failed;
}
@@ -6928,8 +6916,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'While' node")) {
goto failed;
}
@@ -6965,8 +6952,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'While' node")) {
goto failed;
}
@@ -7034,8 +7020,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'If' node")) {
goto failed;
}
@@ -7071,8 +7056,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'If' node")) {
goto failed;
}
@@ -7123,8 +7107,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (items == NULL) goto failed;
for (i = 0; i < len; i++) {
withitem_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'With' node")) {
goto failed;
}
@@ -7160,8 +7143,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'With' node")) {
goto failed;
}
@@ -7229,8 +7211,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (items == NULL) goto failed;
for (i = 0; i < len; i++) {
withitem_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncWith' node")) {
goto failed;
}
@@ -7266,8 +7247,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'AsyncWith' node")) {
goto failed;
}
@@ -7351,8 +7331,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (cases == NULL) goto failed;
for (i = 0; i < len; i++) {
match_case_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Match' node")) {
goto failed;
}
@@ -7452,8 +7431,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Try' node")) {
goto failed;
}
@@ -7489,8 +7467,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (handlers == NULL) goto failed;
for (i = 0; i < len; i++) {
excepthandler_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Try' node")) {
goto failed;
}
@@ -7526,8 +7503,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Try' node")) {
goto failed;
}
@@ -7563,8 +7539,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (finalbody == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Try' node")) {
goto failed;
}
@@ -7616,8 +7591,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'TryStar' node")) {
goto failed;
}
@@ -7653,8 +7627,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (handlers == NULL) goto failed;
for (i = 0; i < len; i++) {
excepthandler_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'TryStar' node")) {
goto failed;
}
@@ -7690,8 +7663,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (orelse == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'TryStar' node")) {
goto failed;
}
@@ -7727,8 +7699,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (finalbody == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'TryStar' node")) {
goto failed;
}
@@ -7825,8 +7796,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (names == NULL) goto failed;
for (i = 0; i < len; i++) {
alias_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Import' node")) {
goto failed;
}
@@ -7894,8 +7864,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (names == NULL) goto failed;
for (i = 0; i < len; i++) {
alias_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ImportFrom' node")) {
goto failed;
}
@@ -7961,8 +7930,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (names == NULL) goto failed;
for (i = 0; i < len; i++) {
identifier val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Global' node")) {
goto failed;
}
@@ -8011,8 +7979,7 @@ obj2ast_stmt(struct ast_state *state, PyObject* obj, stmt_ty* out, PyArena*
if (names == NULL) goto failed;
for (i = 0; i < len; i++) {
identifier val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Nonlocal' node")) {
goto failed;
}
@@ -8237,8 +8204,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (values == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'BoolOp' node")) {
goto failed;
}
@@ -8564,8 +8530,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (keys == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Dict' node")) {
goto failed;
}
@@ -8601,8 +8566,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (values == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Dict' node")) {
goto failed;
}
@@ -8651,8 +8615,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (elts == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Set' node")) {
goto failed;
}
@@ -8719,8 +8682,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (generators == NULL) goto failed;
for (i = 0; i < len; i++) {
comprehension_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ListComp' node")) {
goto failed;
}
@@ -8787,8 +8749,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (generators == NULL) goto failed;
for (i = 0; i < len; i++) {
comprehension_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'SetComp' node")) {
goto failed;
}
@@ -8873,8 +8834,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (generators == NULL) goto failed;
for (i = 0; i < len; i++) {
comprehension_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'DictComp' node")) {
goto failed;
}
@@ -8941,8 +8901,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (generators == NULL) goto failed;
for (i = 0; i < len; i++) {
comprehension_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'GeneratorExp' node")) {
goto failed;
}
@@ -9100,8 +9059,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (ops == NULL) goto failed;
for (i = 0; i < len; i++) {
cmpop_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Compare' node")) {
goto failed;
}
@@ -9137,8 +9095,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (comparators == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Compare' node")) {
goto failed;
}
@@ -9206,8 +9163,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (args == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Call' node")) {
goto failed;
}
@@ -9243,8 +9199,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (keywords == NULL) goto failed;
for (i = 0; i < len; i++) {
keyword_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Call' node")) {
goto failed;
}
@@ -9360,8 +9315,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (values == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'JoinedStr' node")) {
goto failed;
}
@@ -9687,8 +9641,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (elts == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'List' node")) {
goto failed;
}
@@ -9755,8 +9708,7 @@ obj2ast_expr(struct ast_state *state, PyObject* obj, expr_ty* out, PyArena*
if (elts == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'Tuple' node")) {
goto failed;
}
@@ -10242,8 +10194,7 @@ obj2ast_comprehension(struct ast_state *state, PyObject* obj, comprehension_ty*
if (ifs == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'comprehension' node")) {
goto failed;
}
@@ -10432,8 +10383,7 @@ obj2ast_excepthandler(struct ast_state *state, PyObject* obj, excepthandler_ty*
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'ExceptHandler' node")) {
goto failed;
}
@@ -10494,8 +10444,7 @@ obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
if (posonlyargs == NULL) goto failed;
for (i = 0; i < len; i++) {
arg_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'arguments' node")) {
goto failed;
}
@@ -10531,8 +10480,7 @@ obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
if (args == NULL) goto failed;
for (i = 0; i < len; i++) {
arg_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'arguments' node")) {
goto failed;
}
@@ -10585,8 +10533,7 @@ obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
if (kwonlyargs == NULL) goto failed;
for (i = 0; i < len; i++) {
arg_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'arguments' node")) {
goto failed;
}
@@ -10622,8 +10569,7 @@ obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
if (kw_defaults == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'arguments' node")) {
goto failed;
}
@@ -10676,8 +10622,7 @@ obj2ast_arguments(struct ast_state *state, PyObject* obj, arguments_ty* out,
if (defaults == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'arguments' node")) {
goto failed;
}
@@ -11196,8 +11141,7 @@ obj2ast_match_case(struct ast_state *state, PyObject* obj, match_case_ty* out,
if (body == NULL) goto failed;
for (i = 0; i < len; i++) {
stmt_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'match_case' node")) {
goto failed;
}
@@ -11393,8 +11337,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (patterns == NULL) goto failed;
for (i = 0; i < len; i++) {
pattern_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchSequence' node")) {
goto failed;
}
@@ -11445,8 +11388,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (keys == NULL) goto failed;
for (i = 0; i < len; i++) {
expr_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchMapping' node")) {
goto failed;
}
@@ -11482,8 +11424,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (patterns == NULL) goto failed;
for (i = 0; i < len; i++) {
pattern_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchMapping' node")) {
goto failed;
}
@@ -11569,8 +11510,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (patterns == NULL) goto failed;
for (i = 0; i < len; i++) {
pattern_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchClass' node")) {
goto failed;
}
@@ -11606,8 +11546,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (kwd_attrs == NULL) goto failed;
for (i = 0; i < len; i++) {
identifier val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchClass' node")) {
goto failed;
}
@@ -11643,8 +11582,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (kwd_patterns == NULL) goto failed;
for (i = 0; i < len; i++) {
pattern_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchClass' node")) {
goto failed;
}
@@ -11772,8 +11710,7 @@ obj2ast_pattern(struct ast_state *state, PyObject* obj, pattern_ty* out,
if (patterns == NULL) goto failed;
for (i = 0; i < len; i++) {
pattern_ty val;
- PyObject *tmp2 = PyList_GET_ITEM(tmp, i);
- Py_INCREF(tmp2);
+ PyObject *tmp2 = Py_NewRef(PyList_GET_ITEM(tmp, i));
if (_Py_EnterRecursiveCall(" while traversing 'MatchOr' node")) {
goto failed;
}
diff --git a/Python/ast_unparse.c b/Python/ast_unparse.c
index 6565b6b..79b2e2f 100644
--- a/Python/ast_unparse.c
+++ b/Python/ast_unparse.c
@@ -13,7 +13,7 @@ _Py_DECLARE_STR(open_br, "{");
_Py_DECLARE_STR(dbl_open_br, "{{");
_Py_DECLARE_STR(close_br, "}");
_Py_DECLARE_STR(dbl_close_br, "}}");
-static PyObject *_str_replace_inf;
+#define _str_replace_inf _Py_CACHED_OBJECT(str_replace_inf)
/* Forward declarations for recursion via helper functions. */
static PyObject *
diff --git a/Python/compile.c b/Python/compile.c
index 37f3b23..bd41ebc 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -122,7 +122,7 @@
(opcode) == STORE_FAST__STORE_FAST)
#define IS_TOP_LEVEL_AWAIT(c) ( \
- (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
+ (c->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
&& (c->u->u_ste->ste_type == ModuleBlock))
typedef _PyCompilerSrcLocation location;
@@ -418,7 +418,7 @@ struct compiler {
PyObject *c_filename;
struct symtable *c_st;
PyFutureFeatures c_future; /* module's __future__ */
- PyCompilerFlags *c_flags;
+ PyCompilerFlags c_flags;
int c_optimize; /* optimization level */
int c_interactive; /* true if in interactive mode */
@@ -583,11 +583,11 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
return result;
}
+
static int
-compiler_init(struct compiler *c)
+compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
+ PyCompilerFlags flags, int optimize, PyArena *arena)
{
- memset(c, 0, sizeof(struct compiler));
-
c->c_const_cache = PyDict_New();
if (!c->c_const_cache) {
return 0;
@@ -595,57 +595,65 @@ compiler_init(struct compiler *c)
c->c_stack = PyList_New(0);
if (!c->c_stack) {
- Py_CLEAR(c->c_const_cache);
return 0;
}
- return 1;
-}
-
-PyCodeObject *
-_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
- int optimize, PyArena *arena)
-{
- struct compiler c;
- PyCodeObject *co = NULL;
- PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
- int merged;
- if (!compiler_init(&c))
- return NULL;
- c.c_filename = Py_NewRef(filename);
- c.c_arena = arena;
- if (!_PyFuture_FromAST(mod, filename, &c.c_future)) {
- goto finally;
- }
- if (!flags) {
- flags = &local_flags;
+ c->c_filename = Py_NewRef(filename);
+ c->c_arena = arena;
+ if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
+ return 0;
}
- merged = c.c_future.ff_features | flags->cf_flags;
- c.c_future.ff_features = merged;
- flags->cf_flags = merged;
- c.c_flags = flags;
- c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
- c.c_nestlevel = 0;
+ int merged = c->c_future.ff_features | flags.cf_flags;
+ c->c_future.ff_features = merged;
+ flags.cf_flags = merged;
+ c->c_flags = flags;
+ c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
+ c->c_nestlevel = 0;
_PyASTOptimizeState state;
- state.optimize = c.c_optimize;
+ state.optimize = c->c_optimize;
state.ff_features = merged;
if (!_PyAST_Optimize(mod, arena, &state)) {
- goto finally;
+ return 0;
}
-
- c.c_st = _PySymtable_Build(mod, filename, &c.c_future);
- if (c.c_st == NULL) {
- if (!PyErr_Occurred())
+ c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
+ if (c->c_st == NULL) {
+ if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_SystemError, "no symtable");
- goto finally;
+ }
+ return 0;
}
+ return 1;
+}
- co = compiler_mod(&c, mod);
+static struct compiler*
+new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
+ int optimize, PyArena *arena)
+{
+ PyCompilerFlags flags = pflags ? *pflags : _PyCompilerFlags_INIT;
+ struct compiler *c = PyMem_Calloc(1, sizeof(struct compiler));
+ if (c == NULL) {
+ return NULL;
+ }
+ if (!compiler_setup(c, mod, filename, flags, optimize, arena)) {
+ compiler_free(c);
+ return NULL;
+ }
+ return c;
+}
- finally:
- compiler_free(&c);
+PyCodeObject *
+_PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
+ int optimize, PyArena *arena)
+{
+ struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena);
+ if (c == NULL) {
+ return NULL;
+ }
+
+ PyCodeObject *co = compiler_mod(c, mod);
+ compiler_free(c);
assert(co || PyErr_Occurred());
return co;
}
@@ -656,8 +664,9 @@ compiler_free(struct compiler *c)
if (c->c_st)
_PySymtable_Free(c->c_st);
Py_XDECREF(c->c_filename);
- Py_DECREF(c->c_const_cache);
- Py_DECREF(c->c_stack);
+ Py_XDECREF(c->c_const_cache);
+ Py_XDECREF(c->c_stack);
+ PyMem_Free(c);
}
static PyObject *
@@ -2136,15 +2145,13 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
return 1;
}
-static PyCodeObject *
-compiler_mod(struct compiler *c, mod_ty mod)
+static int
+compiler_codegen(struct compiler *c, mod_ty mod)
{
- PyCodeObject *co;
- int addNone = 1;
_Py_DECLARE_STR(anon_module, "<module>");
if (!compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
mod, 1)) {
- return NULL;
+ return 0;
}
location loc = LOCATION(1, 1, 0, 0);
switch (mod->kind) {
@@ -2163,7 +2170,6 @@ compiler_mod(struct compiler *c, mod_ty mod)
break;
case Expression_kind:
VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
- addNone = 0;
break;
default:
PyErr_Format(PyExc_SystemError,
@@ -2171,7 +2177,17 @@ compiler_mod(struct compiler *c, mod_ty mod)
mod->kind);
return 0;
}
- co = assemble(c, addNone);
+ return 1;
+}
+
+static PyCodeObject *
+compiler_mod(struct compiler *c, mod_ty mod)
+{
+ int addNone = mod->kind != Expression_kind;
+ if (!compiler_codegen(c, mod)) {
+ return NULL;
+ }
+ PyCodeObject *co = assemble(c, addNone);
compiler_exit_scope(c);
return co;
}
@@ -8229,7 +8245,7 @@ compute_code_flags(struct compiler *c)
}
/* (Only) inherit compilerflags in PyCF_MASK */
- flags |= (c->c_flags->cf_flags & PyCF_MASK);
+ flags |= (c->c_flags.cf_flags & PyCF_MASK);
if ((IS_TOP_LEVEL_AWAIT(c)) &&
ste->ste_coroutine &&
@@ -9860,6 +9876,9 @@ duplicate_exits_without_lineno(cfg_builder *g)
/* Access to compiler optimizations for unit tests.
*
+ * _PyCompile_CodeGen takes and AST, applies code-gen and
+ * returns the unoptimized CFG as an instruction list.
+ *
* _PyCompile_OptimizeCfg takes an instruction list, constructs
* a CFG, optimizes it and converts back to an instruction list.
*
@@ -9954,7 +9973,9 @@ cfg_to_instructions(cfg_builder *g)
for (int i = 0; i < b->b_iused; i++) {
struct instr *instr = &b->b_instr[i];
location loc = instr->i_loc;
- int arg = HAS_TARGET(instr->i_opcode) ? instr->i_target->b_label : instr->i_oparg;
+ int arg = HAS_TARGET(instr->i_opcode) ?
+ instr->i_target->b_label : instr->i_oparg;
+
PyObject *inst_tuple = Py_BuildValue(
"(iiiiii)", instr->i_opcode, arg,
loc.lineno, loc.end_lineno,
@@ -9977,6 +9998,52 @@ error:
return NULL;
}
+PyObject *
+_PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
+ int optimize)
+{
+ PyObject *res = NULL;
+
+ if (!PyAST_Check(ast)) {
+ PyErr_SetString(PyExc_TypeError, "expected an AST");
+ return NULL;
+ }
+
+ PyArena *arena = _PyArena_New();
+ if (arena == NULL) {
+ return NULL;
+ }
+
+ mod_ty mod = PyAST_obj2mod(ast, arena, 0 /* exec */);
+ if (mod == NULL || !_PyAST_Validate(mod)) {
+ _PyArena_Free(arena);
+ return NULL;
+ }
+
+ struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena);
+ if (c == NULL) {
+ _PyArena_Free(arena);
+ return NULL;
+ }
+
+ if (!compiler_codegen(c, mod)) {
+ goto finally;
+ }
+
+ cfg_builder *g = CFG_BUILDER(c);
+
+ if (translate_jump_labels_to_targets(g->g_entryblock) < 0) {
+ goto finally;
+ }
+
+ res = cfg_to_instructions(g);
+
+finally:
+ compiler_exit_scope(c);
+ compiler_free(c);
+ _PyArena_Free(arena);
+ return res;
+}
PyObject *
_PyCompile_OptimizeCfg(PyObject *instructions, PyObject *consts)
diff --git a/Python/import.c b/Python/import.c
index daee16e..c12b3e0 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -94,9 +94,9 @@ _PyImportZip_Init(PyThreadState *tstate)
in different threads to return with a partially loaded module.
These calls are serialized by the global interpreter lock. */
-static PyThread_type_lock import_lock = NULL;
-static unsigned long import_lock_thread = PYTHREAD_INVALID_THREAD_ID;
-static int import_lock_level = 0;
+#define import_lock _PyRuntime.imports.lock.mutex
+#define import_lock_thread _PyRuntime.imports.lock.thread
+#define import_lock_level _PyRuntime.imports.lock.level
void
_PyImport_AcquireLock(void)
@@ -1759,8 +1759,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
PyObject *mod = NULL;
PyInterpreterState *interp = tstate->interp;
int import_time = _PyInterpreterState_GetConfig(interp)->import_time;
- static int import_level;
- static _PyTime_t accumulated;
+#define import_level _PyRuntime.imports.find_and_load.import_level
+#define accumulated _PyRuntime.imports.find_and_load.accumulated
_PyTime_t t1 = 0, accumulated_copy = accumulated;
@@ -1781,12 +1781,13 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
* _PyDict_GetItemIdWithError().
*/
if (import_time) {
- static int header = 1;
+#define header _PyRuntime.imports.find_and_load.header
if (header) {
fputs("import time: self [us] | cumulative | imported package\n",
stderr);
header = 0;
}
+#undef header
import_level++;
t1 = _PyTime_GetPerfCounter();
@@ -1816,6 +1817,8 @@ import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
}
return mod;
+#undef import_level
+#undef accumulated
}
PyObject *
diff --git a/Python/pystate.c b/Python/pystate.c
index 5d1814f..5c9e4ff 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -275,7 +275,9 @@ alloc_interpreter(void)
static void
free_interpreter(PyInterpreterState *interp)
{
- if (!interp->_static) {
+ // The main interpreter is statically allocated so
+ // should not be freed.
+ if (interp != &_PyRuntime._main_interpreter) {
PyMem_RawFree(interp);
}
}
@@ -359,7 +361,6 @@ PyInterpreterState_New(void)
interp = &runtime->_main_interpreter;
assert(interp->id == 0);
assert(interp->next == NULL);
- assert(interp->_static);
interpreters->main = interp;
}
@@ -374,9 +375,6 @@ PyInterpreterState_New(void)
// Set to _PyInterpreterState_INIT.
memcpy(interp, &initial._main_interpreter,
sizeof(*interp));
- // We need to adjust any fields that are different from the initial
- // interpreter (as defined in _PyInterpreterState_INIT):
- interp->_static = false;
if (id < 0) {
/* overflow or Py_Initialize() not called yet! */
@@ -762,7 +760,9 @@ alloc_threadstate(void)
static void
free_threadstate(PyThreadState *tstate)
{
- if (!tstate->_static) {
+ // The initial thread state of the interpreter is allocated
+ // as part of the interpreter state so should not be freed.
+ if (tstate != &tstate->interp->_initial_thread) {
PyMem_RawFree(tstate);
}
}
@@ -845,7 +845,6 @@ new_threadstate(PyInterpreterState *interp)
assert(id == 1);
used_newtstate = 0;
tstate = &interp->_initial_thread;
- assert(tstate->_static);
}
else {
// Every valid interpreter must have at least one thread.
@@ -857,9 +856,6 @@ new_threadstate(PyInterpreterState *interp)
memcpy(tstate,
&initial._main_interpreter._initial_thread,
sizeof(*tstate));
- // We need to adjust any fields that are different from the initial
- // thread (as defined in _PyThreadState_INIT):
- tstate->_static = false;
}
interp->threads.head = tstate;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 1090b12..6f0a126 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -431,6 +431,8 @@ sys_addaudithook_impl(PyObject *module, PyObject *hook)
if (interp->audit_hooks == NULL) {
return NULL;
}
+ /* Avoid having our list of hooks show up in the GC module */
+ PyObject_GC_UnTrack(interp->audit_hooks);
}
if (PyList_Append(interp->audit_hooks, hook) < 0) {