Skip to content

Commit

Permalink
PEP 343 -- the with-statement.
Browse files Browse the repository at this point in the history
This was started by Mike Bland and completed by Guido
(with help from Neal).

This still needs a __future__ statement added;
Thomas is working on Michael's patch for that aspect.

There's a small amount of code cleanup and refactoring
in ast.c, compile.c and ceval.c (I fixed the lltrace
behavior when EXT_POP is used -- however I had to make
lltrace a static global).
  • Loading branch information
gvanrossum committed Feb 27, 2006
1 parent 5fec904 commit c2e2074
Show file tree
Hide file tree
Showing 23 changed files with 1,768 additions and 731 deletions.
5 changes: 5 additions & 0 deletions Doc/lib/asttable.tex
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@
\lineiii{}{\member{else_}}{}
\hline

\lineiii{With}{\member{expr}}{}
\lineiii{}{\member{vars&}}{}
\lineiii{}{\member{body}}{}
\hline

\lineiii{Yield}{\member{value}}{}
\hline

Expand Down
6 changes: 6 additions & 0 deletions Doc/ref/ref7.tex
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ \section{The \keyword{try} statement\label{try}}
statement to generate exceptions may be found in section~\ref{raise}.


\section{The \keyword{with} statement\label{with}}
\stindex{with}

The \keyword{with} statement specifies


\section{Function definitions\label{function}}
\indexii{function}{definition}
\stindex{def}
Expand Down
4 changes: 3 additions & 1 deletion Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ global_stmt: 'global' NAME (',' NAME)*
exec_stmt: 'exec' expr ['in' test [',' test]]
assert_stmt: 'assert' test [',' test]

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
while_stmt: 'while' test ':' suite ['else' ':' suite]
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
Expand All @@ -79,6 +79,8 @@ try_stmt: ('try' ':' suite
['else' ':' suite]
['finally' ':' suite] |
'finally' ':' suite))
with_stmt: 'with' test [ with_var ] ':' suite
with_var: NAME expr
# NB compile.c makes sure that the default except clause is last
except_clause: 'except' [test [',' test]]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
Expand Down
18 changes: 13 additions & 5 deletions Include/Python-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ struct _mod {
struct _stmt {
enum { FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
Delete_kind=4, Assign_kind=5, AugAssign_kind=6, Print_kind=7,
For_kind=8, While_kind=9, If_kind=10, Raise_kind=11,
TryExcept_kind=12, TryFinally_kind=13, Assert_kind=14,
Import_kind=15, ImportFrom_kind=16, Exec_kind=17,
Global_kind=18, Expr_kind=19, Pass_kind=20, Break_kind=21,
Continue_kind=22 } kind;
For_kind=8, While_kind=9, If_kind=10, With_kind=11,
Raise_kind=12, TryExcept_kind=13, TryFinally_kind=14,
Assert_kind=15, Import_kind=16, ImportFrom_kind=17,
Exec_kind=18, Global_kind=19, Expr_kind=20, Pass_kind=21,
Break_kind=22, Continue_kind=23 } kind;
union {
struct {
identifier name;
Expand Down Expand Up @@ -124,6 +124,12 @@ struct _stmt {
asdl_seq *orelse;
} If;

struct {
expr_ty context_expr;
expr_ty optional_vars;
asdl_seq *body;
} With;

struct {
expr_ty type;
expr_ty inst;
Expand Down Expand Up @@ -355,6 +361,8 @@ stmt_ty While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
PyArena *arena);
stmt_ty If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
PyArena *arena);
stmt_ty With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int
lineno, PyArena *arena);
stmt_ty Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, PyArena
*arena);
stmt_ty TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int
Expand Down
86 changes: 44 additions & 42 deletions Include/graminit.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,47 @@
#define while_stmt 293
#define for_stmt 294
#define try_stmt 295
#define except_clause 296
#define suite 297
#define testlist_safe 298
#define old_test 299
#define old_lambdef 300
#define test 301
#define or_test 302
#define and_test 303
#define not_test 304
#define comparison 305
#define comp_op 306
#define expr 307
#define xor_expr 308
#define and_expr 309
#define shift_expr 310
#define arith_expr 311
#define term 312
#define factor 313
#define power 314
#define atom 315
#define listmaker 316
#define testlist_gexp 317
#define lambdef 318
#define trailer 319
#define subscriptlist 320
#define subscript 321
#define sliceop 322
#define exprlist 323
#define testlist 324
#define dictmaker 325
#define classdef 326
#define arglist 327
#define argument 328
#define list_iter 329
#define list_for 330
#define list_if 331
#define gen_iter 332
#define gen_for 333
#define gen_if 334
#define testlist1 335
#define encoding_decl 336
#define yield_expr 337
#define with_stmt 296
#define with_var 297
#define except_clause 298
#define suite 299
#define testlist_safe 300
#define old_test 301
#define old_lambdef 302
#define test 303
#define or_test 304
#define and_test 305
#define not_test 306
#define comparison 307
#define comp_op 308
#define expr 309
#define xor_expr 310
#define and_expr 311
#define shift_expr 312
#define arith_expr 313
#define term 314
#define factor 315
#define power 316
#define atom 317
#define listmaker 318
#define testlist_gexp 319
#define lambdef 320
#define trailer 321
#define subscriptlist 322
#define subscript 323
#define sliceop 324
#define exprlist 325
#define testlist 326
#define dictmaker 327
#define classdef 328
#define arglist 329
#define argument 330
#define list_iter 331
#define list_for 332
#define list_if 333
#define gen_iter 334
#define gen_for 335
#define gen_if 336
#define testlist1 337
#define encoding_decl 338
#define yield_expr 339
3 changes: 1 addition & 2 deletions Include/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ extern "C" {
#define INPLACE_XOR 78
#define INPLACE_OR 79
#define BREAK_LOOP 80

#define WITH_CLEANUP 81
#define LOAD_LOCALS 82
#define RETURN_VALUE 83
#define IMPORT_STAR 84
#define EXEC_STMT 85
#define YIELD_VALUE 86

#define POP_BLOCK 87
#define END_FINALLY 88
#define BUILD_CLASS 89
Expand Down
31 changes: 28 additions & 3 deletions Lib/compiler/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def __init__(self, decorators, name, argnames, defaults, flags, doc, code, linen
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1



def getChildren(self):
Expand Down Expand Up @@ -584,7 +584,7 @@ def __init__(self, code, lineno=None):
self.lineno = lineno
self.argnames = ['[outmost-iterable]']
self.varargs = self.kwargs = None



def getChildren(self):
Expand Down Expand Up @@ -763,7 +763,7 @@ def __init__(self, argnames, defaults, flags, code, lineno=None):
self.varargs = 1
if flags & CO_VARKEYWORDS:
self.kwargs = 1



def getChildren(self):
Expand Down Expand Up @@ -1297,6 +1297,31 @@ def getChildNodes(self):
def __repr__(self):
return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))

