Hello
I have subclassed code.Interactiv eInterpreter for testing
an "interprete r" i have written myself.
The interpreter is a function (evaluate) that can raise
MyError exceptions. I want these to be reported with an
indication of the position (^) as in the python interactive
interpreter.
The code below does this, but I don't like the raise-catch
of the syntax error before using self.showsyntax error().
How could I improve this? Should I subclass from SyntaxError?
The other thing is that the ^ is shown to the left of the
faulty character, I could easily correct that, but why is this?
Leo
=============== =============== =============== =============== ============
import code
class MyError(Excepti on):
def __init__(self, msg, pos):
Exception.__ini t__(self, msg)
self.pos = pos
def evaluate(source ):
for (pos,c) in enumerate(sourc e):
if not c in "0123456789 ":
raise MyError("Unexpe cted Symbol %s" % c, pos)
return int(source)
class MyConsole(code. InteractiveCons ole):
def raw_input(self, prompt):
return code.Interactiv eConsole.raw_in put(self, 'abc '+prompt)
def runsource(self, source, filename='<stdi n>'):
try:
print evaluate(source )
except MyError, e:
try:
se = SyntaxError(str (e), ('', 1, e.pos, source))
raise se
except:
self.showsyntax error()
if __name__ == '__main__':
MyConsole().int eract()
=============== =============== =============== =============== ===========
And here what it shows on the console:
leonhard@leovai o:~/mypy$ python cons.py
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
(MyConsole)
abc >>1234
1234
abc >>12e4
File "<string>", line 1
12e4
^
SyntaxError: Unexpected Symbol e
abc >>>
I have subclassed code.Interactiv eInterpreter for testing
an "interprete r" i have written myself.
The interpreter is a function (evaluate) that can raise
MyError exceptions. I want these to be reported with an
indication of the position (^) as in the python interactive
interpreter.
The code below does this, but I don't like the raise-catch
of the syntax error before using self.showsyntax error().
How could I improve this? Should I subclass from SyntaxError?
The other thing is that the ^ is shown to the left of the
faulty character, I could easily correct that, but why is this?
Leo
=============== =============== =============== =============== ============
import code
class MyError(Excepti on):
def __init__(self, msg, pos):
Exception.__ini t__(self, msg)
self.pos = pos
def evaluate(source ):
for (pos,c) in enumerate(sourc e):
if not c in "0123456789 ":
raise MyError("Unexpe cted Symbol %s" % c, pos)
return int(source)
class MyConsole(code. InteractiveCons ole):
def raw_input(self, prompt):
return code.Interactiv eConsole.raw_in put(self, 'abc '+prompt)
def runsource(self, source, filename='<stdi n>'):
try:
print evaluate(source )
except MyError, e:
try:
se = SyntaxError(str (e), ('', 1, e.pos, source))
raise se
except:
self.showsyntax error()
if __name__ == '__main__':
MyConsole().int eract()
=============== =============== =============== =============== ===========
And here what it shows on the console:
leonhard@leovai o:~/mypy$ python cons.py
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
(MyConsole)
abc >>1234
1234
abc >>12e4
File "<string>", line 1
12e4
^
SyntaxError: Unexpected Symbol e
abc >>>