Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b89b6a

Browse files
committed
Intern all names and varnames in newcodeobject(), plus those string
literals that look like identifiers. Also intern all strings used as names during the compilation.
1 parent a412d24 commit 7b89b6a

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

Python/compile.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,35 @@ newcodeobject(argcount, nlocals, stacksize, flags,
205205
err_badcall();
206206
return NULL;
207207
}
208-
/* Make sure names and varnames are all strings */
208+
/* Make sure names and varnames are all strings, & intern them */
209209
for (i = gettuplesize(names); --i >= 0; ) {
210210
object *v = gettupleitem(names, i);
211211
if (v == NULL || !is_stringobject(v)) {
212212
err_badcall();
213213
return NULL;
214214
}
215+
PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
215216
}
216217
for (i = gettuplesize(varnames); --i >= 0; ) {
217218
object *v = gettupleitem(varnames, i);
218219
if (v == NULL || !is_stringobject(v)) {
219220
err_badcall();
220221
return NULL;
221222
}
223+
PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
224+
}
225+
/* Intern selected string constants */
226+
for (i = gettuplesize(consts); --i >= 0; ) {
227+
object *v = gettupleitem(consts, i);
228+
int n;
229+
char *p;
230+
if (!is_stringobject(v))
231+
continue;
232+
p = getstringvalue(v);
233+
if (strspn(p, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz")
234+
!= getstringsize(v))
235+
continue;
236+
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
222237
}
223238
co = NEWOBJ(codeobject, &Codetype);
224239
if (co != NULL) {
@@ -620,7 +635,7 @@ com_addopnamestr(c, op, name)
620635
com_mangle(c, name, buffer, (int)sizeof(buffer)))
621636
name = buffer;
622637
#endif
623-
if (name == NULL || (v = newstringobject(name)) == NULL) {
638+
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
624639
c->c_errors++;
625640
i = 255;
626641
}
@@ -986,7 +1001,7 @@ com_argument(c, n, pkeywords)
9861001
com_error(c, SyntaxError, "keyword can't be an expression");
9871002
}
9881003
else {
989-
object *v = newstringobject(STR(m));
1004+
object *v = PyString_InternFromString(STR(m));
9901005
if (v != NULL && *pkeywords == NULL)
9911006
*pkeywords = newdictobject();
9921007
if (v == NULL || *pkeywords == NULL)
@@ -1973,7 +1988,7 @@ com_newlocal(c, name)
19731988
struct compiling *c;
19741989
char *name;
19751990
{
1976-
object *nameval = newstringobject(name);
1991+
object *nameval = PyString_InternFromString(name);
19771992
int i;
19781993
if (nameval == NULL) {
19791994
c->c_errors++;
@@ -2563,7 +2578,7 @@ com_classdef(c, n)
25632578
object *v;
25642579
REQ(n, classdef);
25652580
/* classdef: class NAME ['(' testlist ')'] ':' suite */
2566-
if ((v = newstringobject(STR(CHILD(n, 1)))) == NULL) {
2581+
if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
25672582
c->c_errors++;
25682583
return;
25692584
}
@@ -3186,8 +3201,8 @@ jcompile(n, filename, base)
31863201
consts = listtuple(sc.c_consts);
31873202
names = listtuple(sc.c_names);
31883203
varnames = listtuple(sc.c_varnames);
3189-
filename = newstringobject(sc.c_filename);
3190-
name = newstringobject(sc.c_name);
3204+
filename = PyString_InternFromString(sc.c_filename);
3205+
name = PyString_InternFromString(sc.c_name);
31913206
if (!err_occurred())
31923207
co = newcodeobject(sc.c_argcount,
31933208
sc.c_nlocals,

0 commit comments

Comments
 (0)