class With(Node):
def __init__(self, expr, vars, body, lineno=None):
self.expr = expr
self.vars = vars
self.body = body
self.lineno = lineno

def getChildren(self):
children = []
children.append(self.expr)
children.append(self.vars)
children.append(self.body)
return tuple(children)

def getChildNodes(self):
nodelist = []
nodelist.append(self.expr)
if self.vars is not None:
nodelist.append(self.vars)
nodelist.append(self.body)
return tuple(nodelist)

def __repr__(self):
return "With(%s, %s, %s)" % (repr(self.expr), repr(self.vars), repr(self.body))

class Yield(Node):
def __init__(self, value, lineno=None):
self.value = value
Expand Down
9 changes: 2 additions & 7 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def jabs_op(name, op):
hasjabs.append(op)

# Instruction opcodes for compiled code
# Blank lines correspond to available opcodes

def_op('STOP_CODE', 0)
def_op('POP_TOP', 1)
Expand All @@ -59,7 +60,6 @@ def jabs_op(name, op):

def_op('LIST_APPEND', 18)
def_op('BINARY_POWER', 19)

def_op('BINARY_MULTIPLY', 20)
def_op('BINARY_DIVIDE', 21)
def_op('BINARY_MODULO', 22)
Expand All @@ -70,7 +70,6 @@ def jabs_op(name, op):
def_op('BINARY_TRUE_DIVIDE', 27)
def_op('INPLACE_FLOOR_DIVIDE', 28)
def_op('INPLACE_TRUE_DIVIDE', 29)

def_op('SLICE+0', 30)
def_op('SLICE+1', 31)
def_op('SLICE+2', 32)
Expand All @@ -93,7 +92,6 @@ def jabs_op(name, op):
def_op('INPLACE_MODULO', 59)
def_op('STORE_SUBSCR', 60)
def_op('DELETE_SUBSCR', 61)

def_op('BINARY_LSHIFT', 62)
def_op('BINARY_RSHIFT', 63)
def_op('BINARY_AND', 64)
Expand All @@ -113,13 +111,12 @@ def jabs_op(name, op):
def_op('INPLACE_XOR', 78)
def_op('INPLACE_OR', 79)
def_op('BREAK_LOOP', 80)

def_op('WITH_CLEANUP', 81)
def_op('LOAD_LOCALS', 82)
def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)
def_op('EXEC_STMT', 85)
def_op('YIELD_VALUE', 86)

def_op('POP_BLOCK', 87)
def_op('END_FINALLY', 88)
def_op('BUILD_CLASS', 89)
Expand Down Expand Up @@ -171,7 +168,6 @@ def jabs_op(name, op):
def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
def_op('MAKE_FUNCTION', 132) # Number of args with default values
def_op('BUILD_SLICE', 133) # Number of items

def_op('MAKE_CLOSURE', 134)
def_op('LOAD_CLOSURE', 135)
hasfree.append(135)
Expand All @@ -183,7 +179,6 @@ def jabs_op(name, op):
def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)

def_op('EXTENDED_ARG', 143)
EXTENDED_ARG = 143

Expand Down
Loading

0 comments on commit c2e2074

Please sign in to comment.