Raising SyntaxError inside a Python-based parser

前端 未结 2 809
梦毁少年i
梦毁少年i 2021-01-25 16:32

I\'m writing a Python-based parser that can understand some configuration files that we use. The files will basically consist of (name, type) and (name, value) pairs:

Pa

相关标签:
2条回答
  • 2021-01-25 16:41

    using SyntaxError might be confusing. Either I'd create some special exception type called eg. ParseError or ignore given value and just log in as a warning

    0 讨论(0)
  • 2021-01-25 16:53

    SyntaxError may be confusing, but I require further argumentation before I am persuaded that this is a good reason not to use it in these cases. IMO it's equally or more confusing to use a different kind of exception.

    Even if it might be confusing about when parsing (e.g.) domain-specific code, surely we can -in the error message- make it unambiguous that it is the domain-specific language that has the error, rather than the Python source.

    In addition, a SyntaxError offers ready-made fields for line number and filename. Rolling your own SyntaxError would require reinventing those wheels.

    The accepted answer here says

    Use the most specific Exception constructor that semantically fits your issue.

    ... and an error encountered while parsing is (semantically) a SyntaxError.

    My gut feeling is that an unambiguously constructed SyntaxError is the right thing in such cases. It's also the accepted answer here.

    Can anyone convince me otherwise?

    0 讨论(0)
提交回复
热议问题