X Tutup
Skip to content

Commit 8e8abff

Browse files
author
Troy Melhase
committed
Fixes for exception statements. See #21.
1 parent a21ed25 commit 8e8abff

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

java2python/lib/sourcetypes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def formatExpression(self, expr):
349349
elif isinstance(first, tuple) and isinstance(second, tuple):
350350
return (format(first), format(second))
351351
else:
352-
raise NotImplementedError('Unhandled expression type.')
352+
raise NotImplementedError('Unhandled expression type: %s' % expr)
353353

354354
def alternateName(self, name, key='renameAnyMap'):
355355
""" returns an alternate for given name
@@ -753,6 +753,7 @@ def isBadLabel(self):
753753
parent_names = [p.name for p in self.allParents]
754754
if 'while' not in parent_names and 'for' not in parent_names:
755755
return True
756+
756757
@property
757758
def isNoOp(self):
758759
""" True if instance does nothing
@@ -798,6 +799,6 @@ def writeTo(self, output, indent):
798799
if self.needsBlockIndicator:
799800
output.write(':')
800801
output.write('\n')
801-
if (not lines) and name not in ('break', 'continue', ):
802+
if (not lines) and name not in ('break', 'continue', 'raise'):
802803
self.addSource('pass')
803804
Source.writeTo(self, output, indent+1)

java2python/lib/walker.g

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
Current:
33

4-
This file is part of IbPy. Redistributed under terms of original
5-
license below. Modifications Copyright (C) Troy Melhase
4+
This file is part of java2python. Redistributed under terms of
5+
original license below. Modifications Copyright (C) Troy Melhase
66
<troy@gci.net>.
77

88
Original:
@@ -444,7 +444,7 @@ statement [block]
444444
| {
445445
raise_stat = block.newStatement("raise")
446446
}
447-
#("throw" raise_exp = expression[block])
447+
#("throw" raise_exp = expression[block, False])
448448
{
449449
raise_stat.setExpression(raise_exp)
450450
}
@@ -462,7 +462,7 @@ case_group [block, switch_expr]
462462
right = block.missingValue
463463
}
464464
#(CASE_GROUP
465-
(#("case"
465+
(#("case"
466466
right = expression[other, False]) | "default")+
467467
statement_list[other]
468468
)
@@ -479,13 +479,15 @@ case_group [block, switch_expr]
479479
try_block [block]
480480
{
481481
try_stat = block.newStatement("try")
482-
except_stat = block.newStatement("except")
482+
483483
finally_stat = block.newStatement("finally")
484484
}
485485

486486
: #("try"
487487
statement_list[try_stat]
488-
(handler[except_stat])*
488+
({except_stat = block.newStatement("except")}
489+
handler[except_stat]
490+
)*
489491
(#("finally" statement_list[finally_stat]))?
490492
)
491493
;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class TryCatchTests {
2+
// this works
3+
public void tryCatchSimple() {
4+
try {
5+
System.out.println("try block");
6+
} catch (Exception e) {
7+
System.out.println("catch Exception block");
8+
}
9+
}
10+
11+
// this collapse both catch blocks into a single except
12+
public void tryCatchCompound() {
13+
try {
14+
System.out.println("try block");
15+
} catch (RuntimeException re) {
16+
System.out.println("catch RuntimeException block");
17+
} catch (Exception e) {
18+
System.out.println("catch Exception block");
19+
}
20+
}
21+
22+
// throwing generates broken python
23+
public void tryCatchThrowSimple() {
24+
try {
25+
System.out.println("try block");
26+
} catch (Exception e) {
27+
System.out.println("catch Exception block");
28+
throw e;
29+
}
30+
}
31+
32+
// same problem with multiple catches
33+
public void tryCatchThrowCompound() {
34+
try {
35+
System.out.println("try block");
36+
} catch (RuntimeException re) {
37+
System.out.println("catch RuntimeException block");
38+
throw re;
39+
} catch (Exception e) {
40+
System.out.println("catch Exception block");
41+
throw e;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)
X Tutup