Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
String caches in ``compile.c`` are now subinterpreter compatible.
96 changes: 44 additions & 52 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
static void clean_basic_block(basicblock *bb);

static PyCodeObject *assemble(struct compiler *, int addNone);
static PyObject *__doc__, *__annotations__;

#define CAPSULE_NAME "compile.c compiler unit"

Expand Down Expand Up @@ -438,17 +437,6 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
PyCodeObject *co = NULL;
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
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))
return NULL;
Py_INCREF(filename);
Expand Down Expand Up @@ -1938,6 +1926,11 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
int i = 0;
stmt_ty st;
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.
This way line number for SETUP_ANNOTATIONS will always
Expand Down Expand Up @@ -1975,11 +1968,10 @@ compiler_mod(struct compiler *c, mod_ty mod)
{
PyCodeObject *co;
int addNone = 1;
static PyObject *module;
if (!module) {
module = PyUnicode_InternFromString("<module>");
if (!module)
return NULL;
_Py_static_string(PyId__module, "<module>");
PyObject *module = _PyUnicode_FromId(&PyId__module); /* borrowed ref */
if (module == NULL) {
return 0;
}
/* Use 0 for firstlineno initially, will fixup in assemble(). */
if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
Expand Down Expand Up @@ -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.
*/
static identifier return_str;
_Py_IDENTIFIER(return);
Py_ssize_t annotations_len = 0;

if (!compiler_visit_argannotations(c, args->args, &annotations_len))
Expand All @@ -2250,10 +2242,9 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
args->kwarg->annotation, &annotations_len))
return 0;

if (!return_str) {
return_str = PyUnicode_InternFromString("return");
if (!return_str)
return 0;
identifier return_str = _PyUnicode_FromId(&PyId_return); /* borrowed ref */
if (return_str == NULL) {
return 0;
}
Comment thread
Fidget-Spinner marked this conversation as resolved.
if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
return 0;
Expand Down Expand Up @@ -2799,18 +2790,18 @@ compiler_lambda(struct compiler *c, expr_ty e)
{
PyCodeObject *co;
PyObject *qualname;
static identifier name;
identifier name;
Py_ssize_t funcflags;
arguments_ty args = e->v.Lambda.args;
assert(e->kind == Lambda_kind);

if (!compiler_check_debug_args(c, args))
return 0;

if (!name) {
name = PyUnicode_InternFromString("<lambda>");
if (!name)
return 0;
_Py_static_string(PyId_lambda, "<lambda>");
name = _PyUnicode_FromId(&PyId_lambda); /* borrowed ref */
if (name == NULL) {
return 0;
}

funcflags = compiler_default_arguments(c, args);
Expand Down Expand Up @@ -3421,12 +3412,11 @@ compiler_from_import(struct compiler *c, stmt_ty s)
{
Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.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) {
empty_string = PyUnicode_FromString("");
if (!empty_string)
return 0;
if (empty_string == NULL) {
return 0;
}

ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Expand Down Expand Up @@ -4972,11 +4962,10 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
static int
compiler_genexp(struct compiler *c, expr_ty e)
{
static identifier name;
if (!name) {
name = PyUnicode_InternFromString("<genexpr>");
if (!name)
return 0;
_Py_static_string(PyId_genexpr, "<genexpr>");
identifier name = _PyUnicode_FromId(&PyId_genexpr); /* borrowed ref */
if (name == NULL) {
return 0;
}
assert(e->kind == GeneratorExp_kind);
return compiler_comprehension(c, e, COMP_GENEXP, name,
Expand All @@ -4987,11 +4976,10 @@ compiler_genexp(struct compiler *c, expr_ty e)
static int
compiler_listcomp(struct compiler *c, expr_ty e)
{
static identifier name;
if (!name) {
name = PyUnicode_InternFromString("<listcomp>");
if (!name)
return 0;
_Py_static_string(PyId_listcomp, "<listcomp>");
identifier name = _PyUnicode_FromId(&PyId_listcomp); /* borrowed ref */
if (name == NULL) {
return 0;
}
assert(e->kind == ListComp_kind);
return compiler_comprehension(c, e, COMP_LISTCOMP, name,
Expand All @@ -5002,11 +4990,10 @@ compiler_listcomp(struct compiler *c, expr_ty e)
static int
compiler_setcomp(struct compiler *c, expr_ty e)
{
static identifier name;
if (!name) {
name = PyUnicode_InternFromString("<setcomp>");
if (!name)
return 0;
_Py_static_string(PyId_setcomp, "<setcomp>");
identifier name = _PyUnicode_FromId(&PyId_setcomp); /* borrowed ref */
if (name == NULL) {
return 0;
}
assert(e->kind == SetComp_kind);
return compiler_comprehension(c, e, COMP_SETCOMP, name,
Expand All @@ -5018,11 +5005,10 @@ compiler_setcomp(struct compiler *c, expr_ty e)
static int
compiler_dictcomp(struct compiler *c, expr_ty e)
{
static identifier name;
if (!name) {
name = PyUnicode_InternFromString("<dictcomp>");
if (!name)
return 0;
_Py_static_string(PyId_dictcomp, "<dictcomp>");
identifier name = _PyUnicode_FromId(&PyId_dictcomp); /* borrowed ref */
if (name == NULL) {
return 0;
}
assert(e->kind == DictComp_kind);
return compiler_comprehension(c, e, COMP_DICTCOMP, name,
Expand Down Expand Up @@ -5553,6 +5539,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
{
expr_ty targ = s->v.AnnAssign.target;
PyObject* mangled;
_Py_IDENTIFIER(__annotations__);
/* borrowed ref*/
PyObject *__annotations__ = _PyUnicode_FromId(&PyId___annotations__);
if (__annotations__ == NULL) {
return 0;
}

assert(s->kind == AnnAssign_kind);

Expand Down