[Python-Dev] PEP 565: Show DeprecationWarning in __main__

Guido van Rossum guido at python.org
Tue Dec 12 15:21:32 EST 2017


Nick and Victor,

I'm still hoping to accept this PEP, but I don't have time to wrap my head
around -Xdev ("devmode"?) which appears to be Victor's latest pet project.
Should PEP 565 be changed to copy with devmode's behavior, or the other way
around, or should they just ignore each other? It is not clear of me what
the status of the mention in PEP 565 of -Xdev is -- normative or
informational? I really don't want to have to learn how devmode works in
order to be able to accept PEP 565 (or send it back for revision), so I am
asking you two to let me know.

On Wed, Dec 6, 2017 at 1:42 AM, Victor Stinner <victor.stinner at gmail.com>
wrote:

> Let's discuss -Xdev implementation issue at https://bugs.python.org/
> issue32230
>
> In short, -Xdev must add its warning at the end to respect BytesWarning,
> whereas it's not possible with -W option :-(
>
> Victor
>
> Le 6 déc. 2017 09:15, "Nick Coghlan" <ncoghlan at gmail.com> a écrit :
>
> On 6 December 2017 at 14:50, Nick Coghlan <ncoghlan at gmail.com> wrote:
> > On 6 December 2017 at 14:34, Nick Coghlan <ncoghlan at gmail.com> wrote:
> >> That said, I go agree we could offer easier to use APIs to app
> >> developers that just want to hide warnings from their users, so I've
> >> filed https://bugs.python.org/issue32229 to propose a straightforward
> >> "warnings.hide_warnings()" API that encapsulates things like checking
> >> for a non-empty sys.warnoptions list.
> >
> > I've updated the "Limitations" section of the PEP to mention that
> > separate proposal:
> > https://github.com/python/peps/commit/6e93c8d2e6ad698834578d
> 4077b92a8fc84a70f5
>
> Having rebased the PEP 565 patch atop the "-X dev" changes, I think
> that if we don't change some of the details of how `-X dev` is
> implemented, `warnings.hide_warnings` (or a comparable convenience
> API) is going to be a requirement to help app developers effectively
> manage their default warnings settings in 3.7+.
>
> The problem is that devmode doesn't currently behave the same way
> `-Wd` does when it comes to sys.warnoptions:
>
>     $ ./python -Wd -c "import sys; print(sys.warnoptions);
> print(sys.flags.dev_mode)"
>     ['d']
>     False
>     $ ./python -X dev -c "import sys; print(sys.warnoptions);
> print(sys.flags.dev_mode)"
>     []
>     True
>
> As currently implemented, the warnings module actually checks
> `sys.flags.dev_mode` directly during startup (or `sys._xoptions` in
> the case of the pure Python fallback), and populates the warnings
> filter differently depending on what it finds:
>
>     $ ./python -c "import warnings; print('\n'.join(map(str,
> warnings.filters)))"
>     ('default', None, <class 'DeprecationWarning'>, '__main__', 0)
>     ('ignore', None, <class 'DeprecationWarning'>, None, 0)
>     ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0)
>     ('ignore', None, <class 'ImportWarning'>, None, 0)
>     ('ignore', None, <class 'BytesWarning'>, None, 0)
>     ('ignore', None, <class 'ResourceWarning'>, None, 0)
>
>     $ ./python -X dev -c "import warnings; print('\n'.join(map(str,
> warnings.filters)))"
>     ('ignore', None, <class 'BytesWarning'>, None, 0)
>     ('default', None, <class 'ResourceWarning'>, None, 0)
>     ('default', None, <class 'Warning'>, None, 0)
>
>     $ ./python -Wd -c "import warnings; print('\n'.join(map(str,
> warnings.filters)))"
>     ('default', None, <class 'Warning'>, None, 0)
>     ('default', None, <class 'DeprecationWarning'>, '__main__', 0)
>     ('ignore', None, <class 'DeprecationWarning'>, None, 0)
>     ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0)
>     ('ignore', None, <class 'ImportWarning'>, None, 0)
>     ('ignore', None, <class 'BytesWarning'>, None, 0)
>     ('ignore', None, <class 'ResourceWarning'>, None, 0)
>
> This means the app development snippet proposed in the PEP will no
> longer do the right thing, since it will ignore the dev mode flag:
>
>     if not sys.warnoptions:
>         # This still runs for `-X dev`
>         warnings.simplefilter("ignore")
>
> My main suggested fix would be to adjust the way `-X dev` is
> implemented to include `sys.warnoptions.append('default')` (and remove
> the direct dev_mode query from the warnings module code).
>
> However, another possible way to go would be to make the correct
> Python 3.7+-only snippet look like this:
>
>     import warnings
>     warnings.hide_warnings()
>
> And have the forward-compatible snippet look like this:
>
>     import warnings:
>     if hasattr(warnings, "hide_warnings"):
>         # Accounts for `-W`, `-X dev`, and any other implementation
> specific settings
>         warnings.hide_warnings()
>     else:
>         # Only accounts for `-W`
>         import sys
>         if not sys.warnoptions:
>             warnings.simplefilter("ignore")
>
> (We can also do both, of course)
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> guido%40python.org
>
>


-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171212/4824406b/attachment.html>


More information about the Python-Dev mailing list