[Python-checkins] r42575 - in python/branches/ast-objects: Include/ast.h Python/ast.c Python/compile.c Python/pythonrun.c
martin.v.loewis
python-checkins at python.org
Sun Feb 26 01:49:42 CET 2006
Author: martin.v.loewis
Date: Sun Feb 26 01:49:41 2006
New Revision: 42575
Modified:
python/branches/ast-objects/Include/ast.h
python/branches/ast-objects/Python/ast.c
python/branches/ast-objects/Python/compile.c
python/branches/ast-objects/Python/pythonrun.c
Log:
Fix memory leaks. Make contexts singletons.
Modified: python/branches/ast-objects/Include/ast.h
==============================================================================
--- python/branches/ast-objects/Include/ast.h (original)
+++ python/branches/ast-objects/Include/ast.h Sun Feb 26 01:49:41 2006
@@ -7,6 +7,11 @@
PyAPI_FUNC(PyObject*) PyAST_FromNode(const node *, PyCompilerFlags *flags,
const char *);
+/* Singletons for load contexts */
+extern PyObject *_PyAST_Load, *_PyAST_Store, *_PyAST_Del,
+ *_PyAST_AugLoad, *_PyAST_AugStore, *_PyAST_Param;
+int _PyAST_Init(void);
+
#ifdef __cplusplus
}
#endif
Modified: python/branches/ast-objects/Python/ast.c
==============================================================================
--- python/branches/ast-objects/Python/ast.c (original)
+++ python/branches/ast-objects/Python/ast.c Sun Feb 26 01:49:41 2006
@@ -44,6 +44,9 @@
char *c_encoding; /* source encoding */
};
+PyObject *_PyAST_Load, *_PyAST_Store, *_PyAST_Del,
+ *_PyAST_AugLoad, *_PyAST_AugStore, *_PyAST_Param;
+
static PyObject *seq_for_testlist(struct compiling *, const node *);
static PyObject *ast_for_expr(struct compiling *, const node *);
static PyObject *ast_for_stmt(struct compiling *, const node *);
@@ -558,13 +561,9 @@
int i, len = (NCH(n) + 1) / 2;
PyObject *result = NULL;
PyObject *args = PyList_New(len);
- PyObject *store = NULL;
PyObject *arg = NULL;
if (!args)
goto error;
- store = Store();
- if (!store)
- goto error;
REQ(n, fplist);
@@ -575,21 +574,20 @@
ast_error(child, "assignment to None");
goto error;
}
- arg = Name(NEW_IDENTIFIER(child), store, LINENO(child));
+ arg = Name(NEW_IDENTIFIER(child), _PyAST_Store, LINENO(child));
}
else
arg = compiler_complex_args(CHILD(CHILD(n, 2*i), 1));
- if (!set_context(arg, store, n))
+ if (!set_context(arg, _PyAST_Store, n))
goto error;
STEAL_ITEM(args, i, arg);
}
- result = Tuple(args, store, LINENO(n));
- set_context(result, store, n);
+ result = Tuple(args, _PyAST_Store, LINENO(n));
+ set_context(result, _PyAST_Store, n);
error:
Py_XDECREF(args);
Py_XDECREF(arg);
- Py_XDECREF(store);
if (result && PyAST_Validate(result) == -1) return NULL;
return result;
}
@@ -680,9 +678,7 @@
}
id = NEW_IDENTIFIER(CHILD(ch, 0));
if (!id) goto error;
- if (!param) param = Param();
- if (!param) goto error;
- e = Name(id, param, LINENO(ch));
+ e = Name(id, _PyAST_Param, LINENO(ch));
if (!e)
goto error;
STEAL_ITEM(args, argno++, e);
@@ -726,7 +722,6 @@
Py_XDECREF(defaults);
Py_XDECREF(e);
Py_XDECREF(id);
- Py_XDECREF(param);
if (result && PyAST_Validate(result) == -1) return NULL;
return result;
}
@@ -738,7 +733,6 @@
PyObject *e = NULL;
PyObject *attrib = NULL;
PyObject *id = NULL;
- PyObject *load = NULL;
int i;
REQ(n, dotted_name);
@@ -746,10 +740,7 @@
id = NEW_IDENTIFIER(CHILD(n, 0));
if (!id)
goto error;
- load = Load();
- if (!load)
- goto error;
- e = Name(id, load, LINENO(n));
+ e = Name(id, _PyAST_Load, LINENO(n));
if (!e)
goto error;
id = NULL;
@@ -758,7 +749,7 @@
id = NEW_IDENTIFIER(CHILD(n, i));
if (!id)
goto error;
- attrib = Attribute(e, id, load, LINENO(CHILD(n, i)));
+ attrib = Attribute(e, id, _PyAST_Load, LINENO(CHILD(n, i)));
if (!attrib)
goto error;
e = attrib;
@@ -771,7 +762,6 @@
Py_XDECREF(id);
Py_XDECREF(e);
Py_XDECREF(attrib);
- Py_XDECREF(load);
if (result && PyAST_Validate(result) == -1) return NULL;
return result;
}
@@ -1006,7 +996,6 @@
PyObject *t = NULL;
PyObject *expression = NULL;
PyObject *lc = NULL;
- PyObject *store = NULL;
PyObject *ifs = NULL;
PyObject *tmp = NULL;
int i, n_fors;
@@ -1037,9 +1026,7 @@
REQ(ch, list_for);
- if (!store) store = Store();
- if (!store) goto error;
- t = ast_for_exprlist(c, CHILD(ch, 1), store);
+ t = ast_for_exprlist(c, CHILD(ch, 1), _PyAST_Store);
if (!t)
goto error;
expression = ast_for_testlist(c, CHILD(ch, 3));
@@ -1052,7 +1039,7 @@
goto error;
}
else {
- tmp = Tuple(t, store, LINENO(ch));
+ tmp = Tuple(t, _PyAST_Store, LINENO(ch));
if (!t)
goto error;
lc = comprehension(tmp, expression, NULL);
@@ -1105,7 +1092,6 @@
Py_XDECREF(t);
Py_XDECREF(expression);
Py_XDECREF(lc);
- Py_XDECREF(store);
Py_XDECREF(ifs);
Py_XDECREF(tmp);
if (result && PyAST_Validate(result) == -1) return NULL;
@@ -1186,7 +1172,6 @@
PyObject *ge = NULL;
PyObject *t = NULL;
PyObject *expression = NULL;
- PyObject *store = NULL;
PyObject *tmp = NULL;
int i, n_fors;
node *ch;
@@ -1206,10 +1191,6 @@
if (!genexps)
goto error;
- store = Store();
- if (!store)
- goto error;
-
ch = CHILD(n, 1);
for (i = 0; i < n_fors; i++) {
assert(ge == NULL);
@@ -1218,7 +1199,7 @@
REQ(ch, gen_for);
- t = ast_for_exprlist(c, CHILD(ch, 1), store);
+ t = ast_for_exprlist(c, CHILD(ch, 1), _PyAST_Store);
if (!t)
goto error;
expression = ast_for_expr(c, CHILD(ch, 3));
@@ -1230,7 +1211,7 @@
NULL);
}
else {
- tmp = Tuple(t, store, LINENO(ch));
+ tmp = Tuple(t, _PyAST_Store, LINENO(ch));
if (!tmp)
goto error;
ge = comprehension(tmp, expression, NULL);
@@ -1286,7 +1267,6 @@
Py_XDECREF(ge);
Py_XDECREF(t);
Py_XDECREF(expression);
- /* Py_XDECREF(store); */
/* Py_XDECREF(tmp); */
if (result && PyAST_Validate(result) == -1) return NULL;
return result;
@@ -2147,7 +2127,7 @@
if (!e)
goto error;
- if (!set_context(e, Store(), CHILD(n, i))) {
+ if (!set_context(e, _PyAST_Store, CHILD(n, i))) {
goto error;
}
@@ -2261,7 +2241,7 @@
/* del_stmt: 'del' exprlist */
REQ(n, del_stmt);
- expr_list = ast_for_exprlist(c, CHILD(n, 1), Del());
+ expr_list = ast_for_exprlist(c, CHILD(n, 1), _PyAST_Del);
if (!expr_list)
goto error;
result = Delete(expr_list, LINENO(n));
@@ -2920,7 +2900,7 @@
goto error;
}
- _target = ast_for_exprlist(c, CHILD(n, 1), Store());
+ _target = ast_for_exprlist(c, CHILD(n, 1), _PyAST_Store);
if (!_target) {
goto error;
}
@@ -2929,7 +2909,7 @@
Py_INCREF(target);
}
else
- target = Tuple(_target, Store(), LINENO(n));
+ target = Tuple(_target, _PyAST_Store, LINENO(n));
expression = ast_for_testlist(c, CHILD(n, 3));
if (!expression) {
@@ -2985,7 +2965,7 @@
e = ast_for_expr(c, CHILD(exc, 3));
if (!e)
goto error;
- if (!set_context(e, Store(), CHILD(exc, 3))) {
+ if (!set_context(e, _PyAST_Store, CHILD(exc, 3))) {
goto error;
}
expression = ast_for_expr(c, CHILD(exc, 1));
@@ -3490,3 +3470,15 @@
return NULL;
}
+int _PyAST_Init()
+{
+#define mk(context) _PyAST_##context = context(); if (!_PyAST_##context) return 0;
+ mk(Load);
+ mk(Store);
+ mk(Del);
+ mk(AugLoad);
+ mk(AugStore);
+ mk(Param);
+#undef mk
+ return 1;
+}
Modified: python/branches/ast-objects/Python/compile.c
==============================================================================
--- python/branches/ast-objects/Python/compile.c (original)
+++ python/branches/ast-objects/Python/compile.c Sun Feb 26 01:49:41 2006
@@ -296,6 +296,7 @@
if (!mod)
return NULL;
co = PyAST_Compile(mod, filename, NULL);
+ Py_DECREF(mod);
return co;
}
@@ -1712,7 +1713,7 @@
if (compiler_isdocstring(st)) {
i = 1;
VISIT(c, expr, Expr_value(st));
- if (!compiler_nameop(c, __doc__, Store()))
+ if (!compiler_nameop(c, __doc__, _PyAST_Store))
return 0;
}
for (; i < PyList_GET_SIZE(stmts); i++)
@@ -1877,7 +1878,7 @@
if (id == NULL) {
return 0;
}
- if (!compiler_nameop(c, id, Load())) {
+ if (!compiler_nameop(c, id, _PyAST_Load)) {
Py_DECREF(id);
return 0;
}
@@ -1943,7 +1944,7 @@
ADDOP_I(c, CALL_FUNCTION, 1);
}
- return compiler_nameop(c, FunctionDef_name(s), Store());
+ return compiler_nameop(c, FunctionDef_name(s), _PyAST_Store);
}
static int
@@ -1965,7 +1966,7 @@
c->u->u_private = ClassDef_name(s);
Py_INCREF(c->u->u_private);
str = PyString_InternFromString("__name__");
- if (!str || !compiler_nameop(c, str, Load())) {
+ if (!str || !compiler_nameop(c, str, _PyAST_Load)) {
Py_XDECREF(str);
compiler_exit_scope(c);
return 0;
@@ -1973,7 +1974,7 @@
Py_DECREF(str);
str = PyString_InternFromString("__module__");
- if (!str || !compiler_nameop(c, str, Store())) {
+ if (!str || !compiler_nameop(c, str, _PyAST_Store)) {
Py_XDECREF(str);
compiler_exit_scope(c);
return 0;
@@ -1997,7 +1998,7 @@
ADDOP_I(c, CALL_FUNCTION, 0);
ADDOP(c, BUILD_CLASS);
- if (!compiler_nameop(c, ClassDef_name(s), Store()))
+ if (!compiler_nameop(c, ClassDef_name(s), _PyAST_Store))
return 0;
return 1;
}
@@ -2386,7 +2387,7 @@
src = dot + 1;
}
}
- return compiler_nameop(c, asname, Store());
+ return compiler_nameop(c, asname, _PyAST_Store);
}
static int
@@ -2419,7 +2420,7 @@
if (dot)
tmp = PyString_FromStringAndSize(base,
dot - base);
- r = compiler_nameop(c, tmp, Store());
+ r = compiler_nameop(c, tmp, _PyAST_Store);
if (dot) {
Py_DECREF(tmp);
}
@@ -2475,7 +2476,7 @@
if (alias_asname(alias) != Py_None)
store_name = alias_asname(alias);
- if (!compiler_nameop(c, store_name, Store())) {
+ if (!compiler_nameop(c, store_name, _PyAST_Store)) {
Py_DECREF(names);
return 0;
}
@@ -3073,7 +3074,7 @@
/* only append after the last for generator */
if (gen_index >= PyList_GET_SIZE(generators)) {
- if (!compiler_nameop(c, tmpname, Load()))
+ if (!compiler_nameop(c, tmpname, _PyAST_Load))
return 0;
VISIT(c, expr, elt);
ADDOP_I(c, CALL_FUNCTION, 1);
@@ -3091,7 +3092,7 @@
compiler_use_next_block(c, anchor);
/* delete the append method added to locals */
if (gen_index == 1)
- if (!compiler_nameop(c, tmpname, Del()))
+ if (!compiler_nameop(c, tmpname, _PyAST_Del))
return 0;
return 1;
@@ -3119,7 +3120,7 @@
ADDOP_I(c, BUILD_LIST, 0);
ADDOP(c, DUP_TOP);
ADDOP_O(c, LOAD_ATTR, append, names);
- if (compiler_nameop(c, tmp, Store()))
+ if (compiler_nameop(c, tmp, _PyAST_Store))
rc = compiler_listcomp_generator(c, tmp, generators, 0,
ListComp_elt(e));
Py_DECREF(tmp);
@@ -3371,22 +3372,22 @@
switch (expr_context_kind(Subscript_ctx(e))) {
case AugLoad_kind:
VISIT(c, expr, Subscript_value(e));
- VISIT_SLICE(c, Subscript_slice(e), AugLoad()); /* make a PyObject ?? */
+ VISIT_SLICE(c, Subscript_slice(e), _PyAST_AugLoad); /* make a PyObject ?? */
break;
case Load_kind:
VISIT(c, expr, Subscript_value(e));
- VISIT_SLICE(c, Subscript_slice(e), Load());
+ VISIT_SLICE(c, Subscript_slice(e), _PyAST_Load);
break;
case AugStore_kind:
- VISIT_SLICE(c, Subscript_slice(e), AugStore());
+ VISIT_SLICE(c, Subscript_slice(e), _PyAST_AugLoad);
break;
case Store_kind:
VISIT(c, expr, Subscript_value(e));
- VISIT_SLICE(c, Subscript_slice(e), Store());
+ VISIT_SLICE(c, Subscript_slice(e), _PyAST_Store);
break;
case Del_kind:
VISIT(c, expr, Subscript_value(e));
- VISIT_SLICE(c, Subscript_slice(e), Del());
+ VISIT_SLICE(c, Subscript_slice(e), _PyAST_Del);
break;
case Param_kind:
PyErr_SetString(PyExc_SystemError,
@@ -3420,31 +3421,35 @@
switch (expr_kind(e)) {
case Attribute_kind:
auge = Attribute(Attribute_value(e), Attribute_attr(e),
- AugLoad(), ((struct _expr*)e)->lineno);
+ _PyAST_AugLoad, ((struct _expr*)e)->lineno);
if (auge == NULL)
return 0;
VISIT(c, expr, auge);
VISIT(c, expr, AugAssign_value(s));
ADDOP(c, inplace_binop(c, AugAssign_op(s)));
- Attribute_ctx (auge)= AugStore();
+ Py_DECREF(Attribute_ctx(auge));
+ Attribute_ctx(auge) = _PyAST_AugStore;
+ Py_INCREF(_PyAST_AugStore);
VISIT(c, expr, auge);
break;
case Subscript_kind:
auge = Subscript(Subscript_value(e), Subscript_slice(e),
- AugLoad(), ((struct _expr*)e)->lineno);
+ _PyAST_AugLoad, ((struct _expr*)e)->lineno);
if (auge == NULL)
return 0;
VISIT(c, expr, auge);
VISIT(c, expr, AugAssign_value(s));
ADDOP(c, inplace_binop(c, AugAssign_op(s)));
- Subscript_ctx (auge)= AugStore();
+ Py_DECREF(Attribute_ctx(auge));
+ Subscript_ctx(auge) = _PyAST_AugStore;
+ Py_INCREF(_PyAST_AugStore);
VISIT(c, expr, auge);
break;
case Name_kind:
VISIT(c, expr, AugAssign_target(s));
VISIT(c, expr, AugAssign_value(s));
ADDOP(c, inplace_binop(c, AugAssign_op(s)));
- return compiler_nameop(c, Name_id(e), Store());
+ return compiler_nameop(c, Name_id(e), _PyAST_Store);
default:
fprintf(stderr,
"invalid node type for augmented assignment\n");
Modified: python/branches/ast-objects/Python/pythonrun.c
==============================================================================
--- python/branches/ast-objects/Python/pythonrun.c (original)
+++ python/branches/ast-objects/Python/pythonrun.c Sun Feb 26 01:49:41 2006
@@ -175,6 +175,9 @@
if (!_PyInt_Init())
Py_FatalError("Py_Initialize: can't init ints");
+ if (!_PyAST_Init())
+ Py_FatalError("Py_Initialize: can't init AST");
+
_PyFloat_Init();
interp->modules = PyDict_New();
More information about the Python-checkins
mailing list