@@ -163,6 +163,10 @@ def __init__(self, database, name, create=False, codec_options=None,
163163 if create or kwargs :
164164 self .__create (kwargs )
165165
166+ self .__write_response_codec_options = self .codec_options ._replace (
167+ unicode_decode_error_handler = 'replace' ,
168+ document_class = dict )
169+
166170 def _socket_for_reads (self ):
167171 return self .__database .client ._socket_for_reads (self .read_preference )
168172
@@ -506,17 +510,19 @@ def _insert_one(
506510 if bypass_doc_val and sock_info .max_wire_version >= 4 :
507511 command ['bypassDocumentValidation' ] = True
508512 # Insert command.
509- result = sock_info .command (self .__database .name ,
510- command ,
511- codec_options = self .codec_options ,
512- check_keys = check_keys )
513+ result = sock_info .command (
514+ self .__database .name ,
515+ command ,
516+ codec_options = self .__write_response_codec_options ,
517+ check_keys = check_keys )
513518 _check_write_command_response ([(0 , result )])
514519 else :
515520 # Legacy OP_INSERT.
516521 self ._legacy_write (
517522 sock_info , 'insert' , command , acknowledged , op_id ,
518523 bypass_doc_val , message .insert , self .__full_name , [doc ],
519- check_keys , acknowledged , concern , False , self .codec_options )
524+ check_keys , acknowledged , concern , False ,
525+ self .__write_response_codec_options )
520526 if not isinstance (doc , RawBSONDocument ):
521527 return doc .get ('_id' )
522528
@@ -575,13 +581,13 @@ def gen():
575581 # Batched insert command.
576582 results = message ._do_batched_write_command (
577583 self .database .name + ".$cmd" , message ._INSERT , command ,
578- gen (), check_keys , self .codec_options , bwc )
584+ gen (), check_keys , self .__write_response_codec_options , bwc )
579585 _check_write_command_response (results )
580586 else :
581587 # Legacy batched OP_INSERT.
582588 message ._do_batched_insert (self .__full_name , gen (), check_keys ,
583589 acknowledged , concern , not ordered ,
584- self .codec_options , bwc )
590+ self .__write_response_codec_options , bwc )
585591 return ids
586592
587593 def insert_one (self , document , bypass_document_validation = False ):
@@ -704,9 +710,10 @@ def _update(self, sock_info, criteria, document, upsert=False,
704710
705711 # The command result has to be published for APM unmodified
706712 # so we make a shallow copy here before adding updatedExisting.
707- result = sock_info .command (self .__database .name ,
708- command ,
709- codec_options = self .codec_options ).copy ()
713+ result = sock_info .command (
714+ self .__database .name ,
715+ command ,
716+ codec_options = self .__write_response_codec_options ).copy ()
710717 _check_write_command_response ([(0 , result )])
711718 # Add the updatedExisting field for compatibility.
712719 if result .get ('n' ) and 'upserted' not in result :
@@ -725,7 +732,7 @@ def _update(self, sock_info, criteria, document, upsert=False,
725732 sock_info , 'update' , command , acknowledged , op_id ,
726733 bypass_doc_val , message .update , self .__full_name , upsert ,
727734 multi , criteria , document , acknowledged , concern , check_keys ,
728- self .codec_options )
735+ self .__write_response_codec_options )
729736
730737 def replace_one (self , filter , replacement , upsert = False ,
731738 bypass_document_validation = False ):
@@ -911,17 +918,19 @@ def _delete(
911918
912919 if sock_info .max_wire_version > 1 and acknowledged :
913920 # Delete command.
914- result = sock_info .command (self .__database .name ,
915- command ,
916- codec_options = self .codec_options )
921+ result = sock_info .command (
922+ self .__database .name ,
923+ command ,
924+ codec_options = self .__write_response_codec_options )
917925 _check_write_command_response ([(0 , result )])
918926 return result
919927 else :
920928 # Legacy OP_DELETE.
921929 return self ._legacy_write (
922930 sock_info , 'delete' , command , acknowledged , op_id ,
923931 False , message .delete , self .__full_name , criteria ,
924- acknowledged , concern , self .codec_options , int (not multi ))
932+ acknowledged , concern , self .__write_response_codec_options ,
933+ int (not multi ))
925934
926935 def delete_one (self , filter ):
927936 """Delete a single document matching the filter.
@@ -1192,11 +1201,11 @@ def parallel_scan(self, num_cursors):
11921201 def _count (self , cmd ):
11931202 """Internal count helper."""
11941203 with self ._socket_for_reads () as (sock_info , slave_ok ):
1195- res = self ._command (sock_info , cmd , slave_ok ,
1196- allowable_errors = [ "ns missing" ] ,
1197- codec_options = self . codec_options . _replace (
1198- document_class = dict ) ,
1199- read_concern = self .read_concern )
1204+ res = self ._command (
1205+ sock_info , cmd , slave_ok ,
1206+ allowable_errors = [ "ns missing" ],
1207+ codec_options = self . __write_response_codec_options ,
1208+ read_concern = self .read_concern )
12001209 if res .get ("errmsg" , "" ) == "ns missing" :
12011210 return 0
12021211 return int (res ["n" ])
0 commit comments