Skip to content

Commit

Permalink
pytalloc: Further refactoring to eliminate duplicate code.
Browse files Browse the repository at this point in the history
Signed-off-by: Kristján Valur <[email protected]>
Reviewed-by: Noel Power <[email protected]>
Reviewed-by: Andrew Bartlett <[email protected]>
  • Loading branch information
Kristján Valur authored and abartlet committed Mar 26, 2019
1 parent 9ac4596 commit 417a359
Showing 1 changed file with 34 additions and 36 deletions.
70 changes: 34 additions & 36 deletions lib/talloc/pytalloc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,55 +131,53 @@ static PyObject *pytalloc_steal_or_reference(PyTypeObject *py_type,
TALLOC_CTX *mem_ctx, void *ptr, bool steal)
{
bool ok = false;
TALLOC_CTX *talloc_ctx = NULL;
bool is_baseobject = false;
PyObject *obj = NULL;
PyTypeObject *BaseObjectType = pytalloc_GetBaseObjectType();
PyTypeObject *ObjectType = pytalloc_GetObjectType();

if (mem_ctx == NULL) {
return PyErr_NoMemory();
}

if (PyType_IsSubtype(py_type, BaseObjectType)) {
pytalloc_BaseObject *ret
= (pytalloc_BaseObject *)py_type->tp_alloc(py_type, 0);
ret->talloc_ctx = talloc_new(NULL);
if (ret->talloc_ctx == NULL) {
return NULL;
}
if (steal) {
ok = (talloc_steal(ret->talloc_ctx, mem_ctx) != NULL);
} else {
ok = (talloc_reference(ret->talloc_ctx, mem_ctx) != NULL);
}
if (!ok) {
is_baseobject = PyType_IsSubtype(py_type, BaseObjectType);
if (!is_baseobject) {
if (!PyType_IsSubtype(py_type, ObjectType)) {
PyErr_SetString(PyExc_RuntimeError,
"Expected type based on talloc");
return NULL;
}
talloc_set_name_const(ret->talloc_ctx, py_type->tp_name);
}

obj = py_type->tp_alloc(py_type, 0);

talloc_ctx = talloc_new(NULL);
if (talloc_ctx == NULL) {
return NULL;
}

if (steal) {
ok = (talloc_steal(talloc_ctx, mem_ctx) != NULL);
} else {
ok = (talloc_reference(talloc_ctx, mem_ctx) != NULL);
}
if (!ok) {
return NULL;
}
talloc_set_name_const(talloc_ctx, py_type->tp_name);

if (is_baseobject) {
pytalloc_BaseObject *ret = (pytalloc_BaseObject*)obj;
ret->talloc_ctx = talloc_ctx;
ret->talloc_ptr_ctx = mem_ctx;
ret->ptr = ptr;
return (PyObject *)ret;
} else if (PyType_IsSubtype(py_type, ObjectType)) {
pytalloc_Object *ret
= (pytalloc_Object *)py_type->tp_alloc(py_type, 0);
ret->talloc_ctx = talloc_new(NULL);
if (ret->talloc_ctx == NULL) {
return NULL;
}
if (steal) {
ok = (talloc_steal(ret->talloc_ctx, mem_ctx) != NULL);
} else {
ok = (talloc_reference(ret->talloc_ctx, mem_ctx) != NULL);
}
if (!ok) {
return NULL;
}
talloc_set_name_const(ret->talloc_ctx, py_type->tp_name);
ret->ptr = ptr;
return (PyObject *)ret;
} else {
PyErr_SetString(PyExc_RuntimeError,
"Expected type based on talloc");
return NULL;
pytalloc_Object *ret = (pytalloc_Object*)obj;
ret->talloc_ctx = talloc_ctx;
ret->ptr = ptr;
}
return obj;
}

/*
Expand Down

0 comments on commit 417a359

Please sign in to comment.