classification
Title: TypeError: gdbm key must be string, not unicode
Type: behavior Stage:
Components: Unicode Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, r.david.murray, sam-s, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2017-09-08 20:54 by sam-s, last changed 2017-09-09 19:46 by serhiy.storchaka.

Messages (5)
msg301728 - (view) Author: sds (sam-s) Date: 2017-09-08 20:54
`in` and `has_key` have different behavior for Unicode keys for `gdbm` in 2.7:
```
>>> import gdbm
>>> db = gdbm.open("foo.gdbm","c")
>>> db.has_key("a")
0
>>> db.has_key(u"a")
0
>>> "a" in db
False
>>> u"a" in db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: gdbm key must be string, not unicode
```
msg301729 - (view) Author: sds (sam-s) Date: 2017-09-08 20:56
platform:
Python 2.7.13 (default, Jul 18 2017, 09:17:00) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
msg301730 - (view) Author: sds (sam-s) Date: 2017-09-08 21:00
the problem is not present in 
Python 3.6.2 (default, Jul 17 2017, 16:44:45):
```
>>> import dbm
>>> import dbm.gnu
>>> db = dbm.gnu.open("foo","c")
>>> "a" in db
False
>>> u"a" in db
False
```
msg301781 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-09-09 17:26
In python3, u"a" and "a" are the same thing.  The equivalent in python3 would bee b"a" vs "a", but I have no idea if we even support bytes keys in python3 gdbm.

In 2.7 does has_key(u"x") work if x is a valid key?
msg301788 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-09 19:46
has_key(u"x") works if x is a valid key.

has_key() parses the argument with PyArg_ParseTuple("s#") which implicitly converts unicode to str. __contains__() explicitly checks for str type.
History
Date User Action Args
2017-09-09 19:46:39serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg301788
2017-09-09 17:26:11r.david.murraysetnosy: + r.david.murray
messages: + msg301781
2017-09-08 21:00:21sam-ssetmessages: + msg301730
2017-09-08 20:56:36sam-ssetmessages: + msg301729
2017-09-08 20:54:57sam-screate