Message388912
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. |
|
Date |
User |
Action |
Args |
2021-03-17 09:03:05 | xmorel | set | recipients:
+ xmorel |
2021-03-17 09:03:05 | xmorel | set | messageid: <1615971785.71.0.488992120219.issue43526@roundup.psfhosted.org> |
2021-03-17 09:03:05 | xmorel | link | issue43526 messages |
2021-03-17 09:03:04 | xmorel | create | |
|