Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix trailing text in doctest-syntax plot_directive. #20109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 29, 2021

The problem was that an input like

Some text.

>>> python_code()

The end.

would get split to

Some text.

>>> python_code()

and

The end.

and that unescape_doctest would then believe that The end. is already
python code (as it doesn't contain a >>>) and try to run it as is.

To fix this, instead of repeatedly calling contains_doctest everywhere
to guess whether fragments are doctest or python, just do it once at the
beginning, do the escaping once if needed at the beginning and then call
the new _functions (_split_code_at_show, _run_code) which don't try to
guess anymore. Because of the new (non-guessing) semantics these must
go to new functions, so let's make them private and just deprecate the old
(public) ones.

The escaping itself was done by unescape_doctest, but that had a
separate bug misparsing

This is an example...

>>> some_python()

... isn't it?

the last line would get incorrectly misparsed as a line continuation,
despite the blank line in between. Instead of trying to fix that
ourselves, just use doctest.script_from_examples which exactly serves
that purpose.

Closes #11007.
Closes #19111.

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

The problem was that an input like
```
Some text.

>>> python_code()

The end.
```
would get split to
```
Some text.

>>> python_code()
```
and
```
The end.
```
and that unescape_doctest would then believe that `The end.` is already
python code (as it doesn't contain a `>>>`) and try to run it as is.

To fix this, instead of repeatedly calling `contains_doctest` everywhere
to guess whether fragments are doctest or python, just do it once at the
beginning, do the escaping once if needed at the beginning and then call
the new _functions (`_split_code_at_show`, `_run_code`) which don't try to
guess anymore.  Because of the new (non-guessing) semantics these must
go to new functions, so let's make them private and just deprecate the old
(public) ones.

The escaping itself was done by `unescape_doctest`, but that had a
separate bug misparsing
```
This is an example...

>>> some_python()

... isn't it?
```
the last line would get incorrectly misparsed as a line continuation,
despite the blank line in between.  Instead of trying to fix that
ourselves, just use `doctest.script_from_examples` which exactly serves
that purpose.
@anntzer anntzer added this to the v3.5.0 milestone Apr 29, 2021
@timhoffm timhoffm merged commit 8ac6bcb into matplotlib:master May 1, 2021
@anntzer anntzer deleted the plot_directive_doctest_trailing_text branch May 1, 2021 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

plot_directive errantly tries to run code BUG: Plot directive fails if its content ends with a normal text line (sphinxext)
3 participants