Skip to content

bpo-1812: Fix newline conversion when doctest.testfile loads from a package whose loader has a get_data method. #17385

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
merged 14 commits into from
Mar 26, 2020

Conversation

pdonis
Copy link
Contributor

@pdonis pdonis commented Nov 26, 2019

bpo-1812: Fix newline conversion when doctest.testfile loads from a package whose loader has a get_data method.

This pull request fixes the newline conversion bug originally reported in issue 1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method. Commit b6ad8ee contains the change that implements the fix.

This pull request also adds a test that verifies that doctest.testfile does the newline conversion correctly when loading from a package whose loader has a get_data method. This test fails (a ValueError is raised for inconsistent whitespace) unless the change in commit b6ad8ee is applied. Commit dd02845 contains the changes that add the test; the test is added to the test_lineendings docstring along with supporting comments.

Also, the test_lineendings docstring is missing a test to check for correct handling of the old-style Mac newline \r. This pull request adds that case to the original test_lineendings docstring. Commit da35562 contains the changes that add this test and edit the surrounding comments accordingly.

Finally, this pull request contains commit 22094c8 that adds my name to the MISC/Acks file.

https://bugs.python.org/issue1812

…ings.

The test_lineendings test in test_doctest did not include a test with
old-style Mac line endings (\r). This commit adds one, and edits the
introductory discussion in test_lineendings accordingly.
…le from package with loader.get_data.

The previous test_doctest.test_lineendings did not cover the case where a testfile for doctest.testfile
is loaded from a package whose loader has a get_data method. This commit adds a test for that case.
…ackage with loader.get_data.

This commit corrects the newline conversion that is done in doctest._load_testfile when the file
is being loaded from a package whose loader has a get_data method.
@pdonis pdonis changed the title Fix issue 1812 bpo-1812: Fix newline conversion when doctest.testfile loads from a package whose loader has a get_data method. Nov 26, 2019
Copy link
Member

@zware zware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this is an incredibly clean patch, and you have the patience of a saint to have shepherded this along for 12 years! I have a style nit and a cleanup suggestion, but this is probably acceptable as is.

Thanks for the patch, and hopefully we'll finally get this landed!

@gpshead gpshead added the type-bug An unexpected behavior, bug, or error label Feb 12, 2020
@aeros
Copy link
Contributor

aeros commented Feb 12, 2020

This seems like it should be backported to 3.6+, no? It's fixing an internal function and making some changes to the tests, so I don't think there's any backwards compatibility concerns to worry about. I'll add the backport labels for now, but feel free to remove them if needed.

@zware
Copy link
Member

zware commented Feb 12, 2020

3.6 is in security-fix-only mode, so this definitely will not be backported that far.

@aeros
Copy link
Contributor

aeros commented Feb 12, 2020

Oh oops, for some reason I had forgotten that 3.6 was a security-only branch. Thanks for clarifying.

pdonis and others added 3 commits February 12, 2020 19:51
Break up long line to improve readability.

Co-Authored-By: Zachary Ware <[email protected]>
This commit wraps the doctest.testfile test with a package loader that
has a get_data method in a context manager to ensure proper removal
of the extra package hook.
@pdonis
Copy link
Contributor Author

pdonis commented Feb 21, 2020

Any update on this? Should I be looking at the 3.7 and 3.8 branches for backporting?

@aeros
Copy link
Contributor

aeros commented Feb 21, 2020

Any update on this?

I believe at this point we're just waiting for a follow-up review from @zware.

Should I be looking at the 3.7 and 3.8 branches for backporting?

The labels I added will trigger @miss-islington to attempt to open backport PRs to the 3.8 and 3.7 branches after this one is merged. It typically works without conflicts, but if there are any, you may have to resolve them manually with git cherry-pick (or the tool on PyPI, cherry-picker).

sys.modules.update(self.modules_before)


