This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author xmorel
Recipients xmorel
Date 2021-03-17.09:03:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1615971785.71.0.488992120219.issue43526@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-03-17 09:03:05xmorelsetrecipients: + xmorel
2021-03-17 09:03:05xmorelsetmessageid: <1615971785.71.0.488992120219.issue43526@roundup.psfhosted.org>
2021-03-17 09:03:05xmorellinkissue43526 messages
2021-03-17 09:03:04xmorelcreate