Skip to content

Commit 02fa596

Browse files
committed
Move _encode_python_decimal out of _ENCODERS
1 parent 4fcd939 commit 02fa596

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

bson/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,6 @@ def _encode_maxkey(name: bytes, dummy0: Any, dummy1: Any, dummy2: Any) -> bytes:
896896
str: _encode_text,
897897
tuple: _encode_list,
898898
type(None): _encode_none,
899-
decimal.Decimal: _encode_python_decimal,
900899
uuid.UUID: _encode_uuid,
901900
Binary: _encode_binary,
902901
Int64: _encode_long,
@@ -920,8 +919,7 @@ def _encode_maxkey(name: bytes, dummy0: Any, dummy1: Any, dummy2: Any) -> bytes:
920919
if hasattr(_typ, "_type_marker"):
921920
_MARKERS[_typ._type_marker] = _ENCODERS[_typ]
922921

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)
925923

926924

927925
def _name_value_to_bson(
@@ -949,6 +947,9 @@ def _name_value_to_bson(
949947
# Give the fallback_encoder a chance
950948
was_integer_overflow = True
951949

950+
if opts.convert_decimal and type(value) == decimal.Decimal:
951+
return _encode_python_decimal(name, value, check_keys, opts) # type: ignore
952+
952953
# Second, fall back to trying _type_marker. This has to be done
953954
# before the loop below since users could subclass one of our
954955
# custom types that subclasses a python built-in (e.g. Binary)
@@ -1029,8 +1030,8 @@ def _dict_to_bson(
10291030
return _PACK_INT(len(encoded) + 5) + encoded + b"\x00"
10301031

10311032

1032-
if _USE_C:
1033-
_dict_to_bson = _cbson._dict_to_bson
1033+
# if _USE_C:
1034+
# _dict_to_bson = _cbson._dict_to_bson
10341035

10351036

10361037
_CODEC_OPTIONS_TYPE_ERROR = TypeError("codec_options must be an instance of CodecOptions")

0 commit comments

Comments
 (0)