@@ -89,16 +89,23 @@ STATIC mp_obj_t btree_put(size_t n_args, const mp_obj_t *args) {
8989 (void )n_args ;
9090 mp_obj_btree_t * self = MP_OBJ_TO_PTR (args [0 ]);
9191 DBT key , val ;
92- key .data = (void * )mp_obj_str_get_data (args [1 ], & key .size );
93- val .data = (void * )mp_obj_str_get_data (args [2 ], & val .size );
92+ // Different ports may have different type sizes
93+ mp_uint_t v ;
94+ key .data = (void * )mp_obj_str_get_data (args [1 ], & v );
95+ key .size = v ;
96+ val .data = (void * )mp_obj_str_get_data (args [2 ], & v );
97+ val .size = v ;
9498 return MP_OBJ_NEW_SMALL_INT (__bt_put (self -> db , & key , & val , 0 ));
9599}
96100STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (btree_put_obj , 3 , 4 , btree_put );
97101
98102STATIC mp_obj_t btree_get (size_t n_args , const mp_obj_t * args ) {
99103 mp_obj_btree_t * self = MP_OBJ_TO_PTR (args [0 ]);
100104 DBT key , val ;
101- key .data = (void * )mp_obj_str_get_data (args [1 ], & key .size );
105+ // Different ports may have different type sizes
106+ mp_uint_t v ;
107+ key .data = (void * )mp_obj_str_get_data (args [1 ], & v );
108+ key .size = v ;
102109 int res = __bt_get (self -> db , & key , & val , 0 );
103110 if (res == RET_SPECIAL ) {
104111 if (n_args > 2 ) {
@@ -117,18 +124,22 @@ STATIC mp_obj_t btree_seq(size_t n_args, const mp_obj_t *args) {
117124 int flags = MP_OBJ_SMALL_INT_VALUE (args [1 ]);
118125 DBT key , val ;
119126 if (n_args > 2 ) {
120- key .data = (void * )mp_obj_str_get_data (args [2 ], & key .size );
127+ // Different ports may have different type sizes
128+ mp_uint_t v ;
129+ key .data = (void * )mp_obj_str_get_data (args [2 ], & v );
130+ key .size = v ;
121131 }
122132
123133 int res = __bt_seq (self -> db , & key , & val , flags );
124134 if (res == RET_SPECIAL ) {
125135 return mp_const_none ;
126136 }
127137
128- mp_obj_tuple_t * pair = mp_obj_new_tuple (2 , NULL );
138+ mp_obj_t pair_o = mp_obj_new_tuple (2 , NULL );
139+ mp_obj_tuple_t * pair = MP_OBJ_TO_PTR (pair_o );
129140 pair -> items [0 ] = mp_obj_new_bytes (key .data , key .size );
130141 pair -> items [1 ] = mp_obj_new_bytes (val .data , val .size );
131- return pair ;
142+ return pair_o ;
132143}
133144STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (btree_seq_obj , 2 , 4 , btree_seq );
134145
@@ -185,11 +196,14 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) {
185196 mp_obj_btree_t * self = MP_OBJ_TO_PTR (self_in );
186197 DBT key , val ;
187198 int res ;
199+ // Different ports may have different type sizes
200+ mp_uint_t v ;
188201 bool desc = self -> flags & FLAG_DESC ;
189202 if (self -> start_key != MP_OBJ_NULL ) {
190203 int flags = R_FIRST ;
191204 if (self -> start_key != mp_const_none ) {
192- key .data = (void * )mp_obj_str_get_data (self -> start_key , & key .size );
205+ key .data = (void * )mp_obj_str_get_data (self -> start_key , & v );
206+ key .size = v ;
193207 flags = R_CURSOR ;
194208 } else if (desc ) {
195209 flags = R_LAST ;
@@ -207,7 +221,8 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) {
207221
208222 if (self -> end_key != mp_const_none ) {
209223 DBT end_key ;
210- end_key .data = (void * )mp_obj_str_get_data (self -> end_key , & end_key .size );
224+ end_key .data = (void * )mp_obj_str_get_data (self -> end_key , & v );
225+ end_key .size = v ;
211226 BTREE * t = self -> db -> internal ;
212227 int cmp = t -> bt_cmp (& key , & end_key );
213228 if (desc ) {
@@ -228,20 +243,24 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) {
228243 case FLAG_ITER_VALUES :
229244 return mp_obj_new_bytes (val .data , val .size );
230245 default : {
231- mp_obj_tuple_t * pair = mp_obj_new_tuple (2 , NULL );
246+ mp_obj_t pair_o = mp_obj_new_tuple (2 , NULL );
247+ mp_obj_tuple_t * pair = MP_OBJ_TO_PTR (pair_o );
232248 pair -> items [0 ] = mp_obj_new_bytes (key .data , key .size );
233249 pair -> items [1 ] = mp_obj_new_bytes (val .data , val .size );
234- return pair ;
250+ return pair_o ;
235251 }
236252 }
237253}
238254
239255STATIC mp_obj_t btree_subscr (mp_obj_t self_in , mp_obj_t index , mp_obj_t value ) {
240256 mp_obj_btree_t * self = MP_OBJ_TO_PTR (self_in );
257+ // Different ports may have different type sizes
258+ mp_uint_t v ;
241259 if (value == MP_OBJ_NULL ) {
242260 // delete
243261 DBT key ;
244- key .data = (void * )mp_obj_str_get_data (index , & key .size );
262+ key .data = (void * )mp_obj_str_get_data (index , & v );
263+ key .size = v ;
245264 int res = __bt_delete (self -> db , & key , 0 );
246265 if (res == RET_SPECIAL ) {
247266 nlr_raise (mp_obj_new_exception (& mp_type_KeyError ));
@@ -251,7 +270,8 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
251270 } else if (value == MP_OBJ_SENTINEL ) {
252271 // load
253272 DBT key , val ;
254- key .data = (void * )mp_obj_str_get_data (index , & key .size );
273+ key .data = (void * )mp_obj_str_get_data (index , & v );
274+ key .size = v ;
255275 int res = __bt_get (self -> db , & key , & val , 0 );
256276 if (res == RET_SPECIAL ) {
257277 nlr_raise (mp_obj_new_exception (& mp_type_KeyError ));
@@ -261,8 +281,10 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
261281 } else {
262282 // store
263283 DBT key , val ;
264- key .data = (void * )mp_obj_str_get_data (index , & key .size );
265- val .data = (void * )mp_obj_str_get_data (value , & val .size );
284+ key .data = (void * )mp_obj_str_get_data (index , & v );
285+ key .size = v ;
286+ val .data = (void * )mp_obj_str_get_data (value , & v );
287+ val .size = v ;
266288 int res = __bt_put (self -> db , & key , & val , 0 );
267289 CHECK_ERROR (res );
268290 return mp_const_none ;
@@ -316,8 +338,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_btree_open_obj, 1, mod_btree_open);
316338STATIC const mp_rom_map_elem_t mp_module_btree_globals_table [] = {
317339 { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_btree ) },
318340 { MP_ROM_QSTR (MP_QSTR_open ), MP_ROM_PTR (& mod_btree_open_obj ) },
319- { MP_ROM_QSTR (MP_QSTR_INCL ), MP_OBJ_NEW_SMALL_INT (FLAG_END_KEY_INCL ) },
320- { MP_ROM_QSTR (MP_QSTR_DESC ), MP_OBJ_NEW_SMALL_INT (FLAG_DESC ) },
341+ { MP_ROM_QSTR (MP_QSTR_INCL ), MP_ROM_INT (FLAG_END_KEY_INCL ) },
342+ { MP_ROM_QSTR (MP_QSTR_DESC ), MP_ROM_INT (FLAG_DESC ) },
321343};
322344
323345STATIC MP_DEFINE_CONST_DICT (mp_module_btree_globals , mp_module_btree_globals_table );
0 commit comments