Skip to content

Commit

Permalink
Use explicit ref-counting for user defined data in python.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Jul 8, 2024
1 parent e331c34 commit 0e8ca36
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 35 deletions.
38 changes: 24 additions & 14 deletions bindings/python/cconfigspace/tree_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ def _wrap_user_defined_callbacks(delete, get_child, serialize, deserialize):
def delete_wrapper(ts):
try:
ts = ct.cast(ts, ccs_tree_space)
o = Object.from_handle(ts)
tsdata = o.tree_space_data
if delete is not None:
delete(Object.from_handle(ts))
delete(o)
if tsdata is not None:
ct.pythonapi.Py_DecRef(ct.py_object(tsdata))
_unregister_vector(ts)
return Result.SUCCESS
except Exception as e:
Expand Down Expand Up @@ -252,14 +256,15 @@ def __init__(self, handle = None, retain = False, auto_release = True,
if get_child is None:
raise Error(Result(Result.ERROR_INVALID_VALUE))

(delete_wrapper,
get_child_wrapper,
serialize_wrapper,
deserialize_wrapper,
wrappers = _wrap_user_defined_callbacks(delete, get_child, serialize, deserialize)
(_,
_,
_,
_,
delete_wrapper_func,
get_child_wrapper_func,
serialize_wrapper_func,
deserialize_wrapper_func) = _wrap_user_defined_callbacks(delete, get_child, serialize, deserialize)
deserialize_wrapper_func) = wrappers
handle = ccs_tree_space()
vec = DynamicTreeSpaceVector()
vec.delete = delete_wrapper_func
Expand All @@ -277,30 +282,35 @@ def __init__(self, handle = None, retain = False, auto_release = True,
res = ccs_create_dynamic_tree_space(str.encode(name), tree.handle, feature_space, rng, ct.byref(vec), c_tree_space_data, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
_register_vector(handle, [delete_wrapper, get_child_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, get_child_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tree_space_data])
_register_vector(handle, wrappers)
if c_tree_space_data is not None:
ct.pythonapi.Py_IncRef(c_tree_space_data)
else:
super().__init__(handle = handle, retain = retain, auto_release = auto_release)

@classmethod
def deserialize(cls, delete, get_child, serialize = None, deserialize = None, tree_space_data = None, format = 'binary', handle_map = None, path = None, buffer = None, file_descriptor = None, callback = None, callback_data = None):
if get_child is None:
raise Error(Result(Result.ERROR_INVALID_VALUE))
(delete_wrapper,
get_child_wrapper,
serialize_wrapper,
deserialize_wrapper,

wrappers = _wrap_user_defined_callbacks(delete, get_child, serialize, deserialize)
(_,
_,
_,
_,
delete_wrapper_func,
get_child_wrapper_func,
serialize_wrapper_func,
deserialize_wrapper_func) = _wrap_user_defined_callbacks(delete, get_child, serialize, deserialize)
handle = ccs_tree_space()
deserialize_wrapper_func) = wrappers
vector = DynamicTreeSpaceVector()
vector.delete = delete_wrapper_func
vector.get_child = get_child_wrapper_func
vector.serialize = serialize_wrapper_func
vector.deserialize = deserialize_wrapper_func
res = super().deserialize(format = format, handle_map = handle_map, vector = vector, data = tree_space_data, path = path, buffer = buffer, file_descriptor = file_descriptor, callback = callback, callback_data = callback_data)
_register_vector(res.handle, [delete_wrapper, get_child_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, get_child_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tree_space_data])
_register_vector(res.handle, wrappers)
if tree_space_data is not None:
ct.pythonapi.Py_IncRef(ct.py_object(tree_space_data))
return res

@property
Expand Down
52 changes: 31 additions & 21 deletions bindings/python/cconfigspace/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,12 @@ def _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_histor
def delete_wrapper(tun):
try:
tun = ct.cast(tun, ccs_tuner)
o = Object.from_handle(tun)
tdata = o.tuner_data
if delete is not None:
delete(Object.from_handle(tun))
delete(o)
if tdata is not None:
ct.pythonapi.Py_DecRef(ct.py_object(tdata))
_unregister_vector(tun)
return Result.SUCCESS
except Exception as e:
Expand Down Expand Up @@ -353,22 +357,23 @@ def __init__(self, handle = None, retain = False, auto_release = True,
if ask is None or tell is None or get_optima is None or get_history is None:
raise Error(Result(Result.ERROR_INVALID_VALUE))

(delete_wrapper,
ask_wrapper,
tell_wrapper,
get_optima_wrapper,
get_history_wrapper,
suggest_wrapper,
serialize_wrapper,
deserialize_wrapper,
wrappers = _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize)
(_,
_,
_,
_,
_,
_,
_,
_,
delete_wrapper_func,
ask_wrapper_func,
tell_wrapper_func,
get_optima_wrapper_func,
get_history_wrapper_func,
suggest_wrapper_func,
serialize_wrapper_func,
deserialize_wrapper_func) = _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize)
deserialize_wrapper_func) = wrappers
handle = ccs_tuner()
vec = UserDefinedTunerVector()
vec.delete = delete_wrapper_func
Expand All @@ -386,30 +391,33 @@ def __init__(self, handle = None, retain = False, auto_release = True,
res = ccs_create_user_defined_tuner(str.encode(name), objective_space.handle, ct.byref(vec), c_tuner_data, ct.byref(handle))
Error.check(res)
super().__init__(handle = handle, retain = False)
_register_vector(handle, [delete_wrapper, ask_wrapper, tell_wrapper, get_optima_wrapper, get_history_wrapper, suggest_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, ask_wrapper_func, tell_wrapper_func, get_optima_wrapper_func, get_history_wrapper_func, suggest_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tuner_data])
_register_vector(handle, wrappers)
if c_tuner_data is not None:
ct.pythonapi.Py_IncRef(c_tuner_data)
else:
super().__init__(handle = handle, retain = retain, auto_release = auto_release)

@classmethod
def deserialize(cls, delete, ask, tell, get_optima, get_history, suggest = None, serialize = None, deserialize = None, tuner_data = None, format = 'binary', handle_map = None, path = None, buffer = None, file_descriptor = None, callback = None, callback_data = None):
if ask is None or tell is None or get_optima is None or get_history is None:
raise Error(Result(Result.ERROR_INVALID_VALUE))
(delete_wrapper,
ask_wrapper,
tell_wrapper,
get_optima_wrapper,
get_history_wrapper,
suggest_wrapper,
serialize_wrapper,
deserialize_wrapper,
wrappers = _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize)
(_,
_,
_,
_,
_,
_,
_,
_,
delete_wrapper_func,
ask_wrapper_func,
tell_wrapper_func,
get_optima_wrapper_func,
get_history_wrapper_func,
suggest_wrapper_func,
serialize_wrapper_func,
deserialize_wrapper_func) = _wrap_user_defined_tuner_callbacks(delete, ask, tell, get_optima, get_history, suggest, serialize, deserialize)
deserialize_wrapper_func) = wrappers
vector = UserDefinedTunerVector()
vector.delete = delete_wrapper_func
vector.ask = ask_wrapper_func
Expand All @@ -420,7 +428,9 @@ def deserialize(cls, delete, ask, tell, get_optima, get_history, suggest = None,
vector.serialize = serialize_wrapper_func
vector.deserialize = deserialize_wrapper_func
res = super().deserialize(format = format, handle_map = handle_map, vector = vector, data = tuner_data, path = path, buffer = buffer, file_descriptor = file_descriptor, callback = callback, callback_data = callback_data)
_register_vector(res.handle, [delete_wrapper, ask_wrapper, tell_wrapper, get_optima_wrapper, get_history_wrapper, suggest_wrapper, serialize_wrapper, deserialize_wrapper, delete_wrapper_func, ask_wrapper_func, tell_wrapper_func, get_optima_wrapper_func, get_history_wrapper_func, suggest_wrapper_func, serialize_wrapper_func, deserialize_wrapper_func, tuner_data])
_register_vector(res.handle, wrappers)
if tuner_data is not None:
ct.pythonapi.Py_IncRef(ct.py_object(tuner_data))
return res

@property
Expand Down

0 comments on commit 0e8ca36

Please sign in to comment.