@@ -239,10 +239,7 @@ def _get_regex(data, position, dummy, opts):
239239 pattern , position = _get_c_string (data , position )
240240 bson_flags , position = _get_c_string (data , position )
241241 bson_re = Regex (pattern , bson_flags )
242- if opts [3 ]:
243- return bson_re .try_compile (), position
244- else :
245- return bson_re , position
242+ return bson_re , position
246243
247244
248245def _get_ref (data , position , obj_end , opts ):
@@ -308,9 +305,9 @@ def _elements_to_dict(data, position, obj_end, opts):
308305 return result
309306
310307
311- def _bson_to_dict (data , as_class , tz_aware , uuid_subtype , compile_re ):
308+ def _bson_to_dict (data , as_class , tz_aware , uuid_subtype ):
312309 """Decode a BSON string to as_class."""
313- opts = (as_class , tz_aware , uuid_subtype , compile_re )
310+ opts = (as_class , tz_aware , uuid_subtype )
314311 try :
315312 obj_size = _UNPACK_INT (data [:4 ])[0 ]
316313 except struct .error as e :
@@ -700,7 +697,7 @@ def _dict_to_bson(doc, check_keys, uuid_subtype, top_level=True):
700697
701698
702699def decode_all (data , as_class = dict ,
703- tz_aware = True , uuid_subtype = OLD_UUID_SUBTYPE , compile_re = True ):
700+ tz_aware = True , uuid_subtype = OLD_UUID_SUBTYPE ):
704701 """Decode BSON data to multiple documents.
705702
706703 `data` must be a string of concatenated, valid, BSON-encoded
@@ -714,16 +711,23 @@ def decode_all(data, as_class=dict,
714711 :class:`~datetime.datetime` instances
715712 - `uuid_subtype` (optional): The BSON representation to use for UUIDs.
716713 See the :mod:`bson.binary` module for all options.
717- - `compile_re` (optional): if ``False``, don't attempt to compile
718- BSON regular expressions into Python regular expressions. Return
719- instances of :class:`~bson.regex.Regex` instead. Can avoid
720- :exc:`~bson.errors.InvalidBSON` errors when receiving
721- Python-incompatible regular expressions, for example from ``currentOp``
714+
715+ .. versionchanged:: 3.0
716+ Removed `compile_re` option: PyMongo now always represents BSON regular
717+ expressions as :class:`~bson.regex.Regex` objects. Use
718+ :meth:`~bson.regex.Regex.try_compile` to attempt to convert from a
719+ BSON regular expression to a Python regular expression object.
722720
723721 .. versionchanged:: 2.7
724- Added `compile_re` option.
722+ Added `compile_re` option. If set to False, PyMongo represented BSON
723+ regular expressions as :class:`~bson.regex.Regex` objects instead of
724+ attempting to compile BSON regular expressions as Python native
725+ regular expressions, thus preventing errors for some incompatible
726+ patterns, see `PYTHON-500`_.
727+
728+ .. _PYTHON-500: https://jira.mongodb.org/browse/PYTHON-500
725729 """
726- opts = (as_class , tz_aware , uuid_subtype , compile_re )
730+ opts = (as_class , tz_aware , uuid_subtype )
727731 docs = []
728732 position = 0
729733 end = len (data ) - 1
@@ -751,7 +755,7 @@ def decode_all(data, as_class=dict,
751755
752756
753757def decode_iter (data , as_class = dict , tz_aware = True ,
754- uuid_subtype = OLD_UUID_SUBTYPE , compile_re = True ):
758+ uuid_subtype = OLD_UUID_SUBTYPE ):
755759 """Decode BSON data to multiple documents as a generator.
756760
757761 Works similarly to the decode_all function, but yields one document at a
@@ -768,13 +772,6 @@ def decode_iter(data, as_class=dict, tz_aware=True,
768772 :class:`~datetime.datetime` instances
769773 - `uuid_subtype` (optional): The BSON representation to use for UUIDs.
770774 See the :mod:`bson.binary` module for all options.
771- - `compile_re` (optional): if ``False``, don't attempt to compile
772- BSON regular expressions into Python regular expressions. Return
773- instances of
774- :class:`~bson.regex.Regex` instead. Can avoid
775- :exc:`~bson.errors.InvalidBSON` errors when receiving
776- Python-incompatible regular expressions, for example from
777- ``currentOp``
778775
779776 .. versionadded:: 2.8
780777 """
@@ -786,11 +783,11 @@ def decode_iter(data, as_class=dict, tz_aware=True,
786783 position += obj_size
787784
788785 yield _bson_to_dict (elements , as_class ,
789- tz_aware , uuid_subtype , compile_re )
786+ tz_aware , uuid_subtype )
790787
791788
792789def decode_file_iter (file_obj , as_class = dict , tz_aware = True ,
793- uuid_subtype = OLD_UUID_SUBTYPE , compile_re = True ):
790+ uuid_subtype = OLD_UUID_SUBTYPE ):
794791 """Decode bson data from a file to multiple documents as a generator.
795792
796793 Works similarly to the decode_all function, but reads from the file object
@@ -804,13 +801,6 @@ def decode_file_iter(file_obj, as_class=dict, tz_aware=True,
804801 :class:`~datetime.datetime` instances
805802 - `uuid_subtype` (optional): The BSON representation to use for UUIDs.
806803 See the :mod:`bson.binary` module for all options.
807- - `compile_re` (optional): if ``False``, don't attempt to compile
808- BSON regular expressions into Python regular expressions. Return
809- instances of
810- :class:`~bson.regex.Regex` instead. Can avoid
811- :exc:`~bson.errors.InvalidBSON` errors when receiving
812- Python-incompatible regular expressions, for example from
813- ``currentOp``
814804
815805 .. versionadded:: 2.8
816806 """
@@ -824,7 +814,7 @@ def decode_file_iter(file_obj, as_class=dict, tz_aware=True,
824814 obj_size = _UNPACK_INT (size_data )[0 ] - 4
825815 elements = size_data + file_obj .read (obj_size )
826816 yield _bson_to_dict (elements , as_class ,
827- tz_aware , uuid_subtype , compile_re )
817+ tz_aware , uuid_subtype )
828818
829819
830820def is_valid (bson ):
@@ -841,7 +831,7 @@ def is_valid(bson):
841831 raise TypeError ("BSON data must be an instance of a subclass of bytes" )
842832
843833 try :
844- _bson_to_dict (bson , dict , True , OLD_UUID_SUBTYPE , True )
834+ _bson_to_dict (bson , dict , True , OLD_UUID_SUBTYPE )
845835 return True
846836 except Exception :
847837 return False
@@ -874,7 +864,7 @@ def encode(cls, document, check_keys=False, uuid_subtype=OLD_UUID_SUBTYPE):
874864 return cls (_dict_to_bson (document , check_keys , uuid_subtype ))
875865
876866 def decode (self , as_class = dict ,
877- tz_aware = False , uuid_subtype = OLD_UUID_SUBTYPE , compile_re = True ):
867+ tz_aware = False , uuid_subtype = OLD_UUID_SUBTYPE ):
878868 """Decode this BSON data.
879869
880870 The default type to use for the resultant document is
@@ -896,19 +886,23 @@ def decode(self, as_class=dict,
896886 :class:`~datetime.datetime` instances
897887 - `uuid_subtype` (optional): The BSON representation to use for
898888 UUIDs. See the :mod:`bson.binary` module for all options.
899- - `compile_re` (optional): if ``False``, don't attempt to compile
900- BSON regular expressions into Python regular expressions. Return
901- instances of
902- :class:`~bson.regex.Regex` instead. Can avoid
903- :exc:`~bson.errors.InvalidBSON` errors when receiving
904- Python-incompatible regular expressions, for example from
905- ``currentOp``
889+
890+ .. versionchanged:: 3.0
891+ Removed `compile_re` option: PyMongo now always represents BSON
892+ regular expressions as :class:`~bson.regex.Regex` objects. Use
893+ :meth:`~bson.regex.Regex.try_compile` to attempt to convert from a
894+ BSON regular expression to a Python regular expression object.
906895
907896 .. versionchanged:: 2.7
908- Added ``compile_re`` option.
897+ Added `compile_re` option. If set to False, PyMongo represented BSON
898+ regular expressions as :class:`~bson.regex.Regex` objects instead of
899+ attempting to compile BSON regular expressions as Python native
900+ regular expressions, thus preventing errors for some incompatible
901+ patterns, see `PYTHON-500`_.
902+
903+ .. _PYTHON-500: https://jira.mongodb.org/browse/PYTHON-500
909904 """
910- return _bson_to_dict (
911- self , as_class , tz_aware , uuid_subtype , compile_re )
905+ return _bson_to_dict (self , as_class , tz_aware , uuid_subtype )
912906
913907
914908def has_c ():
0 commit comments