Message376836
Here is the inconsistent behavior, when running with `python -bb` (or just `python -b`), caused by an internal cache:
>>> import struct
>>> struct.calcsize(b'!d') # cache for '!d' uses bytes
8
>>> struct.calcsize('!d') # so there's a warning when trying to use str
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and string
>>> struct.calcsize('>d') # cache for '>d' uses str
8
>>> struct.calcsize(b'>d') # so now the warning is inverted, it shows up when trying to use bytes
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and string
>>> struct.calcsize('>d') # no problem when using str
8
Note that this might be caused by a possible larger problem when dealing with keys of different string types in dicts under `python -b` (or `python -bb`):
$ python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d['a']=1
>>> d[b'a']=2
>>> d['a']
1
>>> d[b'a']
2
$ python -bb
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d['a']=1
>>> d[b'a']=2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
I'm not sure if this warning is intentional, since in Python 3 there seems to be no special reason for dicts to try to compare 'a' with b'a' (other than possible implementation details).
Note: this is from an issue found here: https://github.com/pytest-dev/pytest-xdist/issues/596 |
|
Date |
User |
Action |
Args |
2020-09-13 13:23:48 | tadeu | set | recipients:
+ tadeu |
2020-09-13 13:23:48 | tadeu | set | messageid: <1600003428.26.0.120292470084.issue41777@roundup.psfhosted.org> |
2020-09-13 13:23:48 | tadeu | link | issue41777 messages |
2020-09-13 13:23:47 | tadeu | create | |
|