Skip to content

Commit e7e3b24

Browse files
committed
Box add/remove: pass obx_err code, removed try block #32
c: added c_fn_nocheck for obx_box_add/remove
1 parent 98f5f8b commit e7e3b24

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

objectbox/box.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@ def get(self, id: int):
112112
with self._ob.read_tx():
113113
c_data = ctypes.c_void_p()
114114
c_size = ctypes.c_size_t()
115-
try:
116-
obx_box_get(self._c_box, id, ctypes.byref(
115+
code : obx_err = obx_box_get(self._c_box, id, ctypes.byref(
117116
c_data), ctypes.byref(c_size))
118-
except NotFoundException:
117+
if code == 404:
119118
return None
119+
elif code != 0:
120+
raise CoreException(code)
120121
data = c_voidp_as_bytes(c_data, c_size.value)
121122
return self._entity.unmarshal(data)
122123

@@ -145,11 +146,12 @@ def remove(self, id_or_object):
145146
id = self._entity.get_object_id(id_or_object)
146147
else:
147148
id = id_or_object
148-
try:
149-
obx_box_remove(self._c_box, id)
150-
return True
151-
except NotFoundException:
149+
code : obx_err = obx_box_remove(self._c_box, id)
150+
if code == 404:
152151
return False
152+
elif code != 0:
153+
raise CoreException(code)
154+
return True
153155

154156
def remove_all(self) -> int:
155157
count = ctypes.c_uint64()

objectbox/c.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,13 @@ def c_fn(name: str, restype: Optional[type], argtypes):
292292

293293
return func
294294

295+
# creates a global function "name" with the given restype & argtypes, calling C function with the same name.
296+
# no error checking is done on restype as this is defered to higher-level functions.
297+
def c_fn_nocheck(name: str, restype: type, argtypes):
298+
func = C.__getattr__(name)
299+
func.argtypes = argtypes
300+
func.restype = restype
301+
return func
295302

296303
# like c_fn, but for functions returning obx_err
297304
def c_fn_rc(name: str, argtypes):
@@ -302,7 +309,6 @@ def c_fn_rc(name: str, argtypes):
302309
func.errcheck = check_obx_err
303310
return func
304311

305-
306312
def c_fn_qb_cond(name: str, argtypes):
307313
""" Like c_fn, but for functions returning obx_qb_cond (checks obx_qb_cond validity). """
308314
func = C.__getattr__(name)
@@ -462,7 +468,7 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
462468
obx_box = c_fn('obx_box', OBX_box_p, [OBX_store_p, obx_schema_id])
463469

464470
# obx_err (OBX_box* box, obx_id id, const void** data, size_t* size);
465-
obx_box_get = c_fn_rc('obx_box_get', [
471+
obx_box_get = c_fn_nocheck('obx_box_get', obx_err, [
466472
OBX_box_p, obx_id, ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_size_t)])
467473

468474
# OBX_bytes_array* (OBX_box* box);
@@ -483,7 +489,7 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
483489
OBX_box_p, OBX_bytes_array_p, ctypes.POINTER(obx_id), OBXPutMode])
484490

485491
# obx_err (OBX_box* box, obx_id id);
486-
obx_box_remove = c_fn_rc('obx_box_remove', [OBX_box_p, obx_id])
492+
obx_box_remove = c_fn_nocheck('obx_box_remove', obx_err, [OBX_box_p, obx_id])
487493

488494
# obx_err (OBX_box* box, uint64_t* out_count);
489495
obx_box_remove_all = c_fn_rc('obx_box_remove_all', [

0 commit comments

Comments
 (0)