Skip to content

Commit

Permalink
bpo-38083: Minor improvements in asdl_c.py and Python-ast.c. (pythonG…
Browse files Browse the repository at this point in the history
…H-15824)

* Use the const qualifier for constant C strings.
* Intern field and attribute names.
* Temporary incref a borrowed reference to a list item.
  • Loading branch information
serhiy-storchaka authored and Yhg1s committed Sep 10, 2019
1 parent 359143c commit 43c9731
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 141 deletions.
25 changes: 15 additions & 10 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,11 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
self.emit("if (%s == NULL) goto failed;" % field.name, depth+1)
self.emit("for (i = 0; i < len; i++) {", depth+1)
self.emit("%s val;" % ctype, depth+2)
self.emit("res = obj2ast_%s(PyList_GET_ITEM(tmp, i), &val, arena);" %
self.emit("PyObject *tmp2 = PyList_GET_ITEM(tmp, i);", depth+2)
self.emit("Py_INCREF(tmp2);", depth+2)
self.emit("res = obj2ast_%s(tmp2, &val, arena);" %
field.type, depth+2, reflow=False)
self.emit("Py_DECREF(tmp2);", depth+2)
self.emit("if (res != 0) goto failed;", depth+2)
self.emit("if (len != PyList_GET_SIZE(tmp)) {", depth+2)
self.emit("PyErr_SetString(PyExc_RuntimeError, \"%s field \\\"%s\\\" "
Expand Down Expand Up @@ -576,14 +579,14 @@ def visitProduct(self, prod, name):
if prod.attributes:
for a in prod.attributes:
self.emit_identifier(a.name)
self.emit("static char *%s_attributes[] = {" % name, 0)
self.emit("static const char * const %s_attributes[] = {" % name, 0)
for a in prod.attributes:
self.emit('"%s",' % a.name, 1)
self.emit("};", 0)
if prod.fields:
for f in prod.fields:
self.emit_identifier(f.name)
self.emit("static char *%s_fields[]={" % name,0)
self.emit("static const char * const %s_fields[]={" % name,0)
for f in prod.fields:
self.emit('"%s",' % f.name, 1)
self.emit("};", 0)
Expand All @@ -593,7 +596,7 @@ def visitSum(self, sum, name):
if sum.attributes:
for a in sum.attributes:
self.emit_identifier(a.name)
self.emit("static char *%s_attributes[] = {" % name, 0)
self.emit("static const char * const %s_attributes[] = {" % name, 0)
for a in sum.attributes:
self.emit('"%s",' % a.name, 1)
self.emit("};", 0)
Expand All @@ -614,7 +617,7 @@ def visitConstructor(self, cons, name):
if cons.fields:
for t in cons.fields:
self.emit_identifier(t.name)
self.emit("static char *%s_fields[]={" % cons.name, 0)
self.emit("static const char * const %s_fields[]={" % cons.name, 0)
for t in cons.fields:
self.emit('"%s",' % t.name, 1)
self.emit("};",0)
Expand Down Expand Up @@ -771,7 +774,8 @@ def visitModule(self, mod):
};
static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
static PyTypeObject *
make_type(const char *type, PyTypeObject *base, const char * const *fields, int num_fields)
{
_Py_IDENTIFIER(__module__);
_Py_IDENTIFIER(_ast);
Expand All @@ -780,7 +784,7 @@ def visitModule(self, mod):
fnames = PyTuple_New(num_fields);
if (!fnames) return NULL;
for (i = 0; i < num_fields; i++) {
PyObject *field = PyUnicode_FromString(fields[i]);
PyObject *field = PyUnicode_InternFromString(fields[i]);
if (!field) {
Py_DECREF(fnames);
return NULL;
Expand All @@ -796,14 +800,15 @@ def visitModule(self, mod):
return (PyTypeObject*)result;
}
static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
static int
add_attributes(PyTypeObject *type, const char * const *attrs, int num_fields)
{
int i, result;
PyObject *s, *l = PyTuple_New(num_fields);
if (!l)
return 0;
for (i = 0; i < num_fields; i++) {
s = PyUnicode_FromString(attrs[i]);
s = PyUnicode_InternFromString(attrs[i]);
if (!s) {
Py_DECREF(l);
return 0;
Expand Down Expand Up @@ -1193,7 +1198,7 @@ class PartingShots(StaticVisitor):
{
mod_ty res;
PyObject *req_type[3];
char *req_name[] = {"Module", "Expression", "Interactive"};
const char * const req_name[] = {"Module", "Expression", "Interactive"};
int isinstance;
if (PySys_Audit("compile", "OO", ast, Py_None) < 0) {
Expand Down
Loading

0 comments on commit 43c9731

Please sign in to comment.