Skip to content

Commit 532dd37

Browse files
author
collin.winter
committed
Backport PEP 3110's new 'except' syntax to 2.6.
git-svn-id: http://svn.python.org/projects/python/trunk@55446 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 3f46ad0 commit 532dd37

6 files changed

Lines changed: 12 additions & 7 deletions

File tree

Grammar/Grammar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ try_stmt: ('try' ':' suite
8585
with_stmt: 'with' test [ with_var ] ':' suite
8686
with_var: 'as' expr
8787
# NB compile.c makes sure that the default except clause is last
88-
except_clause: 'except' [test [',' test]]
88+
except_clause: 'except' [test [('as' | ',') test]]
8989
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
9090

9191
# Backward compatibility cruft to support:

Lib/compiler/transformer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def com_try_except_finally(self, nodelist):
930930
for i in range(3, len(nodelist), 3):
931931
node = nodelist[i]
932932
if node[0] == symbol.except_clause:
933-
# except_clause: 'except' [expr [',' expr]] */
933+
# except_clause: 'except' [expr [(',' | 'as') expr]] */
934934
if len(node) > 2:
935935
expr1 = self.com_node(node[2])
936936
if len(node) > 4:

Lib/test/test_grammar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ def __getitem__(self, i):
600600
def testTry(self):
601601
### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
602602
### | 'try' ':' suite 'finally' ':' suite
603-
### except_clause: 'except' [expr [',' expr]]
603+
### except_clause: 'except' [expr [('as' | ',') expr]]
604604
try:
605605
1/0
606606
except ZeroDivisionError:
@@ -609,7 +609,7 @@ def testTry(self):
609609
pass
610610
try: 1/0
611611
except EOFError: pass
612-
except TypeError, msg: pass
612+
except TypeError as msg: pass
613613
except RuntimeError, msg: pass
614614
except: pass
615615
else: pass

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- except clauses may now be spelled either "except E, target:" or
16+
"except E as target:". This is to provide forwards compatibility with
17+
Python 3.0.
18+
1519
- Deprecate BaseException.message as per PEP 352.
1620

1721
- Bug #1303614: don't expose object's __dict__ when the dict is

Python/ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,7 @@ ast_for_for_stmt(struct compiling *c, const node *n)
27652765
static excepthandler_ty
27662766
ast_for_except_clause(struct compiling *c, const node *exc, node *body)
27672767
{
2768-
/* except_clause: 'except' [test [',' test]] */
2768+
/* except_clause: 'except' [test [(',' | 'as') test]] */
27692769
REQ(exc, except_clause);
27702770
REQ(body, suite);
27712771

Python/graminit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ static arc arcs_42_1[2] = {
931931
{26, 2},
932932
{0, 1},
933933
};
934-
static arc arcs_42_2[2] = {
934+
static arc arcs_42_2[3] = {
935+
{78, 3},
935936
{27, 3},
936937
{0, 2},
937938
};
@@ -944,7 +945,7 @@ static arc arcs_42_4[1] = {
944945
static state states_42[5] = {
945946
{1, arcs_42_0},
946947
{2, arcs_42_1},
947-
{2, arcs_42_2},
948+
{3, arcs_42_2},
948949
{1, arcs_42_3},
949950
{1, arcs_42_4},
950951
};

0 commit comments

Comments
 (0)