@@ -896,7 +896,6 @@ def _encode_maxkey(name: bytes, dummy0: Any, dummy1: Any, dummy2: Any) -> bytes:
896
896
str : _encode_text ,
897
897
tuple : _encode_list ,
898
898
type (None ): _encode_none ,
899
- decimal .Decimal : _encode_python_decimal ,
900
899
uuid .UUID : _encode_uuid ,
901
900
Binary : _encode_binary ,
902
901
Int64 : _encode_long ,
@@ -920,8 +919,7 @@ def _encode_maxkey(name: bytes, dummy0: Any, dummy1: Any, dummy2: Any) -> bytes:
920
919
if hasattr (_typ , "_type_marker" ):
921
920
_MARKERS [_typ ._type_marker ] = _ENCODERS [_typ ]
922
921
923
- # Exclude decimal.Decimal since auto-conversion is explicitly opt-in.
924
- _BUILT_IN_TYPES = tuple (t for t in _ENCODERS if t != decimal .Decimal )
922
+ _BUILT_IN_TYPES = tuple (t for t in _ENCODERS )
925
923
926
924
927
925
def _name_value_to_bson (
@@ -949,6 +947,9 @@ def _name_value_to_bson(
949
947
# Give the fallback_encoder a chance
950
948
was_integer_overflow = True
951
949
950
+ if opts .convert_decimal and type (value ) == decimal .Decimal :
951
+ return _encode_python_decimal (name , value , check_keys , opts ) # type: ignore
952
+
952
953
# Second, fall back to trying _type_marker. This has to be done
953
954
# before the loop below since users could subclass one of our
954
955
# custom types that subclasses a python built-in (e.g. Binary)
@@ -1029,8 +1030,8 @@ def _dict_to_bson(
1029
1030
return _PACK_INT (len (encoded ) + 5 ) + encoded + b"\x00 "
1030
1031
1031
1032
1032
- if _USE_C :
1033
- _dict_to_bson = _cbson ._dict_to_bson
1033
+ # if _USE_C:
1034
+ # _dict_to_bson = _cbson._dict_to_bson
1034
1035
1035
1036
1036
1037
_CODEC_OPTIONS_TYPE_ERROR = TypeError ("codec_options must be an instance of CodecOptions" )
0 commit comments