@@ -97,7 +97,7 @@ typedef struct {
9797} bson_buffer ;
9898
9999static int write_dict (bson_buffer * buffer , PyObject * dict ,
100- unsigned char check_keys , unsigned char move_id );
100+ unsigned char check_keys , unsigned char top_level );
101101static PyObject * elements_to_dict (const char * string , int max );
102102
103103static bson_buffer * buffer_new (void ) {
@@ -664,7 +664,7 @@ static int write_pair(bson_buffer* buffer, const char* name, Py_ssize_t name_len
664664
665665static int decode_and_write_pair (bson_buffer * buffer ,
666666 PyObject * key , PyObject * value ,
667- unsigned char check_keys , unsigned char move_id ) {
667+ unsigned char check_keys , unsigned char top_level ) {
668668 PyObject * encoded ;
669669 if (PyUnicode_Check (key )) {
670670 result_t status ;
@@ -706,9 +706,9 @@ static int decode_and_write_pair(bson_buffer* buffer,
706706 return 0 ;
707707 }
708708
709- /* If move_id is True, don't allow writing _id here - it was already written. */
709+ /* If top_level is True, don't allow writing _id here - it was already written. */
710710 if (!write_pair (buffer , PyString_AsString (encoded ),
711- PyString_Size (encoded ), value , check_keys , !move_id )) {
711+ PyString_Size (encoded ), value , check_keys , !top_level )) {
712712 Py_DECREF (encoded );
713713 return 0 ;
714714 }
@@ -719,7 +719,7 @@ static int decode_and_write_pair(bson_buffer* buffer,
719719
720720static int write_son (bson_buffer * buffer , PyObject * dict , int start_position ,
721721 int length_location , unsigned char check_keys ,
722- unsigned char move_id ) {
722+ unsigned char top_level ) {
723723 PyObject * keys = PyObject_CallMethod (dict , "keys" , NULL );
724724 int items ,
725725 i ;
@@ -738,7 +738,7 @@ static int write_son(bson_buffer* buffer, PyObject* dict, int start_position,
738738 }
739739 value = PyDict_GetItem (dict , key );
740740 if (!value ||
741- !decode_and_write_pair (buffer , key , value , check_keys , move_id )) {
741+ !decode_and_write_pair (buffer , key , value , check_keys , top_level )) {
742742 Py_DECREF (keys );
743743 return 0 ;
744744 }
@@ -748,7 +748,7 @@ static int write_son(bson_buffer* buffer, PyObject* dict, int start_position,
748748}
749749
750750/* returns 0 on failure */
751- static int write_dict (bson_buffer * buffer , PyObject * dict , unsigned char check_keys , unsigned char move_id ) {
751+ static int write_dict (bson_buffer * buffer , PyObject * dict , unsigned char check_keys , unsigned char top_level ) {
752752 int start_position = buffer -> position ;
753753 char zero = 0 ;
754754 int length ;
@@ -761,8 +761,8 @@ static int write_dict(bson_buffer* buffer, PyObject* dict, unsigned char check_k
761761 return 0 ;
762762 }
763763
764- /* Write _id first if we have one and move_id is true, whether or not we're SON . */
765- if (is_dict && move_id ) {
764+ /* Write _id first if this is a top level doc . */
765+ if (is_dict && top_level ) {
766766 PyObject * _id = PyDict_GetItemString (dict , "_id" );
767767 if (_id ) {
768768 /* Don't bother checking keys, but do make sure we're allowed to
@@ -774,7 +774,7 @@ static int write_dict(bson_buffer* buffer, PyObject* dict, unsigned char check_k
774774 }
775775
776776 if (PyObject_IsInstance (dict , SON )) {
777- if (!write_son (buffer , dict , start_position , length_location , check_keys , move_id )) {
777+ if (!write_son (buffer , dict , start_position , length_location , check_keys , top_level )) {
778778 return 0 ;
779779 }
780780 } else if (is_dict ) {
@@ -783,7 +783,7 @@ static int write_dict(bson_buffer* buffer, PyObject* dict, unsigned char check_k
783783 Py_ssize_t pos = 0 ;
784784
785785 while (PyDict_Next (dict , & pos , & key , & value )) {
786- if (!decode_and_write_pair (buffer , key , value , check_keys , move_id )) {
786+ if (!decode_and_write_pair (buffer , key , value , check_keys , top_level )) {
787787 return 0 ;
788788 }
789789 }
@@ -814,10 +814,9 @@ static PyObject* _cbson_dict_to_bson(PyObject* self, PyObject* args) {
814814 PyObject * dict ;
815815 PyObject * result ;
816816 unsigned char check_keys ;
817- unsigned char move_id ;
818817 bson_buffer * buffer ;
819818
820- if (!PyArg_ParseTuple (args , "Obb " , & dict , & check_keys , & move_id )) {
819+ if (!PyArg_ParseTuple (args , "Ob " , & dict , & check_keys )) {
821820 return NULL ;
822821 }
823822
@@ -826,7 +825,7 @@ static PyObject* _cbson_dict_to_bson(PyObject* self, PyObject* args) {
826825 return NULL ;
827826 }
828827
829- if (!write_dict (buffer , dict , check_keys , move_id )) {
828+ if (!write_dict (buffer , dict , check_keys , 1 )) {
830829 buffer_free (buffer );
831830 return NULL ;
832831 }
@@ -908,7 +907,6 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
908907
909908 for (i = 0 ; i < PyList_Size (docs ); i ++ ) {
910909 PyObject * doc = PyList_GetItem (docs , i );
911- /* We move_id for all inserts. */
912910 if (!write_dict (buffer , doc , check_keys , 1 )) {
913911 buffer_free (buffer );
914912 return NULL ;
@@ -968,8 +966,6 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
968966 }
969967
970968 // save space for message length
971- /* Note we move_id for the doc part of the update, but not for the spec.
972- * This is to handle the upsert case. */
973969 length_location = buffer_save_bytes (buffer , 4 );
974970 if (length_location == -1 ||
975971 !buffer_write_bytes (buffer , (const char * )& request_id , 4 ) ||
@@ -982,7 +978,7 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
982978 collection_name ,
983979 collection_name_length + 1 ) ||
984980 !buffer_write_bytes (buffer , (const char * )& options , 4 ) ||
985- !write_dict (buffer , spec , 0 , 0 ) ||
981+ !write_dict (buffer , spec , 0 , 1 ) ||
986982 !write_dict (buffer , doc , 0 , 1 )) {
987983 buffer_free (buffer );
988984 PyMem_Free (collection_name );
0 commit comments