Skip to content

Commit 43c9731

Browse files
serhiy-storchakaYhg1s
authored andcommitted
bpo-38083: Minor improvements in asdl_c.py and Python-ast.c. (GH-15824)
* Use the const qualifier for constant C strings. * Intern field and attribute names. * Temporary incref a borrowed reference to a list item.
1 parent 359143c commit 43c9731

File tree

2 files changed

+322
-141
lines changed

2 files changed

+322
-141
lines changed

Parser/asdl_c.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,11 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
537537
self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
538538
self.emit("for (i = 0; i < len; i++) {", depth+1)
539539
self.emit("%s val;" % ctype, depth+2)
540-
self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
540+
self.emit("PyObject *tmp2 = PyList_GET_ITEM(tmp, i);", depth+2)
541+
self.emit("Py_INCREF(tmp2);", depth+2)
542+
self.emit("res = obj2ast_%s(tmp2, &val, arena);" %
541543
field.type, depth+2, reflow=False)
544+
self.emit("Py_DECREF(tmp2);", depth+2)
542545
self.emit("if (res != 0) goto failed;", depth+2)
543546
self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
544547
self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
@@ -576,14 +579,14 @@ def visitProduct(self, prod, name):
576579
if prod.attributes:
577580
for a in prod.attributes:
578581
self.emit_identifier(a.name)
579-
self.emit("static char *%s_attributes[] = {" % name, 0)
582+
self.emit("static const char * const %s_attributes[] = {" % name, 0)
580583
for a in prod.attributes:
581584
self.emit('"%s",' % a.name, 1)
582585
self.emit("};", 0)
583586
if prod.fields:
584587
for f in prod.fields:
585588
self.emit_identifier(f.name)
586-
self.emit("static char *%s_fields[]={" % name,0)
589+
self.emit("static const char * const %s_fields[]={" % name,0)
587590
for f in prod.fields:
588591
self.emit('"%s",' % f.name, 1)
589592
self.emit("};", 0)
@@ -593,7 +596,7 @@ def visitSum(self, sum, name):
593596
if sum.attributes:
594597
for a in sum.attributes:
595598
self.emit_identifier(a.name)
596-
self.emit("static char *%s_attributes[] = {" % name, 0)
599+
self.emit("static const char * const %s_attributes[] = {" % name, 0)
597600
for a in sum.attributes:
598601
self.emit('"%s",' % a.name, 1)
599602
self.emit("};", 0)
@@ -614,7 +617,7 @@ def visitConstructor(self, cons, name):
614617
if cons.fields:
615618
for t in cons.fields:
616619
self.emit_identifier(t.name)
617-
self.emit("static char *%s_fields[]={" % cons.name, 0)
620+
self.emit("static const char * const %s_fields[]={" % cons.name, 0)
618621
for t in cons.fields:
619622
self.emit('"%s",' % t.name, 1)
620623
self.emit("};",0)
@@ -771,7 +774,8 @@ def visitModule(self, mod):
771774
};
772775
773776
774-
static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
777+
static PyTypeObject *
778+
make_type(const char *type, PyTypeObject *base, const char * const *fields, int num_fields)
775779
{
776780
_Py_IDENTIFIER(__module__);
777781
_Py_IDENTIFIER(_ast);
@@ -780,7 +784,7 @@ def visitModule(self, mod):
780784
fnames = PyTuple_New(num_fields);
781785
if (!fnames) return NULL;
782786
for (i = 0; i < num_fields; i++) {
783-
PyObject *field = PyUnicode_FromString(fields[i]);
787+
PyObject *field = PyUnicode_InternFromString(fields[i]);
784788
if (!field) {
785789
Py_DECREF(fnames);
786790
return NULL;
@@ -796,14 +800,15 @@ def visitModule(self, mod):
796800
return (PyTypeObject*)result;
797801
}
798802
799-
static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
803+
static int
804+
add_attributes(PyTypeObject *type, const char * const *attrs, int num_fields)
800805
{
801806
int i, result;
802807
PyObject *s, *l = PyTuple_New(num_fields);
803808
if (!l)
804809
return 0;
805810
for (i = 0; i < num_fields; i++) {
806-
s = PyUnicode_FromString(attrs[i]);
811+
s = PyUnicode_InternFromString(attrs[i]);
807812
if (!s) {
808813
Py_DECREF(l);
809814
return 0;
@@ -1193,7 +1198,7 @@ class PartingShots(StaticVisitor):
11931198
{
11941199
mod_ty res;
11951200
PyObject *req_type[3];
1196-
char *req_name[] = {"Module", "Expression", "Interactive"};
1201+
const char * const req_name[] = {"Module", "Expression", "Interactive"};
11971202
int isinstance;
11981203
11991204
if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {

0 commit comments

Comments
 (0)