New submission from Xavier Morel <xavier.mo...@masklinn.net>:

When setting `BytesWarning` programmatically (via the warnings API), though the 
`warnings.filters` value matches what's obtained via `python -b` and an 
explicit `warnings.warn` trigger will trigger, "native" triggers of the warning 
fail to trigger properly:

    import warnings
    warnings.simplefilter('default', category=BytesWarning)
    str(b'')
    warnings.warn('test', category=BytesWarning)

If run using `python`, this will print:

    test.py:4: BytesWarning: test
      warnings.warn('test', category=BytesWarning)

There is no warning for the string-ification of the bytes instance.

If run using `python -b`, the behaviour is as one would expect:

    test.py:3: BytesWarning: str() on a bytes instance
      str(b'')
    test.py:4: BytesWarning: test
      warnings.warn('test', category=BytesWarning)

Inspecting `warnings.filters` shows now difference in their contents, in both 
cases it is:

    [('default', None, <class 'BytesWarning'>, 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 'ResourceWarning'>, None, 
0)]

(in Python 3.9).

The warning module's own suggestion:

    import sys
    if not sys.warnoptions:
        import warnings
        warnings.simplefilter("default") # Change the filter in this process

also fails to enable BytesWarning.

If this is intended behaviour, which seems to be the case according to 
ncoghlan's comment https://bugs.python.org/issue32230#msg307721, it should be 
clearly documented, as it's rather frustrating.

----------
components: Library (Lib)
messages: 388912
nosy: xmorel
priority: normal
severity: normal
status: open
title: Programmatic management of BytesWarning doesn't work for native triggers.
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43526>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to