Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2e2074

Browse files
committed
PEP 343 -- the with-statement.
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).
1 parent 5fec904 commit c2e2074

23 files changed

Lines changed: 1768 additions & 731 deletions

File tree

Doc/lib/asttable.tex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@
272272
\lineiii{}{\member{else_}}{}
273273
\hline
274274

275+
\lineiii{With}{\member{expr}}{}
276+
\lineiii{}{\member{vars&}}{}
277+
\lineiii{}{\member{body}}{}
278+
\hline
279+
275280
\lineiii{Yield}{\member{value}}{}
276281
\hline
277282

Doc/ref/ref7.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ \section{The \keyword{try} statement\label{try}}
308308
statement to generate exceptions may be found in section~\ref{raise}.
309309

310310

311+
\section{The \keyword{with} statement\label{with}}
312+
\stindex{with}
313+
314+
The \keyword{with} statement specifies
315+
316+
311317
\section{Function definitions\label{function}}
312318
\indexii{function}{definition}
313319
\stindex{def}

Grammar/Grammar

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ global_stmt: 'global' NAME (',' NAME)*
7070
exec_stmt: 'exec' expr ['in' test [',' test]]
7171
assert_stmt: 'assert' test [',' test]
7272

73-
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
73+
compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef
7474
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
7575
while_stmt: 'while' test ':' suite ['else' ':' suite]
7676
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
@@ -79,6 +79,8 @@ try_stmt: ('try' ':' suite
7979
['else' ':' suite]
8080
['finally' ':' suite] |
8181
'finally' ':' suite))
82+
with_stmt: 'with' test [ with_var ] ':' suite
83+
with_var: NAME expr
8284
# NB compile.c makes sure that the default except clause is last
8385
except_clause: 'except' [test [',' test]]
8486
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

Include/Python-ast.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ struct _mod {
6161
struct _stmt {
6262
enum { FunctionDef_kind=1, ClassDef_kind=2, Return_kind=3,
6363
Delete_kind=4, Assign_kind=5, AugAssign_kind=6, Print_kind=7,
64-
For_kind=8, While_kind=9, If_kind=10, Raise_kind=11,
65-
TryExcept_kind=12, TryFinally_kind=13, Assert_kind=14,
66-
Import_kind=15, ImportFrom_kind=16, Exec_kind=17,
67-
Global_kind=18, Expr_kind=19, Pass_kind=20, Break_kind=21,
68-
Continue_kind=22 } kind;
64+
For_kind=8, While_kind=9, If_kind=10, With_kind=11,
65+
Raise_kind=12, TryExcept_kind=13, TryFinally_kind=14,
66+
Assert_kind=15, Import_kind=16, ImportFrom_kind=17,
67+
Exec_kind=18, Global_kind=19, Expr_kind=20, Pass_kind=21,
68+
Break_kind=22, Continue_kind=23 } kind;
6969
union {
7070
struct {
7171
identifier name;
@@ -124,6 +124,12 @@ struct _stmt {
124124
asdl_seq *orelse;
125125
} If;
126126

127+
struct {
128+
expr_ty context_expr;
129+
expr_ty optional_vars;
130+
asdl_seq *body;
131+
} With;
132+
127133
struct {
128134
expr_ty type;
129135
expr_ty inst;
@@ -355,6 +361,8 @@ stmt_ty While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
355361
PyArena *arena);
356362
stmt_ty If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno,
357363
PyArena *arena);
364+
stmt_ty With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int
365+
lineno, PyArena *arena);
358366
stmt_ty Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, PyArena
359367
*arena);
360368
stmt_ty TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int

Include/graminit.h

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,47 @@
3838
#define while_stmt 293
3939
#define for_stmt 294
4040
#define try_stmt 295
41-
#define except_clause 296
42-
#define suite 297
43-
#define testlist_safe 298
44-
#define old_test 299
45-
#define old_lambdef 300
46-
#define test 301
47-
#define or_test 302
48-
#define and_test 303
49-
#define not_test 304
50-
#define comparison 305
51-
#define comp_op 306
52-
#define expr 307
53-
#define xor_expr 308
54-
#define and_expr 309
55-
#define shift_expr 310
56-
#define arith_expr 311
57-
#define term 312
58-
#define factor 313
59-
#define power 314
60-
#define atom 315
61-
#define listmaker 316
62-
#define testlist_gexp 317
63-
#define lambdef 318
64-
#define trailer 319
65-
#define subscriptlist 320
66-
#define subscript 321
67-
#define sliceop 322
68-
#define exprlist 323
69-
#define testlist 324
70-
#define dictmaker 325
71-
#define classdef 326
72-
#define arglist 327
73-
#define argument 328
74-
#define list_iter 329
75-
#define list_for 330
76-
#define list_if 331
77-
#define gen_iter 332
78-
#define gen_for 333
79-
#define gen_if 334
80-
#define testlist1 335
81-
#define encoding_decl 336
82-
#define yield_expr 337
41+
#define with_stmt 296
42+
#define with_var 297
43+
#define except_clause 298
44+
#define suite 299
45+
#define testlist_safe 300
46+
#define old_test 301
47+
#define old_lambdef 302
48+
#define test 303
49+
#define or_test 304
50+
#define and_test 305
51+
#define not_test 306
52+
#define comparison 307
53+
#define comp_op 308
54+
#define expr 309
55+
#define xor_expr 310
56+
#define and_expr 311
57+
#define shift_expr 312
58+
#define arith_expr 313
59+
#define term 314
60+
#define factor 315
61+
#define power 316
62+
#define atom 317
63+
#define listmaker 318
64+
#define testlist_gexp 319
65+
#define lambdef 320
66+
#define trailer 321
67+
#define subscriptlist 322
68+
#define subscript 323
69+
#define sliceop 324
70+
#define exprlist 325
71+
#define testlist 326
72+
#define dictmaker 327
73+
#define classdef 328
74+
#define arglist 329
75+
#define argument 330
76+
#define list_iter 331
77+
#define list_for 332
78+
#define list_if 333
79+
#define gen_iter 334
80+
#define gen_for 335
81+
#define gen_if 336
82+
#define testlist1 337
83+
#define encoding_decl 338
84+
#define yield_expr 339

