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.

Title: Extend Shelf to support unicode keys
Type: enhancement Stage: test needed
Components: Library (Lib), Unicode Versions:
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, goodger, loewis, mok0, tim.peters
Priority: normal Keywords:

Created on 2004-09-28 19:50 by mok0, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg54267 - (view) Author: Morten Kjeldgaard (mok0) Date: 2004-09-28 19:50
Lots of modules return unicode strings, for example the
xml parser. There is no reason why you shouldn't be
able to use a unicode string as key in a dictionary,
shelve or database lookup. I think this is more a bug
than a feature, since having to make sure what type
your keys are makes the code bloated and ugly.

msg54268 - (view) Author: David Goodger (goodger) (Python committer) Date: 2004-09-28 20:35
Logged In: YES 

Unicode strings work as dictionary keys for me, in both
Python 2.3.3 and 2.4a3.  Are you sure of your assertion? 
Please provide some evidence.
msg54269 - (view) Author: Morten Kjeldgaard (mok0) Date: 2004-09-28 20:58
Logged In: YES 

You are right, unicode strings work in dictionaries. I
should have checked, sorry. However, they don't work in
shelves, as the attached script will demonstrate. You get a
type error:

Traceback (most recent call last):
  File "", line 7, in ?
    x[a] = "arrrghhh"
  File "/usr/lib/python2.3/", line 130, in __setitem__
    self.dict[key] = f.getvalue()
  File "/usr/lib/python2.3/site-packages/", line
120, in __setitem__

TypeError: String or Integer object expected for key,
unicode found

I am using Python 2.3.3 on Linux
msg54270 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-09-28 21:48
Logged In: YES 

Moved to the Feature Request tracker, and changed the 
summary to something more accurate.

The restriction to string keys is documented, and the code is 
functioning as documented.  The restriction is hard to lift 
because the underlying database technologies (gdbm, bsddb) 
require string keys.

I suppose Shelf could encode all incoming keys via utf-8, in 
every method taking a key argument, and decode again in all 
ways of getting keys out, but that wouldn't be backward 

Unassigned Marc-Andre since I doubt he's going to do 
something about this.
msg54271 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-09-28 21:53
Logged In: YES 

Reset the Category to Unicode; SF lost it on the way to the 
Feature tracker.
msg54272 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2004-11-13 11:26
Logged In: YES 

It would be possible to add a unicode argument to the shelve
constructors; this would .encode all keys being passed in,
and .decode all keys being passed out. Canonically, keys
should be encoded as UTF-8.
msg82070 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-14 14:42
Is this one still valid? Test script is missing.
msg82080 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2009-02-14 15:18
It's fixed in 3.0, by means of the keyencoding argument to Shelf. It
would be possible to port that back to 2.7; since no patch had been
forthcoming, I'm closing this as fixed, anyway.
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 40960
2009-02-14 15:18:35loewissetstatus: open -> closed
resolution: fixed
messages: + msg82080
2009-02-14 14:42:56ajaksu2setnosy: + ajaksu2
messages: + msg82070
components: + Library (Lib)
stage: test needed
2004-09-28 19:50:09mok0create