You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
-W option cannot use non-standard categories #66733
Reacted by Dave Brondsema, Katsuhiko Nishimra and YevheniiKotyrlo
Activity
remram44 commented on Oct 2, 2014
remram44mannequin
MannequinAuthor
More actions
warnings._processoptions is called very early, before site-packages are enabled. Because of this, using a non-standard 'category' will almost certainly fail with the message:
The -W option would be a lot more useful if it could actually match non-standard categories (it does, after all, pretend to support modulename.classname).
I don't see any easy way of fixing this, other than initializing the warnings module later or matching category names with the given string (and getting rid of the import).
Doing the initialization later is not really an option as there can be warnings in the parser so it has to be resolved very early on.
I guess some form could be a possibility, but that's also tricky as subclass matching also still needs to work. I think we would need to see a patch on the proposed solution before considering its acceptance.
Another option would to parse it twice: once early on for Python internals and then again later once site-packages and various other things have been set up. Once again I would need to look at a patch before making a decision on whether it's an acceptable solution.
Bluehorn commented on Sep 14, 2016
Bluehornmannequin
Mannequin
More actions
Wow, this was news to me and I just ran into it in python 2.7. Checked in Python 3 and it's still there:
I see no easy way to fix this. One way I thought about is to have hooks for importing of modules (called when a module is put into sys.modules, not sure if there is something like this already) and have the warning system trigger on that.
There is no way to raise an exception before it is imported anyway...
gerritholl commented on Nov 29, 2018
gerrithollmannequin
Mannequin
More actions
There is a thread on StackOverflow related to this problem:
The (currently only and accepted) answer proposes as a workaround to set the PYTHONPATH variable, as the contents of those apparently *are* already in sys.path at the time warnings._processoptions is called, unlike site-packages. So manually adding site-packages to the PYTHONPATH variable does circumvent this problem (tested in Python 3.7.1).
Importing the named module during warning initialization is somewhat like code injection. And it may introduce unintended side-effects -- for example if this causes some 3rd party libraries to initialize too early?
String matching on the class name of every class in the hierarchy is safer, as there is no 'expected' code execution.
Reacted by Jared Lumpe
mmerickel commented on Mar 11, 2022
mmerickelmannequin
Mannequin
More actions
Updated affected versions as I ran into this on 3.9.7.
Any plans to fix this? It seems like string matching on the warning class' __module__ and __name__ attributes as suggested by @fengyu is a much better solution and very easy to implement.
Activity
remram44 commentedon Oct 2, 2014
warnings._processoptions is called very early, before site-packages are enabled. Because of this, using a non-standard 'category' will almost certainly fail with the message:
The -W option would be a lot more useful if it could actually match non-standard categories (it does, after all, pretend to support modulename.classname).
I don't see any easy way of fixing this, other than initializing the warnings module later or matching category names with the given string (and getting rid of the import).
pitrou commentedon Oct 3, 2014
How would this work? Would it auto-import the module?
remram44 commentedon Oct 3, 2014
It already does auto-import, but it does it before site-packages are set up, meaning that it fails in any practical setup.
See _getcategory(), called by _processoptions(): https://hg.python.org/cpython/file/b15c5a66213f/Lib/warnings.py#l148
brettcannon commentedon Oct 3, 2014
Doing the initialization later is not really an option as there can be warnings in the parser so it has to be resolved very early on.
I guess some form could be a possibility, but that's also tricky as subclass matching also still needs to work. I think we would need to see a patch on the proposed solution before considering its acceptance.
Another option would to parse it twice: once early on for Python internals and then again later once site-packages and various other things have been set up. Once again I would need to look at a patch before making a decision on whether it's an acceptable solution.
Bluehorn commentedon Sep 14, 2016
Wow, this was news to me and I just ran into it in python 2.7. Checked in Python 3 and it's still there:
I see no easy way to fix this. One way I thought about is to have hooks for importing of modules (called when a module is put into
sys.modules, not sure if there is something like this already) and have the warning system trigger on that.There is no way to raise an exception before it is imported anyway...
gerritholl commentedon Nov 29, 2018
There is a thread on StackOverflow related to this problem:
https://stackoverflow.com/q/42495641/974555
The (currently only and accepted) answer proposes as a workaround to set the PYTHONPATH variable, as the contents of those apparently *are* already in sys.path at the time warnings._processoptions is called, unlike site-packages. So manually adding site-packages to the PYTHONPATH variable does circumvent this problem (tested in Python 3.7.1).
FengYu commentedon Jan 5, 2020
Coming here from astropy/astropy#9832 (comment)
Importing the named module during warning initialization is somewhat like code injection. And it may introduce unintended side-effects -- for example if this causes some 3rd party libraries to initialize too early?
String matching on the class name of every class in the hierarchy is safer, as there is no 'expected' code execution.
mmerickel commentedon Mar 11, 2022
Updated affected versions as I ran into this on 3.9.7.
astrojuanlu commentedon Jul 4, 2023
This is still an issue in Python 3.11.
jlumpe commentedon Jan 30, 2024
Any plans to fix this? It seems like string matching on the warning class'
__module__and__name__attributes as suggested by @fengyu is a much better solution and very easy to implement.