Include/opcode.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,12 @@ extern "C" {
7272
#define INPLACE_XOR 78
7373
#define INPLACE_OR 79
7474
#define BREAK_LOOP 80
75-
75+
#define WITH_CLEANUP 81
7676
#define LOAD_LOCALS 82
7777
#define RETURN_VALUE 83
7878
#define IMPORT_STAR 84
7979
#define EXEC_STMT 85
8080
#define YIELD_VALUE 86
81-
8281
#define POP_BLOCK 87
8382
#define END_FINALLY 88
8483
#define BUILD_CLASS 89

Lib/compiler/ast.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def __init__(self, decorators, name, argnames, defaults, flags, doc, code, linen
553553
self.varargs = 1
554554
if flags & CO_VARKEYWORDS:
555555
self.kwargs = 1
556-
556+
557557

558558

559559
def getChildren(self):
@@ -584,7 +584,7 @@ def __init__(self, code, lineno=None):
584584
self.lineno = lineno
585585
self.argnames = ['[outmost-iterable]']
586586
self.varargs = self.kwargs = None
587-
587+
588588

589589

590590
def getChildren(self):
@@ -763,7 +763,7 @@ def __init__(self, argnames, defaults, flags, code, lineno=None):
763763
self.varargs = 1
764764
if flags & CO_VARKEYWORDS:
765765
self.kwargs = 1
766-
766+
767767

768768

769769
def getChildren(self):
@@ -1297,6 +1297,31 @@ def getChildNodes(self):
12971297
def __repr__(self):
12981298
return "While(%s, %s, %s)" % (repr(self.test), repr(self.body), repr(self.else_))
12991299

1300+
class With(Node):
1301+
def __init__(self, expr, vars, body, lineno=None):
1302+
self.expr = expr
1303+
self.vars = vars
1304+
self.body = body
1305+
self.lineno = lineno
1306+
1307+
def getChildren(self):
1308+
children = []
1309+
children.append(self.expr)
1310+
children.append(self.vars)
1311+
children.append(self.body)
1312+
return tuple(children)
1313+
1314+
def getChildNodes(self):
1315+
nodelist = []
1316+
nodelist.append(self.expr)
1317+
if self.vars is not None:
1318+
nodelist.append(self.vars)
1319+
nodelist.append(self.body)
1320+
return tuple(nodelist)
1321+
1322+
def __repr__(self):
1323+
return "With(%s, %s, %s)" % (repr(self.expr), repr(self.vars), repr(self.body))
1324+
13001325
class Yield(Node):
13011326
def __init__(self, value, lineno=None):
13021327
self.value = value

Lib/opcode.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def jabs_op(name, op):
4141
hasjabs.append(op)
4242

4343
# Instruction opcodes for compiled code
44+
# Blank lines correspond to available opcodes
4445

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

6061
def_op('LIST_APPEND', 18)
6162
def_op('BINARY_POWER', 19)
62-
6363
def_op('BINARY_MULTIPLY', 20)
6464
def_op('BINARY_DIVIDE', 21)
6565
def_op('BINARY_MODULO', 22)
@@ -70,7 +70,6 @@ def jabs_op(name, op):
7070
def_op('BINARY_TRUE_DIVIDE', 27)
7171
def_op('INPLACE_FLOOR_DIVIDE', 28)
7272
def_op('INPLACE_TRUE_DIVIDE', 29)
73-
7473
def_op('SLICE+0', 30)
7574
def_op('SLICE+1', 31)
7675
def_op('SLICE+2', 32)
@@ -93,7 +92,6 @@ def jabs_op(name, op):
9392
def_op('INPLACE_MODULO', 59)
9493
def_op('STORE_SUBSCR', 60)
9594
def_op('DELETE_SUBSCR', 61)
96-
9795
def_op('BINARY_LSHIFT', 62)
9896
def_op('BINARY_RSHIFT', 63)
9997
def_op('BINARY_AND', 64)
@@ -113,13 +111,12 @@ def jabs_op(name, op):
113111
def_op('INPLACE_XOR', 78)
114112
def_op('INPLACE_OR', 79)
115113
def_op('BREAK_LOOP', 80)
116-
114+
def_op('WITH_CLEANUP', 81)
117115
def_op('LOAD_LOCALS', 82)
118116
def_op('RETURN_VALUE', 83)
119117
def_op('IMPORT_STAR', 84)
120118
def_op('EXEC_STMT', 85)
121119
def_op('YIELD_VALUE', 86)
122-
123120
def_op('POP_BLOCK', 87)
124121
def_op('END_FINALLY', 88)
125122
def_op('BUILD_CLASS', 89)
@@ -171,7 +168,6 @@ def jabs_op(name, op):
171168
def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
172169
def_op('MAKE_FUNCTION', 132) # Number of args with default values
173170
def_op('BUILD_SLICE', 133) # Number of items
174-
175171
def_op('MAKE_CLOSURE', 134)
176172
def_op('LOAD_CLOSURE', 135)
177173
hasfree.append(135)
@@ -183,7 +179,6 @@ def jabs_op(name, op):
183179
def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
184180
def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
185181
def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
186-
187182
def_op('EXTENDED_ARG', 143)
188183
EXTENDED_ARG = 143
189184

0 commit comments

Comments
 (0)