@@ -237,25 +237,35 @@ class CoreException(Exception):
237
237
10502 : "FILE_CORRUPT"
238
238
}
239
239
240
- def __init__ (self , code ):
240
+ def __init__ (self , code : int ):
241
241
self .code = code
242
242
self .message = py_str (C .obx_last_error_message ())
243
243
name = self .codes [code ] if code in self .codes else "n/a"
244
- super (CoreException , self ).__init__ (
245
- "%d (%s) - %s" % (code , name , self .message ))
244
+ super (CoreException , self ).__init__ ("%d (%s) - %s" % (code , name , self .message ))
245
+
246
+ @staticmethod
247
+ def last ():
248
+ """ Creates a CoreException of the last error that was generated in core. """
249
+ return CoreException (C .obx_last_error ())
246
250
247
251
248
252
class NotFoundException (Exception ):
249
253
pass
250
254
251
255
252
- # assert the the returned obx_err is empty
253
256
def check_obx_err (code : obx_err , func , args ) -> obx_err :
257
+ """ Raises an exception if obx_err is not successful. """
254
258
if code == 404 :
255
259
raise NotFoundException ()
256
260
elif code != 0 :
257
261
raise CoreException (code )
262
+ return code
258
263
264
+
265
+ def check_obx_qb_cond (code : obx_qb_cond , func , args ) -> obx_qb_cond :
266
+ """ Raises an exception if obx_qb_cond is not successful. """
267
+ if code == 0 :
268
+ raise CoreException (code )
259
269
return code
260
270
261
271
@@ -279,11 +289,19 @@ def c_fn(name: str, restype: Optional[type], argtypes):
279
289
280
290
# like c_fn, but for functions returning obx_err
281
291
def c_fn_rc (name : str , argtypes ):
292
+ """ Like c_fn, but for functions returning obx_err (checks obx_err validity). """
282
293
func = C .__getattr__ (name )
283
294
func .argtypes = argtypes
284
295
func .restype = obx_err
285
296
func .errcheck = check_obx_err
297
+ return func
286
298
299
+ def c_fn_qb_cond (name : str , argtypes ):
300
+ """ Like c_fn, but for functions returning obx_qb_cond (checks obx_qb_cond validity). """
301
+ func = C .__getattr__ (name )
302
+ func .argtypes = argtypes
303
+ func .restype = obx_qb_cond
304
+ func .errcheck = check_obx_qb_cond
287
305
return func
288
306
289
307
def py_str (ptr : ctypes .c_char_p ) -> str :
@@ -430,8 +448,7 @@ def c_voidp_as_bytes(voidp, size):
430
448
OBX_box_p , ctypes .c_uint64 , ctypes .POINTER (obx_id )])
431
449
432
450
# obx_err (OBX_box* box, obx_id id, const void* data, size_t size);
433
- obx_box_put = c_fn_rc ('obx_box_put' , [
434
- OBX_box_p , obx_id , ctypes .c_void_p , ctypes .c_size_t ])
451
+ obx_box_put = c_fn_rc ('obx_box_put' , [OBX_box_p , obx_id , ctypes .c_void_p , ctypes .c_size_t ])
435
452
436
453
# obx_err (OBX_box* box, const OBX_bytes_array* objects, const obx_id* ids, OBXPutMode mode);
437
454
obx_box_put_many = c_fn_rc ('obx_box_put_many' , [
@@ -620,9 +637,7 @@ def c_voidp_as_bytes(voidp, size):
620
637
obx_qb_order = c_fn_rc ('obx_qb_order' , [OBX_query_builder_p , obx_schema_id , OBXOrderFlags ])
621
638
622
639
# OBX_C_API obx_qb_cond obx_qb_nearest_neighbors_f32(OBX_query_builder* builder, obx_schema_id vector_property_id, const float* query_vector, size_t max_result_count)
623
- obx_qb_nearest_neighbors_f32 = \
624
- c_fn ('obx_qb_nearest_neighbors_f32' , obx_qb_cond , [OBX_query_builder_p , obx_schema_id ,
625
- ctypes .pointer (ctypes .c_float ), ctypes .c_size_t ])
640
+ obx_qb_nearest_neighbors_f32 = c_fn_qb_cond ('obx_qb_nearest_neighbors_f32' , [OBX_query_builder_p , obx_schema_id , ctypes .POINTER (ctypes .c_float ), ctypes .c_size_t ])
626
641
627
642
# OBX_C_API OBX_query* obx_query(OBX_query_builder* builder);
628
643
obx_query = c_fn ('obx_query' , OBX_query_p , [OBX_query_builder_p ])
@@ -687,6 +702,9 @@ def c_voidp_as_bytes(voidp, size):
687
702
# void (OBX_bytes_array * array);
688
703
obx_bytes_array_free = c_fn ('obx_bytes_array_free' , None , [OBX_bytes_array_p ])
689
704
705
+ # OBX_C_API void obx_id_array_free(OBX_id_array* array);
706
+ obx_id_array_free = c_fn ('obx_id_array_free' , None , [OBX_id_array_p ])
707
+
690
708
# OBX_C_API void obx_bytes_score_array_free(OBX_bytes_score_array* array)
691
709
obx_bytes_score_array_free = c_fn ('obx_bytes_score_array_free' , None , [OBX_bytes_score_array_p ])
692
710
0 commit comments