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.

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

Created on 2017-09-08 20:54 by sam-s, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (7)
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.
msg396072 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 15:47
This seems like a python 2-only issue, if nobody objects I will close it in a couple of weeks.
msg396074 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-18 16:05
IMO you can close it immediately. Fixing Python 2.7 is not going to happen ever.
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75579
2021-06-18 16:23:29iritkatrielsetstatus: open -> closed
stage: resolved
2021-06-18 16:05:51vstinnersetstatus: pending -> open

messages: + msg396074
2021-06-18 15:47:46iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg396072

resolution: out of date
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