Skip to content

Commit

Permalink
python: use _cleanup_ in Program_load_debug_info()
Browse files Browse the repository at this point in the history
path_args and paths both require some annoying manual cleanup. Use
cleanup attributes instead.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Oct 22, 2024
1 parent e56ea49 commit bce453d
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions libdrgn/python/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,13 @@ static PyObject *Program_set_pid(Program *self, PyObject *args, PyObject *kwds)

DEFINE_VECTOR(path_arg_vector, struct path_arg);

static void path_arg_vector_cleanup(struct path_arg_vector *path_args)
{
vector_for_each(path_arg_vector, path_arg, path_args)
path_cleanup(path_arg);
path_arg_vector_deinit(path_args);
}

static PyObject *Program_load_debug_info(Program *self, PyObject *args,
PyObject *kwds)
{
Expand All @@ -881,19 +888,20 @@ static PyObject *Program_load_debug_info(Program *self, PyObject *args,
&load_main))
return NULL;

struct path_arg_vector path_args = VECTOR_INIT;
const char **paths = NULL;
_cleanup_(path_arg_vector_cleanup)
struct path_arg_vector path_args = VECTOR_INIT;
_cleanup_free_ const char **paths = NULL;
if (paths_obj != Py_None) {
_cleanup_pydecref_ PyObject *it = PyObject_GetIter(paths_obj);
if (!it)
goto out;
return NULL;

Py_ssize_t length_hint = PyObject_LengthHint(paths_obj, 1);
if (length_hint == -1)
goto out;
return NULL;
if (!path_arg_vector_reserve(&path_args, length_hint)) {
PyErr_NoMemory();
goto out;
return NULL;
}

for (;;) {
Expand All @@ -905,39 +913,33 @@ static PyObject *Program_load_debug_info(Program *self, PyObject *args,
path_arg_vector_append_entry(&path_args);
if (!path_arg) {
PyErr_NoMemory();
break;
return NULL;
}
memset(path_arg, 0, sizeof(*path_arg));
if (!path_converter(item, path_arg)) {
path_arg_vector_pop(&path_args);
break;
return NULL;
}
}
if (PyErr_Occurred())
goto out;
return NULL;

paths = malloc_array(path_arg_vector_size(&path_args),
sizeof(*paths));
if (!paths) {
PyErr_NoMemory();
goto out;
return NULL;
}
for (size_t i = 0; i < path_arg_vector_size(&path_args); i++)
paths[i] = path_arg_vector_at(&path_args, i)->path;
}
err = drgn_program_load_debug_info(&self->prog, paths,
path_arg_vector_size(&path_args),
load_default, load_main);
free(paths);
if (err)
if (err) {
set_drgn_error(err);

out:
vector_for_each(path_arg_vector, path_arg, &path_args)
path_cleanup(path_arg);
path_arg_vector_deinit(&path_args);
if (PyErr_Occurred())
return NULL;
}
Py_RETURN_NONE;
}

Expand Down

0 comments on commit bce453d

Please sign in to comment.