Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
gh-39102: Show continuation lines when doctest crashes
    
I think this makes more sense.

Normally the format is like

```
sage: code(code)  ## line 1234 ##
'result'
```

or

```
sage: code(code)
code(code)  ## line 1234 ##
'result'
```

with the patch, the display becomes

```
sage: code(code)
....: code(code)  ## line 1234 ##
'result'
```


### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - #12345: short description why this is a dependency -->
<!-- - #34567: ... -->
    
URL: #39102
Reported by: user202729
Reviewer(s): Julian Rüth
  • Loading branch information
Release Manager committed Mar 9, 2025
2 parents bfbac28 + 41b39bb commit b84a0da
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ jobs:
uses: tj-actions/changed-files@v45
with:
# File extensions for doctests per sage.doctest.control.skipfile
# Also src/sage/doctests/tests/ are excluded because of nodoctest file
# which would make sage.doctest.control.skipdir return True
files_yaml: |
configures:
- 'build/pkgs/*/spkg-configure.m4'
Expand All @@ -118,6 +120,7 @@ jobs:
doctests:
- 'src/**/*.{py,pyx,pxd,pxi,sage,spyx,rst,tex}'
- '!src/{setup,conftest*}.py'
- '!src/sage/doctest/tests/*'
- name: Determine targets to build
id: build-targets
Expand Down Expand Up @@ -246,7 +249,7 @@ jobs:
./sage -python -m pytest -c tox.ini -qq --doctest --collect-only || true
shell: sh .ci/docker-exec-script.sh BUILD /sage {0}

- name: Test changed files (sage -t --new)
- name: Test changed files
if: (success() || failure()) && steps.container.outcome == 'success' && steps.changed-files.outputs.doctests_all_changed_files
run: |
export MAKE="make -j2 --output-sync=recurse" SAGE_NUM_THREADS=4
Expand Down
3 changes: 2 additions & 1 deletion src/sage/doctest/forker.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,8 @@ def _run(self, test, compileflags, out):
# We print the example we're running for easier debugging
# if this file times out or crashes.
with OriginalSource(example):
print("sage: " + example.source[:-1] + " ## line %s ##" % (test.lineno + example.lineno + 1))
assert example.source.endswith("\n"), example
print("sage: " + example.source[:-1].replace("\n", "\n....: ") + " ## line %s ##" % (test.lineno + example.lineno + 1))
# Update the position so that result comparison works
self._fakeout.getvalue()
if not quiet:
Expand Down
23 changes: 18 additions & 5 deletions src/sage/doctest/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,21 @@
...
16
A different kind of crash::
A different kind of crash (also test printing of line continuation ``...:``,
represented by ``<DOTSCOLON>`` below)::
sage: subprocess.call(["sage", "-t", "--warn-long", "0", # long time
....: "--random-seed=0", "--optional=sage", "fail_and_die.rst"], **kwds)
sage: # long time
sage: proc = subprocess.run(["sage", "-t", "--warn-long", "0",
....: "--random-seed=0", "--optional=sage", "fail_and_die.rst"], **kwds,
....: stdout=subprocess.PIPE, text=True)
sage: # the replacements are needed to avoid the strings being interpreted
....: # specially by the doctesting framework
sage: print(proc.stdout.replace('sage:', 'sage<COLON>').replace('....:', '<DOTSCOLON>'))
Running doctests...
Doctesting 1 file.
sage -t --warn-long 0.0 --random-seed=0 fail_and_die.rst
**********************************************************************
File "fail_and_die.rst", line 5, in sage.doctest.tests.fail_and_die
File "fail_and_die.rst", line 8, in sage.doctest.tests.fail_and_die
Failed example:
this_gives_a_NameError
Exception raised:
Expand All @@ -313,11 +319,18 @@
Killed due to kill signal
**********************************************************************
Tests run before process (pid=...) failed:
...
sage<COLON> import time, signal ## line 4 ##
sage<COLON> print(1,
<DOTSCOLON> 2) ## line 5 ##
1 2
sage<COLON> this_gives_a_NameError ## line 8 ##
sage<COLON> os.kill(os.getpid(), signal.SIGKILL) ## line 9 ##
**********************************************************************
----------------------------------------------------------------------
sage -t --warn-long 0.0 --random-seed=0 fail_and_die.rst # Killed due to kill signal
----------------------------------------------------------------------
...
sage: proc.returncode
16
Test that ``sig_on_count`` is checked correctly::
Expand Down
5 changes: 4 additions & 1 deletion src/sage/doctest/tests/fail_and_die.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
The :exc:`NameError` raised on the second line should be displayed, even
if we crash immediately afterwards::
if we crash immediately afterwards (also test printing of line continuation)::

sage: import time, signal
sage: print(1,
....: 2)
1 2
sage: this_gives_a_NameError
sage: os.kill(os.getpid(), signal.SIGKILL)

0 comments on commit b84a0da

Please sign in to comment.