8000 bpo-40275: reduce importing module nums by lazy import in libregrtest by shihai1991 · Pull Request #20207 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40275: reduce importing module nums by lazy import in libregrtest #20207

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

Closed
wants to merge 3 commits into from

Conversation

shihai1991
Copy link
Member
@shihai1991 shihai1991 commented May 19, 2020

Make the the following imports lazy in lib.regrtest:
inspect, asyncio, logging, shutil, urllib, textwrap

Remove redundant import in lib.regrtest:
_multiprocessing

https://bugs.python.org/issue40275

@shihai1991
Copy link
Member Author

After this PR, the import module nums from 248 to 146 when I run from test.libregrtest import *

@shihai1991
Copy link
Member Author

@vstinner @serhiy-storchaka pls take a look if you have free time.

Copy link
Member
@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

Hum. I'm not sure that this change reduces the number of imported modules when a test in run with "./python -m test test_os". For example, set_temp_dir() is called to run tests, and so tempfile is imported anyway. setup_tests() is also called in all cases before running a test. It's made on purpose.

Maybe we need a mechanism to load everything needed to run tests, and restore old sys.modules before loading tests. The risk is to load a module multiple times, but I don't think that it's an issue.

@serhiy-storchaka: What do you think?

8000

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@shihai1991
Copy link
Member Author

Hum. I'm not sure that this change reduces the number of imported modules when a test in run with "./python -m test test_os". For example, set_temp_dir() is called to run tests, and so tempfile is imported anyway. setup_tests() is also called in all cases before running a test. It's made on purpose.

Oh, you are right. ./python -m test xx will call some function of libregrtest.
To some case of 'set_temp_dir()', Looks like I need revert it now.

Maybe we need a mechanism to load everything needed to run tests, and restore old sys.modules before loading tests. The risk is to load a module multiple times, but I don't think that it's an issue.

Good idea. But I am not sure it will increse the complexity of unit test~

@shihai1991
Copy link
Member Author

@vstinner Hi, victor.

@shihai1991
Copy link
Member Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

@bedevere-bot bedevere-bot requested a review from vstinner June 17, 2020 12:56

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

@@ -1,18 +1,14 @@
import asyncio
Copy link
Member

Choose a reason for hiding this comment

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

IMO these changes are useless, since saved_test_environment() is used by runtest(). I mean that all these imports are imported anyway.

If someone wants to reduce the number of imported modules, saved_test_environment() must be redesigned. Currently, it has no knowledge of what test is run, and so all checks are always run.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, thanks, victor. I will check the detail of this function.

@@ -2,7 +2,6 @@
import re
import sys
import warnings
from inspect import isabstract
Copy link
Member

Choose a reason for hiding this comment

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

Rather than modifying refleak imports, it would be more generic to only import the refleak module when it's used.

Currently, clear_caches() is always called. Maybe we can only clear caches when -R 3:3 option is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks, victor. I will try your suggestion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Rather than modifying refleak imports, it would be more generic to only import the refleak module when it's used.

Currently, clear_caches() is always called. Maybe we can only clear caches when -R 3:3 option is used.

about this idea, I found you and serihy discussed the clear_caches() in https://bugs.python.org/issue23839.
the point is that it's maybe raise some memoryError when calling clear_caches() after -R.
So I thought lazy import isabstract in dash_R() would be more safe(it would be called when we use -R 3:3 in tests.)

@shihai1991
Copy link
Member Author

thanks victor for your suggestions.

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.

4 participants
0