Skip to content

Commit 2966924

Browse files
bpo-44156: Make cached string constants in compile.c subinterpreter compatible (GH-26161)
1 parent d168569 commit 2966924

File tree

2 files changed

+45
-52
lines changed

2 files changed

+45
-52
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
String caches in ``compile.c`` are now subinterpreter compatible.

Python/compile.c

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
339339
static void clean_basic_block(basicblock *bb);
340340

341341
static PyCodeObject *assemble(struct compiler *, int addNone);
342-
static PyObject *__doc__, *__annotations__;
343342

344343
#define CAPSULE_NAME "compile.c compiler unit"
345344

@@ -438,17 +437,6 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
438437
PyCodeObject *co = NULL;
439438
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
440439
int merged;
441-
442-
if (!__doc__) {
443-
__doc__ = PyUnicode_InternFromString("__doc__");
444-
if (!__doc__)
445-
return NULL;
446-
}
447-
if (!__annotations__) {
448-
__annotations__ = PyUnicode_InternFromString("__annotations__");
449-
if (!__annotations__)
450-
return NULL;
451-
}
452440
if (!compiler_init(&c))
453441
return NULL;
454442
Py_INCREF(filename);
@@ -1938,6 +1926,11 @@ compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
19381926
int i = 0;
19391927
stmt_ty st;
19401928
PyObject *docstring;
1929+
_Py_IDENTIFIER(__doc__);
1930+
PyObject *__doc__ = _PyUnicode_FromId(&PyId___doc__); /* borrowed ref*/
1931+
if (__doc__ == NULL) {
1932+
return 0;
1933+
}
19411934

