msg402257 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-20 16:30 |
It seems like fine grained error locations do not work in failed doctest traceback output:
version 3.11.0a0
file contents:
------------------
def a(x):
"""
>>> 1 1
1
"""
import doctest
doctest.testmod()
OUTPUT
-------
Failed example:
1 1
Exception raised:
Traceback (most recent call last):
File "/Users/ak/opensource/cpython/Lib/doctest.py", line 1348, in __run
exec(compile(example.source, filename, "single",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<doctest __main__.a[0]>", line 1
1 1
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
The location in doctests that causes this:
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/doctest.py#L1348
|
msg402258 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-20 16:34 |
I've ran into this when looking at doctest docs, the issue is that they use the old example where a single column is highlighted, I want to update it to explain why doctest output differs from the one you get from REPL, but I probably need to understand why it happens to provide a good explanation.
Alternatively this may be fixed to be consistent if it's easy enough to do.
|
msg402260 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-09-20 16:39 |
Hummmm, could you explain a bit more in detail what is the expected output? I can see highlighting in the exec call that you pasted:
exec(compile(example.source, filename, "single",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The fact that you see those "^^^^^^" indicate that is working no? What is missing?
|
msg402263 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-20 18:01 |
Sorry, I should have noted I’m referring to the line 1 1
Which is underlined by a single caret, but on the command line it has 3
carets as expected.
|
msg402268 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-09-20 18:31 |
Ah, but that is a different issue. This is not PEP 657, this is a SyntaxError, so is related how those are printed, which I think is separared.
|
msg402269 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-09-20 18:31 |
Can you try a doctest that fails on something that is not a SyntaxError.
Something like:
>>> def foo(x):
... return x + 42
>>> foo(None)
|
msg402271 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-20 18:45 |
Pablo: that works fine, thanks!
I will look into updating the doctest docs, and will close this issue later
today ( or you can close it if you like).
|
msg402284 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-21 02:17 |
Thinking a bit more on this, I'm not sure this can be closed, as SyntaxError indicators seems not to be working. I'm not sure if this is limited to doctests or not. I've updated the title to narrow it down to SyntaxErrors.
I can look more into this if needed. If it's just doctests, it doesn't matter because the doctests ignore the indicators anyway, but if it can happen in some other cases, perhaps it's worth fixing.
However I don't have experience with the C parts of python, so I'm not sure where / how to look.
Pablo: let me know if you have any pointers on how to debug this, or if you think this should be closed.
|
msg402589 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2021-09-24 22:13 |
In 3.10+, end_lineno and end_offset fields were added to SyntaxError objects and the args tuple.
>>> try: compile('1 1', '', 'single')
... except SyntaxError as e: print(e.args)
...
('invalid syntax. Perhaps you forgot a comma?', ('', 1, 1, '1 1', 1, 4))
Here, line 1, offset 4 is the first 1-based column not part of the error.
The builtin default sys.excepthook was modified to read and use this new information and mark (end_offset - offset) columns with '^'s. This default prints what it does to sys.stderr.
The syntax error formatting in the traceback module was not altered. However, a new method, TracebackException._format_syntax_error, was extracted from TracebackException.format_exception_only so that the former could be overridden by software that simulates interaction.
The printed traceback does not come from line 1348. That *executes* the user code, but all Exceptions, including SyntaxError, are caught. If the exception is not expected and the run is not quiet, the exception is output by report_unexpected_exception(), as seen above as 'OUTPUT' and the lines that follows.
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/doctest.py#L1264
This calls _exception_traceback(exc_info).
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/doctest.py#L244
This calls traceback.print_exception, which I believe, for syntax errors, ultimately calls TracebackException._format_syntax_error.
I believe that the options for a fix are either
1. Call default sys.excepthook while capturing its output into a StringIO instance.
2. Assuming I am correct above about _format_syntax_error being called, monkeypatch it. In line 779,
https://github.com/python/cpython/blob/5846c9b71ee9277fe866b1bdee4cc6702323fe7e/Lib/traceback.py#L779
replace '^' with a field with a calculated number of ^s.
I need to do one of these two for IDLE, and may try both.
|
msg402652 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-25 22:09 |
Terry: I got it mostly working using your 2nd suggestion, I will do some testing and should be able to put up a PR in the next couple of days. Thanks for looking at this and explaining!
|
msg402662 - (view) |
Author: Andrei Kulakov (andrei.avk) *  |
Date: 2021-09-26 14:37 |
Terry: please take a look - https://github.com/python/cpython/pull/28567/files .
|
msg402670 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-09-26 16:58 |
One important thing is that "traceback.print_exception" should correctly print syntax errors .
|
msg402739 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-09-27 20:59 |
New changeset 20f439b6b9e1032930a31b88694ab9f37a09e6b4 by Pablo Galindo Salgado in branch 'main':
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (GH-28575)
https://github.com/python/cpython/commit/20f439b6b9e1032930a31b88694ab9f37a09e6b4
|
msg402742 - (view) |
Author: miss-islington (miss-islington) |
Date: 2021-09-27 21:26 |
New changeset c7fdd6879b5c8447ee65b1bec95a1db43837a7a5 by Miss Islington (bot) in branch '3.10':
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (GH-28575)
https://github.com/python/cpython/commit/c7fdd6879b5c8447ee65b1bec95a1db43837a7a5
|
msg403157 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2021-10-04 19:18 |
New changeset 1e2058214fffcb3919e0e127145106ade40a0420 by Pablo Galindo (Miss Islington (bot)) in branch '3.10':
bpo-45249: Ensure the traceback module prints correctly syntax errors with ranges (GH-28575)
https://github.com/python/cpython/commit/1e2058214fffcb3919e0e127145106ade40a0420
|
msg403588 - (view) |
Author: daniel hahler (blueyed) * |
Date: 2021-10-10 14:22 |
I've noticed a regression/change with the code change for this issue.
When not catching the exception from `compile("invalid(", "<stdin>", "single")` it has a caret below the opening parenthesis:
```
Traceback (most recent call last):
File "…/t-syntaxerror-chained.py", line 2, in <module>
compile("invalid(", "<stdin>", "single")
File "<stdin>", line 1
invalid(
^
SyntaxError: '(' was never closed
```
When using `traceback.print_exc` however this is missing:
```
Traceback (most recent call last):
File "…/t-syntaxerror-chained.py", line 2, in <module>
compile("invalid(", "<stdin>", "single")
File "<stdin>", line 1
invalid(
SyntaxError: '(' was never closed
```
The file used for testing:
```
try:
compile("invalid(", "<stdin>", "single")
except Exception:
# raise
__import__("traceback").print_exc()
```
(this change was noticed between 3.10.0rc2 and the final release with pdbpp's test suite)
I've not investigated further (yet), and also feel free to ask for creating a new issue, but I've figured it would be good to notify you here first (where the code was changed).
|
msg404103 - (view) |
Author: miss-islington (miss-islington) |
Date: 2021-10-16 17:27 |
New changeset fe0d9e22a52a10c4cbe52254b51f2d4e74d83568 by Pablo Galindo Salgado in branch 'main':
bpo-45249: Fix caret location when end_offset is set to 0 (GH-28855)
https://github.com/python/cpython/commit/fe0d9e22a52a10c4cbe52254b51f2d4e74d83568
|
msg404106 - (view) |
Author: miss-islington (miss-islington) |
Date: 2021-10-16 17:51 |
New changeset 5df35faf3611b459f7f32bfe9fd2ffa7fb2dc59e by Miss Islington (bot) in branch '3.10':
bpo-45249: Fix caret location when end_offset is set to 0 (GH-28855)
https://github.com/python/cpython/commit/5df35faf3611b459f7f32bfe9fd2ffa7fb2dc59e
|
|
Date |
User |
Action |
Args |
2022-04-11 14:59:50 | admin | set | github: 89412 |
2021-10-16 17:51:11 | miss-islington | set | messages:
+ msg404106 |
2021-10-16 17:37:28 | pablogsal | set | status: open -> closed stage: patch review -> resolved |
2021-10-16 17:27:55 | miss-islington | set | pull_requests:
+ pull_request27277 |
2021-10-16 17:27:51 | miss-islington | set | messages:
+ msg404103 |
2021-10-10 15:15:39 | pablogsal | set | pull_requests:
+ pull_request27162 |
2021-10-10 15:13:21 | pablogsal | set | stage: resolved -> patch review pull_requests:
+ pull_request27161 |
2021-10-10 15:08:06 | pablogsal | set | status: closed -> open |
2021-10-10 14:23:32 | blueyed | set | versions:
+ Python 3.10 |
2021-10-10 14:22:42 | blueyed | set | nosy:
+ blueyed messages:
+ msg403588
|
2021-10-04 19:18:42 | pablogsal | set | messages:
+ msg403157 |
2021-09-29 15:48:28 | pablogsal | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
2021-09-27 21:26:47 | miss-islington | set | messages:
+ msg402742 |
2021-09-27 20:59:26 | pablogsal | set | messages:
+ msg402739 |
2021-09-27 20:59:14 | miss-islington | set | nosy:
+ miss-islington pull_requests:
+ pull_request26969
|
2021-09-26 22:59:15 | pablogsal | set | pull_requests:
+ pull_request26958 |
2021-09-26 16:58:33 | pablogsal | set | messages:
+ msg402670 |
2021-09-26 14:37:48 | andrei.avk | set | messages:
+ msg402662 |
2021-09-26 14:10:12 | andrei.avk | set | keywords:
+ patch stage: needs patch -> patch review pull_requests:
+ pull_request26951 |
2021-09-25 22:09:13 | andrei.avk | set | messages:
+ msg402652 |
2021-09-24 22:13:12 | terry.reedy | set | title: SyntaxError location range indicator does not work in doctests -> Update doctect SyntaxErrors for location range nosy:
+ terry.reedy
messages:
+ msg402589
components:
+ Library (Lib), - Interpreter Core, Tests stage: needs patch |
2021-09-21 02:48:39 | andrei.avk | set | title: Syntax error location range indicator does not work in doctests -> SyntaxError location range indicator does not work in doctests |
2021-09-21 02:17:16 | andrei.avk | set | messages:
+ msg402284 title: Fine grained error locations do not work in doctests -> Syntax error location range indicator does not work in doctests |
2021-09-20 18:45:19 | andrei.avk | set | messages:
+ msg402271 |
2021-09-20 18:31:53 | pablogsal | set | messages:
+ msg402269 |
2021-09-20 18:31:13 | pablogsal | set | messages:
+ msg402268 |
2021-09-20 18:01:35 | andrei.avk | set | messages:
+ msg402263 |
2021-09-20 16:39:48 | pablogsal | set | messages:
+ msg402260 |
2021-09-20 16:34:13 | andrei.avk | set | messages:
+ msg402258 |
2021-09-20 16:30:58 | andrei.avk | create | |