Problem re-raising Python error to surrounding Javascript code (Brython 3.2.3)

11 views
Skip to first unread message

André

unread,
Nov 22, 2015, 7:20:04 PM11/22/15
to brython
I just upgraded to Brython 3.2.3 from 3.2.0 alpha and encountered a problem.  I found a workaround solution and would like to know if it is the best way to proceed and if it is likely to be future-proof.
[Note: I am working with Brython 3.2.3 only on a locally hosted private version of my site.]

In Reeborg's World (http://reeborg.ca/world.html) I run Python code from a Javascript context as follows (greatly simplified):

try {
    eval_python(src);
} catch (e) {
    //  console.log(e)
    process(e)
}

where eval_python(src) can be thought of being

def eval_python(src):
   exec(src, some_dict)

Up until now (Brython 3.2.0 and many earlier), "e" would be an object with various attributes including in particular  __name__ and "$message".  I also defined my own custom errors, with attributes, and could get them.

With Brython 3.2.3, I do not get this.  If I log the error in the console, instead of a "nice" error object with the relevant attributes, I get a "native error" raised at line 6423 of brython.js with the following information available

Error
  at Error (native)
  at _b_.SyntaxError (<anonymous>:105:390)
  at eval (<anonymous>:16:81)
  ...

Line 6423 of brython .js is var result=js_attr.apply(self.js,args)

So, no more "nice" javascript object to decipher...

=====
After much experimentation, I arrived at this workaround solution (again, simplified).  I catch any error in eval_python as follows:

def eval_python(src):
    window.__python_error = ""
    try:
        exec(src, some_dict)
    except Exception as e:
        window.__python_error = e

and I modified the way I run the code from Javascript as follows:

try {
    eval_python(src);
    if (window.__python_error) {
        throw window.__python_error;
    }
} catch (e) {
    //  console.log(e)
    process(e)
}

====
Is there a better way to have errors be lifted from the Python context to the surrounding Javascript context and handling them?

André




Pierre Quentel

unread,
Nov 23, 2015, 6:04:43 AM11/23/15
to brython


Le lundi 23 novembre 2015 01:20:04 UTC+1, André a écrit :
I just upgraded to Brython 3.2.3 from 3.2.0 alpha and encountered a problem.  I found a workaround solution and would like to know if it is the best way to proceed and if it is likely to be future-proof.
[Note: I am working with Brython 3.2.3 only on a locally hosted private version of my site.]

In Reeborg's World (http://reeborg.ca/world.html) I run Python code from a Javascript context as follows (greatly simplified):

try {
    eval_python(src);
} catch (e) {
    //  console.log(e)
    process(e)
}

where eval_python(src) can be thought of being

def eval_python(src):
   exec(src, some_dict)

Up until now (Brython 3.2.0 and many earlier), "e" would be an object with various attributes including in particular  __name__ and "$message".  I also defined my own custom errors, with attributes, and could get them.

With Brython 3.2.3, I do not get this.  If I log the error in the console, instead of a "nice" error object with the relevant attributes, I get a "native error" raised at line 6423 of brython.js with the following information available

I didn't think of such use cases when rewriting parts of the exception management. I have restored the previous behaviour in todays commit ; does it work for your application ?

Andre Roberge

unread,
Nov 23, 2015, 6:57:01 AM11/23/15
to bry...@googlegroups.com
Well... the way I do things is a bit unusual, I do admit.

I've tried it and it works.   However, for this case, since I would (apparently) have a workaround solution, if the new exception handling proves to be more useful, do not feel obliged to keep the old behaviour simply on my behalf.

André

 

--
You received this message because you are subscribed to a topic in the Google Groups "brython" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/brython/JLkdwzKL2z4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/2a0bfecf-c7e6-4d90-b7ab-dd76755f9367%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages