1717.. versionadded:: 2.7
1818"""
1919
20- from __future__ import unicode_literals
21-
2220from bson .objectid import ObjectId
21+ from bson .py3compat import u
2322from bson .son import SON
2423from pymongo .common import (validate_is_mapping ,
2524 validate_is_mutable_mapping ,
4544_COMMANDS = ('insert' , 'update' , 'delete' )
4645
4746
47+ # These string literals are used when we create fake server return
48+ # documents client side. We use unicode literals in python 2.x to
49+ # match the actual return values from the server.
50+ _UID = u ("_id" )
51+ _UCODE = u ("code" )
52+ _UERRMSG = u ("errmsg" )
53+ _UINDEX = u ("index" )
54+ _UOP = u ("op" )
55+
56+
4857class _Run (object ):
4958 """Represents a batch of write operations.
5059 """
@@ -79,10 +88,10 @@ def _make_error(index, code, errmsg, operation):
7988 """Create and return an error document.
8089 """
8190 return {
82- "index" : index ,
83- "code" : code ,
84- "errmsg" : errmsg ,
85- "op" : operation
91+ _UINDEX : index ,
92+ _UCODE : code ,
93+ _UERRMSG : errmsg ,
94+ _UOP : operation
8695 }
8796
8897
@@ -109,7 +118,7 @@ def _merge_legacy(run, full_result, result, index):
109118 full_result ['nInserted' ] += 1
110119 elif run .op_type == _UPDATE :
111120 if "upserted" in result :
112- doc = {"index" : run .index (index ), "_id" : result ["upserted" ]}
121+ doc = {_UINDEX : run .index (index ), _UID : result ["upserted" ]}
113122 full_result ["upserted" ].append (doc )
114123 full_result ['nUpserted' ] += affected
115124 # Versions of MongoDB before 2.6 don't return the _id for an
@@ -119,7 +128,7 @@ def _merge_legacy(run, full_result, result, index):
119128 # If _id is in both the update document *and* the query spec
120129 # the update document _id takes precedence.
121130 _id = op ['u' ].get ('_id' , op ['q' ].get ('_id' ))
122- doc = {"index" : run .index (index ), "_id" : _id }
131+ doc = {_UINDEX : run .index (index ), _UID : _id }
123132 full_result ["upserted" ].append (doc )
124133 full_result ['nUpserted' ] += affected
125134 else :
@@ -153,7 +162,7 @@ def _merge_command(run, full_result, results):
153162 else :
154163 n_upserted = 1
155164 index = run .index (offset )
156- doc = {"index" : index , "_id" : upserted }
165+ doc = {_UINDEX : index , _UID : upserted }
157166 full_result ["upserted" ].append (doc )
158167 full_result ["nUpserted" ] += n_upserted
159168 full_result ["nMatched" ] += (affected - n_upserted )
@@ -175,7 +184,7 @@ def _merge_command(run, full_result, results):
175184 idx = doc ["index" ] + offset
176185 doc ["index" ] = run .index (idx )
177186 # Add the failed operation to the error document.
178- doc ["op" ] = run .ops [idx ]
187+ doc [_UOP ] = run .ops [idx ]
179188 full_result ["writeErrors" ].extend (write_errors )
180189
181190 wc_error = result .get ("writeConcernError" )
0 commit comments