19421935
/* Set current line number to the line number of first statement.
19431936
This way line number for SETUP_ANNOTATIONS will always
@@ -1975,11 +1968,10 @@ compiler_mod(struct compiler *c, mod_ty mod)
19751968
{
19761969
PyCodeObject *co;
19771970
int addNone = 1;
1978-
static PyObject *module;
1979-
if (!module) {
1980-
module = PyUnicode_InternFromString("<module>");
1981-
if (!module)
1982-
return NULL;
1971+
_Py_static_string(PyId__module, "<module>");
1972+
PyObject *module = _PyUnicode_FromId(&PyId__module); /* borrowed ref */
1973+
if (module == NULL) {
1974+
return 0;
19831975
}
19841976
/* Use 0 for firstlineno initially, will fixup in assemble(). */
19851977
if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1))
@@ -2232,7 +2224,7 @@ compiler_visit_annotations(struct compiler *c, arguments_ty args,
22322224
22332225
Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
22342226
*/
2235-
static identifier return_str;
2227+
_Py_IDENTIFIER(return);
22362228
Py_ssize_t annotations_len = 0;
22372229

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

2253-
if (!return_str) {
2254-
return_str = PyUnicode_InternFromString("return");
2255-
if (!return_str)
2256-
return 0;
2245+
identifier return_str = _PyUnicode_FromId(&PyId_return); /* borrowed ref */
2246+
if (return_str == NULL) {
2247+
return 0;
22572248
}
22582249
if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) {
22592250
return 0;
@@ -2799,18 +2790,18 @@ compiler_lambda(struct compiler *c, expr_ty e)
27992790
{
28002791
PyCodeObject *co;
28012792
PyObject *qualname;
2802-
static identifier name;
2793+
identifier name;
28032794
Py_ssize_t funcflags;
28042795
arguments_ty args = e->v.Lambda.args;
28052796
assert(e->kind == Lambda_kind);
28062797

28072798
if (!compiler_check_debug_args(c, args))
28082799
return 0;
28092800

2810-
if (!name) {
2811-
name = PyUnicode_InternFromString("<lambda>");
2812-
if (!name)
2813-
return 0;
2801+
_Py_static_string(PyId_lambda, "<lambda>");
2802+
name = _PyUnicode_FromId(&PyId_lambda); /* borrowed ref */
2803+
if (name == NULL) {
2804+
return 0;
28142805
}
28152806

28162807
funcflags = compiler_default_arguments(c, args);
@@ -3421,12 +3412,11 @@ compiler_from_import(struct compiler *c, stmt_ty s)
34213412
{
34223413
Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
34233414
PyObject *names;
3424-
static PyObject *empty_string;
3415+
_Py_static_string(PyId_empty_string, "");
3416+
PyObject *empty_string = _PyUnicode_FromId(&PyId_empty_string); /* borrowed ref */
34253417

3426-
if (!empty_string) {
3427-
empty_string = PyUnicode_FromString("");
3428-
if (!empty_string)
3429-
return 0;
3418+
if (empty_string == NULL) {
3419+
return 0;
34303420
}
34313421

34323422
ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
@@ -4972,11 +4962,10 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
49724962
static int
49734963
compiler_genexp(struct compiler *c, expr_ty e)
49744964
{
4975-
static identifier name;
4976-
if (!name) {
4977-
name = PyUnicode_InternFromString("<genexpr>");
4978-
if (!name)
4979-
return 0;
4965+
_Py_static_string(PyId_genexpr, "<genexpr>");
4966+
identifier name = _PyUnicode_FromId(&PyId_genexpr); /* borrowed ref */
4967+
if (name == NULL) {
4968+
return 0;
49804969
}
49814970
assert(e->kind == GeneratorExp_kind);
49824971
return compiler_comprehension(c, e, COMP_GENEXP, name,
@@ -4987,11 +4976,10 @@ compiler_genexp(struct compiler *c, expr_ty e)
49874976
static int
49884977
compiler_listcomp(struct compiler *c, expr_ty e)
49894978
{
4990-
static identifier name;
4991-
if (!name) {
4992-
name = PyUnicode_InternFromString("<listcomp>");
4993-
if (!name)
4994-
return 0;
4979+
_Py_static_string(PyId_listcomp, "<listcomp>");
4980+
identifier name = _PyUnicode_FromId(&PyId_listcomp); /* borrowed ref */
4981+
if (name == NULL) {
4982+
return 0;
49954983
}
49964984
assert(e->kind == ListComp_kind);
49974985
return compiler_comprehension(c, e, COMP_LISTCOMP, name,
@@ -5002,11 +4990,10 @@ compiler_listcomp(struct compiler *c, expr_ty e)
50024990
static int
50034991
compiler_setcomp(struct compiler *c, expr_ty e)
50044992
{
5005-
static identifier name;
5006-
if (!name) {
5007-
name = PyUnicode_InternFromString("<setcomp>");
5008-
if (!name)
5009-
return 0;
4993+
_Py_static_string(PyId_setcomp, "<setcomp>");
4994+
identifier name = _PyUnicode_FromId(&PyId_setcomp); /* borrowed ref */
4995+
if (name == NULL) {
4996+
return 0;
50104997
}
50114998
assert(e->kind == SetComp_kind);
50124999
return compiler_comprehension(c, e, COMP_SETCOMP, name,
@@ -5018,11 +5005,10 @@ compiler_setcomp(struct compiler *c, expr_ty e)
50185005
static int
50195006
compiler_dictcomp(struct compiler *c, expr_ty e)
50205007
{
5021-
static identifier name;
5022-
if (!name) {
5023-
name = PyUnicode_InternFromString("<dictcomp>");
5024-
if (!name)
5025-
return 0;
5008+
_Py_static_string(PyId_dictcomp, "<dictcomp>");
5009+
identifier name = _PyUnicode_FromId(&PyId_dictcomp); /* borrowed ref */
5010+
if (name == NULL) {
5011+
return 0;
50265012
}
50275013
assert(e->kind == DictComp_kind);
50285014
return compiler_comprehension(c, e, COMP_DICTCOMP, name,
@@ -5553,6 +5539,12 @@ compiler_annassign(struct compiler *c, stmt_ty s)
55535539
{
55545540
expr_ty targ = s->v.AnnAssign.target;
55555541
PyObject* mangled;
5542+
_Py_IDENTIFIER(__annotations__);
5543+
/* borrowed ref*/
5544+
PyObject *__annotations__ = _PyUnicode_FromId(&PyId___annotations__);
5545+
if (__annotations__ == NULL) {
5546+
return 0;
5547+
}
55565548

55575549
assert(s->kind == AnnAssign_kind);
55585550

0 commit comments

Comments
 (0)