From 7fb125cc1b4a3ddee18d673aacf6a13906541a38 Mon Sep 17 00:00:00 2001 From: Reorx Date: Sat, 2 Feb 2013 11:53:34 +0800 Subject: [PATCH] `__str__` method of ObjectId return MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As Python supports both `__str__` and `__unicode__` method for objects' string conversion, it is suggested to return `` for `__str__`, and `` for `__unicode__`. This is my understanding about the difference between this two methods. So I think it would be better to return str explicitly for `__str__`, and add `__unicode__` for possible use I have encountered a problem when I formatting strings with ObjectId: ```python >>> '%s %s' % ('\xe5\x90\x8d\xe5\xad\x97', ObjectId()) Traceback (most recent call last):   File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) ``` The reason why this problem happen is because Python tried to encode every argument since he found an unicode in the arguments, but the first one has already been encoded as utf8, the re-encode action then cause error. --- bson/objectid.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bson/objectid.py b/bson/objectid.py index d21fd837fd..537dae9541 100644 --- a/bson/objectid.py +++ b/bson/objectid.py @@ -244,7 +244,10 @@ def __setstate__(self, value): self.__id = oid def __str__(self): - return binascii.hexlify(self.__id).decode() + return binascii.hexlify(self.__id) + + def __unicode__(self): + return self.__str__().decode() def __repr__(self): return "ObjectId('%s')" % (str(self),)