summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2022-02-23 00:23:51 (GMT)
committerGitHub <noreply@github.com>2022-02-23 00:23:51 (GMT)
commit1f455361ecfb1892e375bdbee813cdf095b6cfb8 (patch)
treed7def4d5d167965a45c4b0e30bb5a1a0bb5c3b4a /Python/compile.c
parentcff4d5c5d29528299ec1ac5b3b3a6f7735577c01 (diff)
downloadcpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.zip
cpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.tar.gz
cpython-1f455361ecfb1892e375bdbee813cdf095b6cfb8.tar.bz2
bpo-46765: Replace Locally Cached Strings with Statically Initialized Objects (gh-31366)
https://bugs.python.org/issue46765
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c38
1 files changed, 7 insertions, 31 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 7f0a6f0..cb6e5e3 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -2522,7 +2522,6 @@ static int
compiler_class(struct compiler *c, stmt_ty s)
{
PyCodeObject *co;
- PyObject *str;
int i, firstlineno;
asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
@@ -2557,30 +2556,21 @@ compiler_class(struct compiler *c, stmt_ty s)
Py_INCREF(s->v.ClassDef.name);
Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
/* load (global) __name__ ... */
- str = PyUnicode_InternFromString("__name__");
- if (!str || !compiler_nameop(c, str, Load)) {
- Py_XDECREF(str);
+ if (!compiler_nameop(c, &_Py_ID(__name__), Load)) {
compiler_exit_scope(c);
return 0;
}
- Py_DECREF(str);
/* ... and store it as __module__ */
- str = PyUnicode_InternFromString("__module__");
- if (!str || !compiler_nameop(c, str, Store)) {
- Py_XDECREF(str);
+ if (!compiler_nameop(c, &_Py_ID(__module__), Store)) {
compiler_exit_scope(c);
return 0;
}
- Py_DECREF(str);
assert(c->u->u_qualname);
ADDOP_LOAD_CONST(c, c->u->u_qualname);
- str = PyUnicode_InternFromString("__qualname__");
- if (!str || !compiler_nameop(c, str, Store)) {
- Py_XDECREF(str);
+ if (!compiler_nameop(c, &_Py_ID(__qualname__), Store)) {
compiler_exit_scope(c);
return 0;
}
- Py_DECREF(str);
/* compile the body proper */
if (!compiler_body(c, s->v.ClassDef.body)) {
compiler_exit_scope(c);
@@ -2591,13 +2581,7 @@ compiler_class(struct compiler *c, stmt_ty s)
/* Return __classcell__ if it is referenced, otherwise return None */
if (c->u->u_ste->ste_needs_class_closure) {
/* Store __classcell__ into class namespace & return it */
- str = PyUnicode_InternFromString("__class__");
- if (str == NULL) {
- compiler_exit_scope(c);
- return 0;
- }
- i = compiler_lookup_arg(c->u->u_cellvars, str);
- Py_DECREF(str);
+ i = compiler_lookup_arg(c->u->u_cellvars, &_Py_ID(__class__));
if (i < 0) {
compiler_exit_scope(c);
return 0;
@@ -2606,13 +2590,10 @@ compiler_class(struct compiler *c, stmt_ty s)
ADDOP_I(c, LOAD_CLOSURE, i);
ADDOP_I(c, COPY, 1);
- str = PyUnicode_InternFromString("__classcell__");
- if (!str || !compiler_nameop(c, str, Store)) {
- Py_XDECREF(str);
+ if (!compiler_nameop(c, &_Py_ID(__classcell__), Store)) {
compiler_exit_scope(c);
return 0;
}
- Py_DECREF(str);
}
else {
/* No methods referenced __class__, so just return None */
@@ -4741,13 +4722,8 @@ compiler_joined_str(struct compiler *c, expr_ty e)
Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
if (value_count > STACK_USE_GUIDELINE) {
- ADDOP_LOAD_CONST_NEW(c, _PyUnicode_FromASCII("", 0));
- PyObject *join = _PyUnicode_FromASCII("join", 4);
- if (join == NULL) {
- return 0;
- }
- ADDOP_NAME(c, LOAD_METHOD, join, names);
- Py_DECREF(join);
+ ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty));
+ ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names);
ADDOP_I(c, BUILD_LIST, 0);
for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));