Discussion:
[Jython-users] Some help with error messages
James Ervin
2016-08-16 19:15:41 UTC
Permalink
I was wondering if someone could point me to either the jython class or a
document where the format of error messages is defined.

I am working on embedding a jython engine inside an Eclipse RCP
application. I would really love to be able to extract line numbers (if
not exact character offsets) from errors generated by bad python code.

For example I get:
SyntaxError: ("no viable alternative at input 'crap'", ('<string>', 5, 11,
' crappy crap crap\n'))

I am trying to figure out how to successfully extract the 5 and 11 there.

Another example:
Traceback (most recent call last):File "<string>", line 1, in
<module>AttributeError: 'NoneType' object has no attribute 'getName'

Obviously the line followed by the number should be sufficient.

Also is there a better way to get these errors than catch exceptions from
PythonInterpreter.exec()?

Thanks,
James E. Ervin, IV

"Noli nothis permittere te terere."
"Romanes eunt domus."
Blog: http://iacobus.blogspot.com
Jeff Allen
2016-08-18 06:37:33 UTC
Permalink
Syntax errors are mostly reported directly from code generated by the
ANTLR parser-generator, so will reflect its conventions: "no viable
alternative" is one of those. A move to ANTLR 4 (suggested but not
straightforward) could change them somewhat, but they are quite
stylised. Our guidance for other error messages is to be as like the
ones from CPython as is sensible -- nothing more formal is consistently
followed.

The code that formats exceptions for output (may help with your ideas)
is here:
https://hg.python.org/jython/file/tip/src/org/python/core/Py.java#l1173

It may also be worth looking at sys.excepthook as an alternative to
catching the exception, but catching sounds like a natural Java approach.

Jeff Allen
Post by James Ervin
I was wondering if someone could point me to either the jython class
or a document where the format of error messages is defined.
I am working on embedding a jython engine inside an Eclipse RCP
application. I would really love to be able to extract line numbers
(if not exact character offsets) from errors generated by bad python code.
SyntaxError: ("no viable alternative at input 'crap'", ('<string>', 5,
11, ' crappy crap crap\n'))
I am trying to figure out how to successfully extract the 5 and 11 there.
Traceback (most recent call last):File "<string>", line 1, in
<module>AttributeError: 'NoneType' object has no attribute 'getName'
Obviously the line followed by the number should be sufficient.
Also is there a better way to get these errors than catch exceptions
from PythonInterpreter.exec()?
Thanks,
James E. Ervin, IV
"Noli nothis permittere te terere."
"Romanes eunt domus."
Blog: http://iacobus.blogspot.com
------------------------------------------------------------------------------
_______________________________________________
Jython-users mailing list
https://lists.sourceforge.net/lists/listinfo/jython-users
------------------------------------------------------------------------------
Loading...