How to process syntax errors
Pierre-Alain Dorange
pdorange at pas-de-pub-merci.mac.com
Tue Oct 11 04:02:39 EDT 2016
Chris Angelico <rosuav at gmail.com> wrote:
> >> Yes and no. Syntax errors are detected when the script is compiled, so
> >> you can't do something like this:
> >
> > You're right, except that Python is never compiled, it was just checked
> > for syntax error before interpreting code.
>
> https://docs.python.org/3/library/functions.html#compile
>
> It's compiled.
Using this function, the code is "compiled".
I do not think this function is often used and most python project
simply use the interpreter (which do a small translation into byte-code
to be faster and check syntax error before running interpretation).
>
> >> However, you can catch this at some form of outer level. If you're
> >> using exec/eval to run the code, you can guard that:
> >
> > Your solution are OK, but that's not very "pythonic".
> >
> > Python was not design to be used in such a way (intercepting syntax
> > error). There was hack to made somthing like requested but that only a
> > hack and should not be used in real world.
>
> The error is raised as an exception, which means it is most definitely
> meant to be able to be caught.
Using compile() function yes.
So yes there is a way to check "syntax error" before executing code
(using compile function and exceptions) but it was not standard, nor
widely used... It was still a hack for me, but perhaps i misunderstood
or misinterpret.
--
Pierre-Alain Dorange Moof <http://clarus.chez-alice.fr/>
Ce message est sous licence Creative Commons "by-nc-sa-2.0"
<http://creativecommons.org/licenses/by-nc-sa/2.0/fr/>
More information about the Python-list
mailing list