callidus / tinypy

Automatically exported from code.google.com/p/tinypy
Other
0 stars 0 forks source link

cannot catch an exception #43

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Here the code.

1 try:
2     raise "a"
3 except e:
4     print e

What is the expected output? What do you see instead?
Should print ...

| a

Instead, this exception occured :

| (_tp_dict_get) KeyError: e

at line of 4

What version of the product are you using? On what operating system?
Version : from SVN, revision 146
OS : Linux Fedora

Please provide any additional information below.
As you understood, e does not exist in the "except" context. I cannot get
the catched exception.

Original issue reported on code.google.com by arab.ga...@gmail.com on 10 Jul 2009 at 6:11

GoogleCodeExporter commented 9 years ago
I think this error is normal.  The first token after the except keyword should 
be the
type of the exception we are looking for. e.g. :

class A:
    pass

try:
    raise A()
except A, ex:
    print(ex)

This code also fails with tinypy, but in the print(ex) statement.

I also noticed that putting several exception clauses is not handled, e.g. :

class A: pass
class B: pass

try:
    raise B()
except A: print("got A")
except B: print("got B")

will print "got A" instead of "got B"

Original comment by charlie...@gmail.com on 5 Feb 2010 at 9:18

GoogleCodeExporter commented 9 years ago
A first attempt to solve the issue.  With this patch we can run example like 
this one :

class A:
    def msg(self):
        return "hello"

try:
    raise A()
except A, ex:
    print("got this :", ex.msg())

The parenthesis in the raise are important, because tinypy won't automatically
instantiate A() like cpython would do.

Original comment by charlie...@gmail.com on 5 Feb 2010 at 4:19

Attachments:

GoogleCodeExporter commented 9 years ago
This second patch works with python 3 syntax (except X as x)

Original comment by charlie...@gmail.com on 10 Feb 2010 at 5:58

Attachments: