From f4ec4be807450c1d2cc9e33ea44d5c7120887438 Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Wed, 30 May 2012 09:56:03 -0700 Subject: [PATCH] Avoid encoding errors with pickle.loads PYTHON-355. This works around issues caused by applications that use sys.setdefaultencoding to set an interpreter-wide encoding other than ascii or utf8. This is only an issue in python 2.x. In python 3.0 and 3.1 setdefaultencoding is a noop. It was completely removed in python 3.2. --- bson/objectid.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/bson/objectid.py b/bson/objectid.py index 78d1e6853..ee1364ea0 100644 --- a/bson/objectid.py +++ b/bson/objectid.py @@ -32,7 +32,7 @@ import threading import time from bson.errors import InvalidId -from bson.py3compat import (b, binary_type, text_type, +from bson.py3compat import (PY3, b, binary_type, text_type, bytes_from_hex, string_types) from bson.tz_util import utc @@ -208,21 +208,18 @@ class ObjectId(object): """explicit state set from pickling """ # Provide backwards compatability with OIDs - # pickled with pymongo-1.9. + # pickled with pymongo-1.9 or older. if isinstance(value, dict): - try: - # ObjectIds pickled in python 2.x used `str` for __id. - # In python 3.x this has to be converted to `bytes` - # by encoding latin-1. - self.__id = value['_ObjectId__id'].encode('latin-1') - except UnicodeDecodeError: - self.__id = value['_ObjectId__id'] + oid = value["_ObjectId__id"] else: - try: - # See the previous comment about python 2/3 pickle issues. - self.__id = value.encode('latin-1') - except (UnicodeDecodeError, AttributeError): - self.__id = value + oid = value + # ObjectIds pickled in python 2.x used `str` for __id. + # In python 3.x this has to be converted to `bytes` + # by encoding latin-1. + if PY3 and isinstance(oid, text_type): + self.__id = oid.encode('latin-1') + else: + self.__id = oid def __str__(self): return binascii.hexlify(self.__id).decode()