Skip to content

Commit 2af690f

Browse files
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) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent b5cedd0 commit 2af690f

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
@@ -409,14 +409,16 @@ The following exceptions are the exceptions that are usually raised.
409409

410410
.. versionadded:: 3.5
411411

412-
.. exception:: SyntaxError
412+
.. exception:: SyntaxError(message, details)
413413

414414
Raised when the parser encounters a syntax error. This may occur in an
415-
:keyword:`import` statement, in a call to the built-in functions :func:`exec`
415+
:keyword:`import` statement, in a call to the built-in functions
416+
:func:`compile`, :func:`exec`,
416417
or :func:`eval`, or when reading the initial script or standard input
417418
(also interactively).
418419

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

421423
.. attribute:: filename
422424

@@ -446,6 +448,11 @@ The following exceptions are the exceptions that are usually raised.
446448
The column in the end line where the error occurred finishes. This is
447449
1-indexed: the first character in the line has an ``offset`` of 1.
448450

451+
For errors in f-string fields, the message is prefixed by "f-string: "
452+
and the offsets are offsets in a text constructed from the replacement
453+
expression. For example, compiling f'Bad {a b} field' results in this
454+
args attribute: ('f-string: ...', ('', 1, 2, '(a b)\n', 1, 5)).
455+
449456
.. versionchanged:: 3.10
450457
Added the :attr:`end_lineno` and :attr:`end_offset` attributes.
451458

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)