[Q] raise exception with fake filename and linenumber

K

kwatch

Hi all,

Is it possible to raise exception with custom traceback to specify
file and line?

Situation
=========

I'm creating a certain parser.
I want to report syntax error with the same format as other exception.

Example
=======

parser.py:
-------------------------
1: def parse(filename):
2: if something_is_wrong():
3: linenum = 123
4: raise Exception("syntax error on %s, line %s" % (filename,
linenum))
5:
6: parse('example.file')
-------------------------

current result:
-------------------------
Traceback (most recent call last):
File "/tmp/parser.py", line 6, in <module>
parse('example.file')
File "/tmp/parser.py", line 4, in parse
raise Exception("syntax error on %s, line %s" % (filename,
linenum))
Exception: syntax error on example.file, line 123
-------------------------

my hope is:
-------------------------
Traceback (most recent call last):
File "/tmp/parser.py", line 6, in <module>
parse('example.file')
File "/tmp/parser.py", line 4, in parse
raise Exception("syntax error on %s, line %s" % (filename,
linenum))
File "/tmp/example.file", line 123
foreach item in items # wrong syntax line
Exception: syntax error
-------------------------

I guess I must create dummy traceback data, but I don't know how to do
it.
Could you give me an advice?

Thank you.
 
G

Gabriel Genellina

Is it possible to raise exception with custom traceback to specify
file and line?
I'm creating a certain parser.
I want to report syntax error with the same format as other exception.
-------------------------
1: def parse(filename):
2: if something_is_wrong():
3: linenum = 123
4: raise Exception("syntax error on %s, line %s" % (filename,
linenum))
5:
6: parse('example.file')
-------------------------

my hope is:
-------------------------
Traceback (most recent call last):
File "/tmp/parser.py", line 6, in <module>
parse('example.file')
File "/tmp/parser.py", line 4, in parse
raise Exception("syntax error on %s, line %s" % (filename,
linenum))
File "/tmp/example.file", line 123
foreach item in items # wrong syntax line
Exception: syntax error
-------------------------

The built-in SyntaxError exception does what you want. Constructor
parameters are undocumented, but they're as follows:

raise SyntaxError("A descriptive error message", (filename, linenum,
colnum, source_line))

colnum is used to place the ^ symbol (10 in this fake example). Output:

Traceback (most recent call last):
File "1.py", line 9, in <module>
foo()
File "1.py", line 7, in foo
raise SyntaxError("A descriptive error message", (filename, linenum,
colnum, "this is line 123 in example.file"))
File "example.file", line 123
this is line 123 in example.file
^
SyntaxError: A descriptive error message
 
D

Dennis Lee Bieber

Situation
=========

I'm creating a certain parser.
I want to report syntax error with the same format as other exception.

Personally, I would NOT consider this a valid reason to try to use a
Python exception format.

You are creating an application which has to process a data file
containing whatever language you are parsing. Therefore, to me, reports
of errors IN that language must be created as part of your parser
application. Python exceptions report errors in the executing Python
code.

Or, in other words, I think you'd have to create your own
"exception" handler system to report traces in the source domain (the
input data file).
 
K

kwatch

The built-in SyntaxError exception does what you want. Constructor  
parameters are undocumented, but they're as follows:

    raise SyntaxError("A descriptive error message", (filename, linenum,  
colnum, source_line))

colnum is used to place the ^ symbol (10 in this fake example). Output:

Traceback (most recent call last):
   File "1.py", line 9, in <module>
     foo()
   File "1.py", line 7, in foo
     raise SyntaxError("A descriptive error message", (filename, linenum,  
colnum, "this is line 123 in example.file"))
   File "example.file", line 123
     this is line 123 in example.file
              ^
SyntaxError: A descriptive error message

Thank you Gabriel,
this is great help for me.

By the way, is it hard to specify any other exception class instead of
SyntaxError?
The SyntaxError class is a good solution in my case, but if possible,
I want to know
more general solution to specify filename and linenum for exception.
 
G

Gabriel Genellina

By the way, is it hard to specify any other exception class instead of
SyntaxError?
The SyntaxError class is a good solution in my case, but if possible,
I want to know
more general solution to specify filename and linenum for exception.

You can always store any info you want in the exception object, just write
__str__ accordingly.
The advantage of SyntaxError is that it is special-cased in the
interpreter itself as to add some spaces and the ^ character.
The disadvantages are already pointed out by Dennis Lee Bieber and aren't
minor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top