bpo-44156: Make cached string constants in compile.c subinterpreter compatible (GH-26161)

This commit is contained in:
Ken Jin 2021-05-25 21:55:34 +08:00 committed by GitHub
parent d16856960e
commit 29669245d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 52 deletions

View file

@ -0,0 +1 @@
String caches in ``compile.c`` are now subinterpreter compatible.

View file

@ -339,7 +339,6 @@ static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
static void clean_basic_block(basicblock *bb); static void clean_basic_block(basicblock *bb);
static PyCodeObject *assemble(struct compiler *, int addNone); static PyCodeObject *assemble(struct compiler *, int addNone);
static PyObject *__doc__, *__annotations__;
#define CAPSULE_NAME "compile.c compiler unit" #define CAPSULE_NAME "compile.c compiler unit"
@ -438,17 +437,6 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
PyCodeObject *co = NULL; PyCodeObject *co = NULL;
PyCompilerFlags local_flags = _PyCompilerFlags_INIT; PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
int merged; int merged;
if (!__doc__) {
__doc__ = PyUnicode_InternFromString("__doc__");
if (!__doc__)
return NULL;
}
if (!__annotations__) {
__annotations__ = PyUnicode_InternFromString("__annotations__");
if (!__annotations__)
return NULL;
}
if (!compiler_init(&c)) if (!compiler_init(&c))
return NULL; return NULL;
Py_INCREF(filename); Py_INCREF(filename);
@ -1938,6 +1926,11 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
int i = 0; int i = 0;
stmt_ty st; stmt_ty st;
PyObject *docstring; PyObject *docstring;
_Py_IDENTIFIER(__doc__);
PyObject *__doc__ = _PyUnicode_FromId(&PyId___doc__); /* borrowed ref*/
if (__doc__ == NULL) {
return 0;
}
/* Set current line number to the line number of first statement. /* Set current line number to the line number of first statement.
This way line number for SETUP_ANNOTATIONS will always This way line number for SETUP_ANNOTATIONS will always
@ -1975,11 +1968,10 @@ compiler_mod(struct compiler *c, mod_ty mod)
{ {
PyCodeObject *co; PyCodeObject *co;
int addNone = 1; int addNone = 1;
static PyObject *module; _Py_static_string(PyId__module, "<module>");
if (!module) { PyObject *module = _PyUnicode_FromId(&PyId__module); /* borrowed ref */
module = PyUnicode_InternFromString("<module>"); if (module == NULL) {
if (!module) return 0;
return NULL;
} }
/* Use 0 for firstlineno initially, will fixup in assemble(). */ /* Use 0 for firstlineno initially, will fixup in assemble(). */
if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
@ -2232,7 +2224,7 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed. Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
*/ */
static identifier return_str; _Py_IDENTIFIER(return);
Py_ssize_t annotations_len = 0; Py_ssize_t annotations_len = 0;
if (!compiler_visit_argannotations(c, args->args, &annotations_len)) if (!compiler_visit_argannotations(c, args->args, &annotations_len))
@ -2250,9 +2242,8 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
args->kwarg->annotation, &annotations_len)) args->kwarg->annotation, &annotations_len))
return 0; return 0;
if (!return_str) { identifier return_str = _PyUnicode_FromId(&PyId_return); /* borrowed ref */
return_str = PyUnicode_InternFromString("return"); if (return_str == NULL) {
if (!return_str)
return 0; return 0;
} }
if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
@ -2799,7 +2790,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
{ {
PyCodeObject *co; PyCodeObject *co;
PyObject *qualname; PyObject *qualname;
static identifier name; identifier name;
Py_ssize_t funcflags; Py_ssize_t funcflags;
arguments_ty args = e->v.Lambda.args; arguments_ty args = e->v.Lambda.args;
assert(e->kind == Lambda_kind); assert(e->kind == Lambda_kind);
@ -2807,9 +2798,9 @@ compiler_lambda(struct compiler *c, expr_ty e)
if (!compiler_check_debug_args(c, args)) if (!compiler_check_debug_args(c, args))
return 0; return 0;
if (!name) { _Py_static_string(PyId_lambda, "<lambda>");
name = PyUnicode_InternFromString("<lambda>"); name = _PyUnicode_FromId(&PyId_lambda); /* borrowed ref */
if (!name) if (name == NULL) {
return 0; return 0;
} }
@ -3421,11 +3412,10 @@ compiler_from_import(struct compiler *c, stmt_ty s)
{ {
Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
PyObject *names; PyObject *names;
static PyObject *empty_string; _Py_static_string(PyId_empty_string, "");
PyObject *empty_string = _PyUnicode_FromId(&PyId_empty_string); /* borrowed ref */
if (!empty_string) { if (empty_string == NULL) {
empty_string = PyUnicode_FromString("");
if (!empty_string)
return 0; return 0;
} }
@ -4972,10 +4962,9 @@ error:
static int static int
compiler_genexp(struct compiler *c, expr_ty e) compiler_genexp(struct compiler *c, expr_ty e)
{ {
static identifier name; _Py_static_string(PyId_genexpr, "<genexpr>");
if (!name) { identifier name = _PyUnicode_FromId(&PyId_genexpr); /* borrowed ref */
name = PyUnicode_InternFromString("<genexpr>"); if (name == NULL) {
if (!name)
return 0; return 0;
} }
assert(e->kind == GeneratorExp_kind); assert(e->kind == GeneratorExp_kind);
@ -4987,10 +4976,9 @@ compiler_genexp(struct compiler *c, expr_ty e)
static int static int
compiler_listcomp(struct compiler *c, expr_ty e) compiler_listcomp(struct compiler *c, expr_ty e)
{ {
static identifier name; _Py_static_string(PyId_listcomp, "<listcomp>");
if (!name) { identifier name = _PyUnicode_FromId(&PyId_listcomp); /* borrowed ref */
name = PyUnicode_InternFromString("<listcomp>"); if (name == NULL) {
if (!name)
return 0; return 0;
} }
assert(e->kind == ListComp_kind); assert(e->kind == ListComp_kind);
@ -5002,10 +4990,9 @@ compiler_listcomp(struct compiler *c, expr_ty e)
static int static int
compiler_setcomp(struct compiler *c, expr_ty e) compiler_setcomp(struct compiler *c, expr_ty e)
{ {
static identifier name; _Py_static_string(PyId_setcomp, "<setcomp>");
if (!name) { identifier name = _PyUnicode_FromId(&PyId_setcomp); /* borrowed ref */
name = PyUnicode_InternFromString("<setcomp>"); if (name == NULL) {
if (!name)
return 0; return 0;
} }
assert(e->kind == SetComp_kind); assert(e->kind == SetComp_kind);
@ -5018,10 +5005,9 @@ compiler_setcomp(struct compiler *c, expr_ty e)
static int static int
compiler_dictcomp(struct compiler *c, expr_ty e) compiler_dictcomp(struct compiler *c, expr_ty e)
{ {
static identifier name; _Py_static_string(PyId_dictcomp, "<dictcomp>");
if (!name) { identifier name = _PyUnicode_FromId(&PyId_dictcomp); /* borrowed ref */
name = PyUnicode_InternFromString("<dictcomp>"); if (name == NULL) {
if (!name)
return 0; return 0;
} }
assert(e->kind == DictComp_kind); assert(e->kind == DictComp_kind);
@ -5553,6 +5539,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
{ {
expr_ty targ = s->v.AnnAssign.target; expr_ty targ = s->v.AnnAssign.target;
PyObject* mangled; PyObject* mangled;
_Py_IDENTIFIER(__annotations__);
/* borrowed ref*/
PyObject *__annotations__ = _PyUnicode_FromId(&PyId___annotations__);
if (__annotations__ == NULL) {
return 0;
}
assert(s->kind == AnnAssign_kind); assert(s->kind == AnnAssign_kind);