Skip to content

Commit

Permalink
Make try / except work
Browse files Browse the repository at this point in the history
Issue: skulpt#38 on Github
Issue: 109 and 114 on Google Code

I did this by not generating a JavaScript try block, but instead
added a try/catch around the whole run-time switch loop.

Each Python try/except already did a push/pop of the except block
on the $exc stack. So if the outer JS try/catch is triggered,
it checks the exception block stack and jumps to the exception
block.  If there is no active exception, it means some other
runtime error has occurred so the JS rethrows the exception
so it is exposed to the user in the browser.

This approach makes it far easier to implement the full
try/except/else/finally as any nesting issues are no longer
a problem - I did not implement that in this fix - but note
that it is now easier if someone wants to do it.

I am sure this code could be improved by someone more intimate
with the runtime.

*** This change may need to be adjusted for all the other instances of
switchCode and suffixCode in src/compile.js

Not knowing how to test those other cases I left them alone.
At least the changes to setupExcept and endExcept will insure
that the generated JS will be syntactically correct.  The worst
that will happen is that when code in a try block blows up, we
will not know to run the except block.  The other problem is that we
might catch something that is really an internal error - it might be
nice to add code in the above catch block that looked at
the kind of exception and only popped the stack for exceptions
that are from the original code rather than artifacts of some
code generation or execution environment error.  We at least
err on the side of exceptions being revealed to the user.

I added comments to these effects in the code - you may want to
remove those when you do the pull / merge of the changes.  I
just wanted to leave a really clear trail.
  • Loading branch information
csev committed Jan 24, 2013
1 parent cf84bbc commit fbeb06e
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/run/t335.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
print "----Start 01"
try:
print "First try"
try:
print "Second try - should see Second except next"
i = int('badint');
print "Second try - should not see this"
except:
print "Second except"
print "First try - should see First except next"
i = float('otherbadint')
print "First try - should not see this"
except:
print "First except"
print "----End 01"

print "----Start 02"
try:
print "First try"
try:
print "Second try"
except:
print "Second except - should not see this"
print "First try - should see First except next"
i = float('otherbadint')
print "First try - should not see this"
except:
print "First except"
print "----End 02"

print "----Start 03"
try:
print "First try"
try:
print "Second try"
except:
print "Second except - should not see this"
print "First try - after inner try"
except:
print "First except - should not see this"
print "----End 03"

print "----Start 04"
try:
print "First try - shuold see First Except next"
i = int('first');
print "First try - should not see this"
except:
print "First except"
try:
print "Second try - should see Second except next"
i = int('badint');
print "Second try - should not see this"
except:
print "Second except"
print "First except - After inner try/except"
print "----End 04"

print "----Start 05"
try:
print "First try"
try:
print "Second try - should see Second except next"
i = int('badint');
print "Second try - should not see this"
except:
print "Second except - should see First except next"
i = float('otherbadint')
print "Second except - should not see this"
print "First try - should not see this"
except:
print "First except"
print "----End 05"

print "----Start 06"
try:
print "First try"
if 123 < 12345 :
if 456 < 4567 :
print "You should see this"
else:
print "You should not see this (inner)"
else:
print "You should not see this"
print "First try - near the end"
except:
print "First except - should not see this"
print "----End 06"

print "----Start 07"
try:
print "First try"
if 123 < 12345 :
if 456 < 4567 :
print "Next you should see First except"
i = int('badint')
else:
print "You should not see this (inner)"
else:
print "You should not see this"
print "First try - near the end - you should not see this"
except:
print "First except - should see this"
print "----End 07"

41 changes: 41 additions & 0 deletions test/run/t335.py.real
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
----Start 01
First try
Second try - should see Second except next
Second except
First try - should see First except next
First except
----End 01
----Start 02
First try
Second try
First try - should see First except next
First except
----End 02
----Start 03
First try
Second try
First try - after inner try
----End 03
----Start 04
First try - shuold see First Except next
First except
Second try - should see Second except next
Second except
First except - After inner try/except
----End 04
----Start 05
First try
Second try - should see Second except next
Second except - should see First except next
First except
----End 05
----Start 06
First try
You should see this
First try - near the end
----End 06
----Start 07
First try
Next you should see First except
First except - should see this
----End 07
42 changes: 42 additions & 0 deletions test/run/t335.py.symtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Sym_type: module
Sym_name: top
Sym_lineno: 0
Sym_nested: False
Sym_haschildren: False
-- Identifiers --
name: float
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
name: i
is_referenced: False
is_imported: False
is_parameter: False
is_global: False
is_declared_global: False
is_local: True
is_free: False
is_assigned: True
is_namespace: False
namespaces: [
]
name: int
is_referenced: True
is_imported: False
is_parameter: False
is_global: True
is_declared_global: False
is_local: False
is_free: False
is_assigned: False
is_namespace: False
namespaces: [
]
Loading

0 comments on commit fbeb06e

Please sign in to comment.