-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
…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.
There was a problem hiding this 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!
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. |
3.6 is in security-fix-only mode, so this definitely will not be backported that far. |
Oh oops, for some reason I had forgotten that 3.6 was a security-only branch. Thanks for clarifying. |
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.
Any update on this? Should I be looking at the 3.7 and 3.8 branches for backporting? |
I believe at this point we're just waiting for a follow-up review from @zware.
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 |
sys.modules.update(self.modules_before) | ||
|
||
|
||
@contextlib.contextmanager |
There was a problem hiding this comment.
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:
-
cpython/Lib/test/test_importlib/fixtures.py
Lines 71 to 83 in fd5116c
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)) -
cpython/Lib/test/test_importlib/fixtures.py
Lines 50 to 56 in fd5116c
@contextlib.contextmanager def install_finder(finder): sys.meta_path.append(finder) try: yield finally: sys.meta_path.remove(finder)
There was a problem hiding this comment.
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.
@aeros Thanks for the update and for explaining how the backport process works. |
@zware is there any update on this? |
Lib/test/test_doctest.py
Outdated
@@ -2659,12 +2663,52 @@ def test_testfile(): r""" | |||
>>> sys.argv = save_argv | |||
""" | |||
|
|||
class TestLoader(importlib.abc.MetaPathFinder, importlib.abc.ResourceLoader): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Change the name of the class used to test the case of a package with a loader.get_data method from TestLoader to TestImporter.
Any update on merging this? |
I refreshed the review request for @zware to see if he has time to review again. |
There was a problem hiding this 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.
…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]>
GH-19175 is a backport of this pull request to the 3.8 branch. |
…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]>
GH-19176 is a backport of this pull request to the 3.7 branch. |
…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]>
…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]>
@zware @brettcannon Thanks so much for getting this merged! |
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