Skip to content

Commit d5f8bd6

Browse files
authored
[3.9] bpo-44322: Document more SyntaxError details. (GH-26562)
1. SyntaxError args have a tuple of other attributes. 2. Attributes are adjusted for errors in f-string field expressions. 3. Compile() can raise SyntaxErrors. (cherry picked from commit 67dfa6f)
1 parent 8e2c0fd commit d5f8bd6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Doc/library/exceptions.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,16 @@ The following exceptions are the exceptions that are usually raised.
390390

391391
.. versionadded:: 3.5
392392

393-
.. exception:: SyntaxError
393+
.. exception:: SyntaxError(message, details)
394394

395395
Raised when the parser encounters a syntax error. This may occur in an
396-
:keyword:`import` statement, in a call to the built-in functions :func:`exec`
396+
:keyword:`import` statement, in a call to the built-in functions
397+
:func:`compile`, :func:`exec`,
397398
or :func:`eval`, or when reading the initial script or standard input
398399
(also interactively).
399400

400401
The :func:`str` of the exception instance returns only the error message.
402+
Details is a tuple whose members are also available as separate attributes.
401403

402404
.. attribute:: filename
403405

@@ -417,6 +419,11 @@ The following exceptions are the exceptions that are usually raised.
417419

418420
The source code text involved in the error.
419421

422+
For errors in f-string fields, the message is prefixed by "f-string: "
423+
and the offsets are offsets in a text constructed from the replacement
424+
expression. For example, compiling f'Bad {a b} field' results in this
425+
args attribute: ('f-string: ...', ('', 1, 4, '(a b)\n')).
426+
420427

421428
.. exception:: IndentationError
422429

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Document that SyntaxError args have a details tuple and that details are
2+
adjusted for errors in f-string field replacement expressions.

0 commit comments

Comments
 (0)