[issue9636] {'key': 'value'}[b'key'] raises a BytesWarning

STINNER Victor report at bugs.python.org
Thu Aug 19 02:47:27 CEST 2010


New submission from STINNER Victor <victor.stinner at haypocalc.com>:

With python3 -bb: {'key': 'value'}[b'key'] raises a BytesWarning, but {'key': 'value'}[b'missing_key'] doesn't. The warning is unexpected here because it's an implicit comparaison (I mean, different than an explicit: 'key' == b'key'), we cannot check that the dict keys are all bytes / unicode (at least, I don't want to). And so I think that it should be fixed.

First lookdict_unicode() is used because all dict keys are unicode, but lookdict_unicode() falls back to lookdict() because the asked key type is not unicode.

lookdict() checks the hash: they matches, hash('key') == hash(b'key'). Then it compares the two key objects with PyObject_RichCompareBool(startkey, key, Py_EQ). PyUnicode_RichCompare() returns NotImplemented, and so bytes_richcompare() is called. Finally, bytes_richcompare() raises the BytesWarning.

----------
components: Interpreter Core, Unicode
messages: 114314
nosy: haypo
priority: normal
severity: normal
status: open
title: {'key': 'value'}[b'key'] raises a BytesWarning
versions: Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9636>
_______________________________________


More information about the Python-bugs-list mailing list