@contextlib.contextmanager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think some of this and the code above can be folded to use stuff from https://github.com/python/cpython/blob/master/Lib/test/test_importlib/fixtures.py like:

  • class OnSysPath(Fixtures):
    @staticmethod
    @contextlib.contextmanager
    def add_sys_path(dir):
    sys.path[:0] = [str(dir)]
    try:
    yield
    finally:
    sys.path.remove(str(dir))
    def setUp(self):
    super(OnSysPath, self).setUp()
    self.fixtures.enter_context(self.add_sys_path(self.site_dir))

  • @contextlib.contextmanager
    def install_finder(finder):
    sys.meta_path.append(finder)
    try:
    yield
    finally:
    sys.meta_path.remove(finder)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OnSysPath fixture appears to be designed for a unittest test case, not doctest. A standalone version of the add_sys_path context manager might work if combined with install_finder; however, I notice that neither of those clears the path importer cache, and neither one removes the imported test module from sys.modules when the test is done. I don't know enough about the internals of the test framework to know if leaving out those last two items is OK; I put them in to make sure that everything that could possibly be mutated by this particular test was restored afterwards.

@pdonis
Copy link
Contributor Author

pdonis commented Feb 22, 2020

@aeros Thanks for the update and for explaining how the backport process works.

@pdonis
Copy link
Contributor Author

pdonis commented Mar 1, 2020

@zware is there any update on this?

@@ -2659,12 +2663,52 @@ def test_testfile(): r"""
>>> sys.argv = save_argv
"""

class TestLoader(importlib.abc.MetaPathFinder, importlib.abc.ResourceLoader):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misnamed as this is implementing a finder and a loader, so call it an importer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll push an update changing the name to TestImporter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit bb836d7 does the name change.

pdonis added 2 commits March 16, 2020 14:17
Change the name of the class used to test the case of a package with
a loader.get_data method from TestLoader to TestImporter.
@pdonis
Copy link
Contributor Author

pdonis commented Mar 24, 2020

Any update on merging this?

@brettcannon brettcannon requested a review from zware March 24, 2020 19:06
@brettcannon
Copy link
Member

I refreshed the review request for @zware to see if he has time to review again.

Copy link
Member

@zware zware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind seeing the cleanup code cleaned up a bit in a separate PR, but as nobody has pointed out any actual problems with it as is, let's go ahead.

@zware zware merged commit e0b8101 into python:master Mar 26, 2020
@miss-islington
Copy link
Contributor

Thanks @pdonis for the PR, and @zware for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7, 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 26, 2020
…ackage whose loader has a get_data method (pythonGH-17385)

This pull request fixes the newline conversion bug originally reported in bpo-1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method.
(cherry picked from commit e0b8101)

Co-authored-by: Peter Donis <[email protected]>
@bedevere-bot
Copy link

GH-19175 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 26, 2020
…ackage whose loader has a get_data method (pythonGH-17385)

This pull request fixes the newline conversion bug originally reported in bpo-1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method.
(cherry picked from commit e0b8101)

Co-authored-by: Peter Donis <[email protected]>
@bedevere-bot
Copy link

GH-19176 is a backport of this pull request to the 3.7 branch.

miss-islington added a commit that referenced this pull request Mar 26, 2020
…ackage whose loader has a get_data method (GH-17385)

This pull request fixes the newline conversion bug originally reported in bpo-1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method.
(cherry picked from commit e0b8101)

Co-authored-by: Peter Donis <[email protected]>
miss-islington added a commit that referenced this pull request Mar 26, 2020
…ackage whose loader has a get_data method (GH-17385)

This pull request fixes the newline conversion bug originally reported in bpo-1812. When that issue was originally submitted, the open builtin did not default to universal newline mode; now it does, which makes the issue fix simpler, since the only code path that needs to be changed is the one in doctest._load_testfile where the file is loaded from a package whose loader has a get_data method.
(cherry picked from commit e0b8101)

Co-authored-by: Peter Donis <[email protected]>
@pdonis
Copy link
Contributor Author

pdonis commented Mar 26, 2020

@zware @brettcannon Thanks so much for getting this merged!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants