Python: raise SyntaxError with lineno

2020-07-22 17:46发布

问题:

I am implementing a parser for a domain specific language, and want to be able to raise a SyntaxError. How do I set the filename, lineno and offset when raising this exception?

exception SyntaxError

Raised when the parser encounters a syntax error. This may occur in an import statement, in an exec statement, in a call to the built-in function eval() or input(), or when reading the initial script or standard input (also interactively).

Instances of this class have attributes filename, lineno, offset and text for easier access to the details. str() of the exception instance returns only the message.

Source: https://docs.python.org/3.2/library/exceptions.html#SyntaxError

回答1:

The answer is:

>>> raise SyntaxError('End quote missing', ("test.py", 1000, 11, "print 'bar"))                                                                             
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 1000
    print 'bar
              ^
SyntaxError: End quote missing

The second argument needs to be a 4-tuple.



回答2:

You can have a look here: https://github.com/python/cpython/blob/master/Objects/exceptions.c#L1273

I could come up only with this:

import traceback

print("Our exception")

try:
    print("(1)")
    raise SyntaxError('Test2', {'filename': "test.py", 'lineno': 1000, 'offset': 1, 'text': "My text ..."})
except SyntaxError as inst:
    print("(2)")
    print(inst.args)
    print("(3) Get filename %s" % inst.args[1]['filename'])
    print("(4) Traceback")
    traceback.print_tb(inst.__traceback__) 

The output is then:

Our exception
(1)
(2)
('Test2', {'offset': 1, 'filename': 'test.py', 'text': 'My text ...', 'lineno': 1000})
(3) Get filename test.py
(4) Traceback
  File "test.py", line 7, in <module>
    raise SyntaxError('Test2', {'filename': "test.py", 'lineno': 1000, 'offset': 1, 'text': "My text ..."})


回答3:

How about:

raise SyntaxError('filename: {}, lineno: {}, offset: {}'.format(
    filename, lineno, offset))