diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2007-02-26 18:10:47 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2007-02-26 18:10:47 (GMT) |
commit | 7b7d1c8282fa2599e2f132c401defde3c9b42e3f (patch) | |
tree | b3b1755eb9bc44d73eac17fd8b23451ba578d245 | |
parent | 1177bc4dfd74cc2671ccec26fd52d281c618e974 (diff) | |
download | cpython-7b7d1c8282fa2599e2f132c401defde3c9b42e3f.zip cpython-7b7d1c8282fa2599e2f132c401defde3c9b42e3f.tar.gz cpython-7b7d1c8282fa2599e2f132c401defde3c9b42e3f.tar.bz2 |
Fix a couple of problems in generating the AST code:
* use %r instead of backticks since backticks are going away in Py3k
* PyArena_Malloc() already sets PyErr_NoMemory so we don't need to do it again
* the signature for ast2obj_int incorrectly used a bool, rather than a long
-rwxr-xr-x | Parser/asdl_c.py | 8 | ||||
-rw-r--r-- | Python/Python-ast.c | 222 |
2 files changed, 59 insertions, 171 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 14859ee..4877853 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -47,7 +47,7 @@ def reflow_lines(s, depth): # XXX this should be fixed for real if i == -1 and 'GeneratorExp' in cur: i = size + 3 - assert i != -1, "Impossible line %d to reflow: %s" % (size, `s`) + assert i != -1, "Impossible line %d to reflow: %r" % (size, s) lines.append(padding + cur[:i]) if len(lines) == 1: # find new size based on brace @@ -299,10 +299,8 @@ class FunctionVisitor(PrototypeVisitor): emit('}', 1) emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1); - emit("if (!p) {", 1) - emit("PyErr_NoMemory();", 2) + emit("if (!p)", 1) emit("return NULL;", 2) - emit("}", 1) if union: self.emit_body_union(name, args, attrs) else: @@ -474,7 +472,7 @@ static PyObject* ast2obj_bool(bool b) return PyBool_FromLong(b); } -static PyObject* ast2obj_int(bool b) +static PyObject* ast2obj_int(long b) { return PyInt_FromLong(b); } diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 46a1817..421a2d1 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -440,7 +440,7 @@ static PyObject* ast2obj_bool(bool b) return PyBool_FromLong(b); } -static PyObject* ast2obj_int(bool b) +static PyObject* ast2obj_int(long b) { return PyInt_FromLong(b); } @@ -740,10 +740,8 @@ Module(asdl_seq * body, PyArena *arena) { mod_ty p; p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Module_kind; p->v.Module.body = body; return p; @@ -754,10 +752,8 @@ Interactive(asdl_seq * body, PyArena *arena) { mod_ty p; p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Interactive_kind; p->v.Interactive.body = body; return p; @@ -773,10 +769,8 @@ Expression(expr_ty body, PyArena *arena) return NULL; } p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Expression_kind; p->v.Expression.body = body; return p; @@ -787,10 +781,8 @@ Suite(asdl_seq * body, PyArena *arena) { mod_ty p; p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Suite_kind; p->v.Suite.body = body; return p; @@ -812,10 +804,8 @@ FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = FunctionDef_kind; p->v.FunctionDef.name = name; p->v.FunctionDef.args = args; @@ -837,10 +827,8 @@ ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, int lineno, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = ClassDef_kind; p->v.ClassDef.name = name; p->v.ClassDef.bases = bases; @@ -855,10 +843,8 @@ Return(expr_ty value, int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Return_kind; p->v.Return.value = value; p->lineno = lineno; @@ -871,10 +857,8 @@ Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Delete_kind; p->v.Delete.targets = targets; p->lineno = lineno; @@ -893,10 +877,8 @@ Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Assign_kind; p->v.Assign.targets = targets; p->v.Assign.value = value; @@ -926,10 +908,8 @@ AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = AugAssign_kind; p->v.AugAssign.target = target; p->v.AugAssign.op = op; @@ -945,10 +925,8 @@ Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset, { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Print_kind; p->v.Print.dest = dest; p->v.Print.values = values; @@ -974,10 +952,8 @@ For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = For_kind; p->v.For.target = target; p->v.For.iter = iter; @@ -999,10 +975,8 @@ While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = While_kind; p->v.While.test = test; p->v.While.body = body; @@ -1023,10 +997,8 @@ If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = If_kind; p->v.If.test = test; p->v.If.body = body; @@ -1047,10 +1019,8 @@ With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno, return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = With_kind; p->v.With.context_expr = context_expr; p->v.With.optional_vars = optional_vars; @@ -1066,10 +1036,8 @@ Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset, { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Raise_kind; p->v.Raise.type = type; p->v.Raise.inst = inst; @@ -1085,10 +1053,8 @@ TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno, { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = TryExcept_kind; p->v.TryExcept.body = body; p->v.TryExcept.handlers = handlers; @@ -1104,10 +1070,8 @@ TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset, { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = TryFinally_kind; p->v.TryFinally.body = body; p->v.TryFinally.finalbody = finalbody; @@ -1126,10 +1090,8 @@ Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena) return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Assert_kind; p->v.Assert.test = test; p->v.Assert.msg = msg; @@ -1143,10 +1105,8 @@ Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Import_kind; p->v.Import.names = names; p->lineno = lineno; @@ -1165,10 +1125,8 @@ ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = ImportFrom_kind; p->v.ImportFrom.module = module; p->v.ImportFrom.names = names; @@ -1189,10 +1147,8 @@ Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset, return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Exec_kind; p->v.Exec.body = body; p->v.Exec.globals = globals; @@ -1207,10 +1163,8 @@ Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Global_kind; p->v.Global.names = names; p->lineno = lineno; @@ -1228,10 +1182,8 @@ Expr(expr_ty value, int lineno, int col_offset, PyArena *arena) return NULL; } p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Expr_kind; p->v.Expr.value = value; p->lineno = lineno; @@ -1244,10 +1196,8 @@ Pass(int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Pass_kind; p->lineno = lineno; p->col_offset = col_offset; @@ -1259,10 +1209,8 @@ Break(int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Break_kind; p->lineno = lineno; p->col_offset = col_offset; @@ -1274,10 +1222,8 @@ Continue(int lineno, int col_offset, PyArena *arena) { stmt_ty p; p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Continue_kind; p->lineno = lineno; p->col_offset = col_offset; @@ -1295,10 +1241,8 @@ BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = BoolOp_kind; p->v.BoolOp.op = op; p->v.BoolOp.values = values; @@ -1328,10 +1272,8 @@ BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = BinOp_kind; p->v.BinOp.left = left; p->v.BinOp.op = op; @@ -1357,10 +1299,8 @@ UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = UnaryOp_kind; p->v.UnaryOp.op = op; p->v.UnaryOp.operand = operand; @@ -1385,10 +1325,8 @@ Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Lambda_kind; p->v.Lambda.args = args; p->v.Lambda.body = body; @@ -1418,10 +1356,8 @@ IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = IfExp_kind; p->v.IfExp.test = test; p->v.IfExp.body = body; @@ -1437,10 +1373,8 @@ Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena { expr_ty p; p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Dict_kind; p->v.Dict.keys = keys; p->v.Dict.values = values; @@ -1460,10 +1394,8 @@ ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = ListComp_kind; p->v.ListComp.elt = elt; p->v.ListComp.generators = generators; @@ -1483,10 +1415,8 @@ GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = GeneratorExp_kind; p->v.GeneratorExp.elt = elt; p->v.GeneratorExp.generators = generators; @@ -1500,10 +1430,8 @@ Yield(expr_ty value, int lineno, int col_offset, PyArena *arena) { expr_ty p; p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Yield_kind; p->v.Yield.value = value; p->lineno = lineno; @@ -1522,10 +1450,8 @@ Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Compare_kind; p->v.Compare.left = left; p->v.Compare.ops = ops; @@ -1546,10 +1472,8 @@ Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs, return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Call_kind; p->v.Call.func = func; p->v.Call.args = args; @@ -1571,10 +1495,8 @@ Repr(expr_ty value, int lineno, int col_offset, PyArena *arena) return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Repr_kind; p->v.Repr.value = value; p->lineno = lineno; @@ -1592,10 +1514,8 @@ Num(object n, int lineno, int col_offset, PyArena *arena) return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Num_kind; p->v.Num.n = n; p->lineno = lineno; @@ -1613,10 +1533,8 @@ Str(string s, int lineno, int col_offset, PyArena *arena) return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Str_kind; p->v.Str.s = s; p->lineno = lineno; @@ -1645,10 +1563,8 @@ Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Attribute_kind; p->v.Attribute.value = value; p->v.Attribute.attr = attr; @@ -1679,10 +1595,8 @@ Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Subscript_kind; p->v.Subscript.value = value; p->v.Subscript.slice = slice; @@ -1708,10 +1622,8 @@ Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Name_kind; p->v.Name.id = id; p->v.Name.ctx = ctx; @@ -1731,10 +1643,8 @@ List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = List_kind; p->v.List.elts = elts; p->v.List.ctx = ctx; @@ -1754,10 +1664,8 @@ Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Tuple_kind; p->v.Tuple.elts = elts; p->v.Tuple.ctx = ctx; @@ -1771,10 +1679,8 @@ Ellipsis(PyArena *arena) { slice_ty p; p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Ellipsis_kind; return p; } @@ -1784,10 +1690,8 @@ Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena) { slice_ty p; p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Slice_kind; p->v.Slice.lower = lower; p->v.Slice.upper = upper; @@ -1800,10 +1704,8 @@ ExtSlice(asdl_seq * dims, PyArena *arena) { slice_ty p; p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = ExtSlice_kind; p->v.ExtSlice.dims = dims; return p; @@ -1819,10 +1721,8 @@ Index(expr_ty value, PyArena *arena) return NULL; } p = (slice_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->kind = Index_kind; p->v.Index.value = value; return p; @@ -1843,10 +1743,8 @@ comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena) return NULL; } p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->target = target; p->iter = iter; p->ifs = ifs; @@ -1859,10 +1757,8 @@ excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int { excepthandler_ty p; p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->type = type; p->name = name; p->body = body; @@ -1877,10 +1773,8 @@ arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq * { arguments_ty p; p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->args = args; p->vararg = vararg; p->kwarg = kwarg; @@ -1903,10 +1797,8 @@ keyword(identifier arg, expr_ty value, PyArena *arena) return NULL; } p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->arg = arg; p->value = value; return p; @@ -1922,10 +1814,8 @@ alias(identifier name, identifier asname, PyArena *arena) return NULL; } p = (alias_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) { - PyErr_NoMemory(); + if (!p) return NULL; - } p->name = name; p->asname = asname; return p; |