]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
bpo-42827: Fix crash on SyntaxError in multiline expressions (GH-24140)
authorLysandros Nikolaou <lisandrosnik@gmail.com>
Thu, 14 Jan 2021 21:36:30 +0000 (23:36 +0200)
committerGitHub <noreply@github.com>
Thu, 14 Jan 2021 21:36:30 +0000 (21:36 +0000)
commite5fe509054183bed9aef42c92da8407d339e8af8
tree74174755289b6d7f87fea41612d9882f9f8202ba
parent971235827754eee6c0d9f7d39b52fecdfd4cb7b4
bpo-42827: Fix crash on SyntaxError in multiline expressions (GH-24140)

When trying to extract the error line for the error message there
are two distinct cases:

1. The input comes from a file, which means that we can extract the
   error line by using `PyErr_ProgramTextObject` and which we already
   do.
2. The input does not come from a file, at which point we need to get
   the source code from the tokenizer:
   * If the tokenizer's current line number is the same with the line
     of the error, we get the line from `tok->buf` and we're ready.
   * Else, we can extract the error line from the source code in the
     following two ways:
     * If the input comes from a string we have all the input
       in `tok->str` and we can extract the error line from it.
     * If the input comes from stdin, i.e. the interactive prompt, we
       do not have access to the previous line. That's why a new
       field `tok->stdin_content` is added which holds the whole input for the
       current (multiline) statement or expression. We can then extract the
       error line from `tok->stdin_content` like we do in the string case above.

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Lib/test/test_exceptions.py
Misc/NEWS.d/next/Core and Builtins/2021-01-06-17-06-37.bpo-42827.jtRR0D.rst [new file with mode: 0644]
Parser/pegen.c
Parser/tokenizer.c
Parser/tokenizer.h