Skip to content
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

Consider dropping ImportWarning for empty sys.path_hooks and sys.meta_path #65251

Closed
brettcannon opened this issue Mar 24, 2014 · 7 comments
Closed
Assignees
Labels
easy interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@brettcannon
Copy link
Member

BPO 21052
Nosy @brettcannon, @ncoghlan, @pitrou, @ericsnowcurrently, @vadmium, @pquentin

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/brettcannon'
closed_at = <Date 2014-10-10.14:55:42.348>
created_at = <Date 2014-03-24.17:20:08.573>
labels = ['interpreter-core', 'easy', 'type-bug']
title = 'Consider dropping ImportWarning for empty sys.path_hooks and sys.meta_path'
updated_at = <Date 2014-10-11.02:36:12.729>
user = 'https://github.com/brettcannon'

bugs.python.org fields:

activity = <Date 2014-10-11.02:36:12.729>
actor = 'berker.peksag'
assignee = 'brett.cannon'
closed = True
closed_date = <Date 2014-10-10.14:55:42.348>
closer = 'brett.cannon'
components = ['Interpreter Core']
creation = <Date 2014-03-24.17:20:08.573>
creator = 'brett.cannon'
dependencies = []
files = []
hgrepos = []
issue_num = 21052
keywords = ['easy']
message_count = 7.0
messages = ['214702', '214703', '214712', '214715', '222403', '229002', '229005']
nosy_count = 10.0
nosy_names = ['brett.cannon', 'ncoghlan', 'pitrou', 'Arfrever', 'ikanobori', 'python-dev', 'eric.snow', 'martin.panter', 'msmhrt', 'Quentin.Pradet']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21052'
versions = ['Python 3.5']

@brettcannon
Copy link
Member Author

Issue bpo-21049 managed to trigger a huge output of "ImportWarning: sys.meta_path is empty" because it managed to trigger an import during interpreter shutdown. The ImportWarning for sys.path_hooks and sys.meta_path were originally added because before importlib took over import it was totally legal to blank out sys.path_hooks and sys.meta_path and have import continue to work. But hopefully by the time Python 3.5 comes out there will be enough knowledge out there to not blindly empty them, making the warning superfluous as well as an annoyance when things go wrong in other respects.

@brettcannon brettcannon added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 24, 2014
@brettcannon
Copy link
Member Author

I should also say that dropping warnings leaves only a single case of ImportWarning related to empty directory names (in relation to Finder.find_module()). Once that can go away that will eliminate all uses of warnings in importlib and thus won't necessitate importing it in this case.

@pitrou
Copy link
Member

pitrou commented Mar 24, 2014

Note that in issue bpo-21409, sys.meta_path isn't "empty", it is actually None.

Another possibility is to drop those warnings only when the interpreter is shutting down (it would be easy to add a private API in sys to get that information).

@brettcannon
Copy link
Member Author

It's a simple if not sys.meta_path check so it doesn't differentiate. But that's actually a good point about it being None in this case due to the shutdown; if the check changed to if sys.meta_path is not None and len(sys.meta_path) == 0 then that should skip the shutdown issue while keeping the warning around.

Definitely going to be one of those instances where the tests are going to be more troublesome than the fix. =)

@msmhrt
Copy link
Mannequin

msmhrt mannequin commented Jul 6, 2014

Hi,

I can reproduce this issue with the following steps.

$ uname -a
Linux ashrose 3.2.0-65-generic #99-Ubuntu SMP Fri Jul 4 21:03:29 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ python3.4 --version
Python 3.4.1
$ touch spam.txt
$ python3.4 -Wa -c "class A: pass; a = open('spam.txt')"
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
_frozen_importlib:2150: ImportWarning: sys.meta_path is empty
_frozen_importlib:2150: ImportWarning: sys.meta_path is empty
_frozen_importlib:2150: ImportWarning: sys.meta_path is empty
_frozen_importlib:2150: ImportWarning: sys.meta_path is empty
_frozen_importlib:2150: ImportWarning: sys.meta_path is empty
...
$ python3.4 -Wa -c "a = open('spam.txt')"
sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
$ python3.4 -Wa -c "class A: pass; open('spam.txt')"
-c:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='spam.txt' mode='r' encoding='UTF-8'>
$

@brettcannon brettcannon self-assigned this Sep 24, 2014
@brettcannon
Copy link
Member Author

@python-dev
Copy link
Mannequin

python-dev mannequin commented Oct 10, 2014

New changeset d9f71bc6d897 by Brett Cannon in branch 'default':
Issue bpo-21052: Don't raise ImportWarning for sys.meta_path or
https://hg.python.org/cpython/rev/d9f71bc6d897

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants