Skip to content

Commit c8ad84a

Browse files
committed
c.py: bubble up more functions to py objectbox#24
1 parent cbdd07d commit c8ad84a

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

objectbox/c.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,35 @@ class CoreException(Exception):
237237
10502: "FILE_CORRUPT"
238238
}
239239

240-
def __init__(self, code):
240+
def __init__(self, code: int):
241241
self.code = code
242242
self.message = py_str(C.obx_last_error_message())
243243
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())
246250

247251

248252
class NotFoundException(Exception):
249253
pass
250254

251255

252-
# assert the the returned obx_err is empty
253256
def check_obx_err(code: obx_err, func, args) -> obx_err:
257+
""" Raises an exception if obx_err is not successful. """
254258
if code == 404:
255259
raise NotFoundException()
256260
elif code != 0:
257261
raise CoreException(code)
262+
return code
258263

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)
259269
return code
260270

261271

@@ -279,11 +289,19 @@ def c_fn(name: str, restype: Optional[type], argtypes):
279289

280290
# like c_fn, but for functions returning obx_err
281291
def c_fn_rc(name: str, argtypes):
292+
""" Like c_fn, but for functions returning obx_err (checks obx_err validity). """
282293
func = C.__getattr__(name)
283294
func.argtypes = argtypes
284295
func.restype = obx_err
285296
func.errcheck = check_obx_err
297+
return func
286298

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
287305
return func
288306

289307
def py_str(ptr: ctypes.c_char_p) -> str:
@@ -430,8 +448,7 @@ def c_voidp_as_bytes(voidp, size):
430448
OBX_box_p, ctypes.c_uint64, ctypes.POINTER(obx_id)])
431449

432450
# 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])
435452

436453
# obx_err (OBX_box* box, const OBX_bytes_array* objects, const obx_id* ids, OBXPutMode mode);
437454
obx_box_put_many = c_fn_rc('obx_box_put_many', [
@@ -620,9 +637,7 @@ def c_voidp_as_bytes(voidp, size):
620637
obx_qb_order = c_fn_rc('obx_qb_order', [OBX_query_builder_p, obx_schema_id, OBXOrderFlags])
621638

622639
# 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])
626641

627642
# OBX_C_API OBX_query* obx_query(OBX_query_builder* builder);
628643
obx_query = c_fn('obx_query', OBX_query_p, [OBX_query_builder_p])
@@ -687,6 +702,9 @@ def c_voidp_as_bytes(voidp, size):
687702
# void (OBX_bytes_array * array);
688703
obx_bytes_array_free = c_fn('obx_bytes_array_free', None, [OBX_bytes_array_p])
689704

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+
690708
# OBX_C_API void obx_bytes_score_array_free(OBX_bytes_score_array* array)
691709
obx_bytes_score_array_free = c_fn('obx_bytes_score_array_free', None, [OBX_bytes_score_array_p])
692710

0 commit comments

Comments
 (0)