diff --git a/bson/codec_options.py b/bson/codec_options.py index 0db900f59..8b8482905 100644 --- a/bson/codec_options.py +++ b/bson/codec_options.py @@ -295,6 +295,17 @@ class CodecOptions(_options_base): self.unicode_decode_error_handler, self.tzinfo, self.type_registry)) + def _options_dict(self): + """Dictionary of the arguments used to create this object.""" + # TODO: PYTHON-2442 use _asdict() instead + return { + 'document_class': self.document_class, + 'tz_aware': self.tz_aware, + 'uuid_representation': self.uuid_representation, + 'unicode_decode_error_handler': self.unicode_decode_error_handler, + 'tzinfo': self.tzinfo, + 'type_registry': self.type_registry} + def __repr__(self): return '%s(%s)' % (self.__class__.__name__, self._arguments_repr()) @@ -310,7 +321,7 @@ class CodecOptions(_options_base): .. versionadded:: 3.5 """ - opts = self._asdict() + opts = self._options_dict() opts.update(kwargs) return CodecOptions(**opts) diff --git a/bson/json_util.py b/bson/json_util.py index 1eef9270e..38c39a12c 100644 --- a/bson/json_util.py +++ b/bson/json_util.py @@ -311,6 +311,16 @@ class JSONOptions(CodecOptions): self.json_mode, super(JSONOptions, self)._arguments_repr())) + def _options_dict(self): + # TODO: PYTHON-2442 use _asdict() instead + options_dict = super(JSONOptions, self)._options_dict() + options_dict.update({ + 'strict_number_long': self.strict_number_long, + 'datetime_representation': self.datetime_representation, + 'strict_uuid': self.strict_uuid, + 'json_mode': self.json_mode}) + return options_dict + def with_options(self, **kwargs): """ Make a copy of this JSONOptions, overriding some options:: @@ -324,7 +334,7 @@ class JSONOptions(CodecOptions): .. versionadded:: 3.12 """ - opts = self._asdict() + opts = self._options_dict() for opt in ('strict_number_long', 'datetime_representation', 'strict_uuid', 'json_mode'): opts[opt] = kwargs.get(opt, getattr(self, opt)) diff --git a/test/test_client.py b/test/test_client.py index 04831a71c..f02a075b3 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -280,6 +280,9 @@ class ClientUnitTest(unittest.TestCase): readpreference=ReadPreference.NEAREST.mongos_mode) self.assertEqual(c.read_preference, ReadPreference.NEAREST) + @unittest.skipIf( + sys.version_info[0] == 3 and sys.version_info[1] == 4, + "PYTHON-2442: workaround namedtuple._asdict() bug on Python 3.4") def test_metadata(self): metadata = copy.deepcopy(_METADATA) metadata['application'] = {'name': 'foobar'}