From 36bdb3b9af271a8dcde23d40737117d7ce5daef8 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Tue, 30 Nov 2010 22:59:51 +0100 Subject: pytalloc: Add convenience function for checking if something is a talloc object. --- lib/talloc/pytalloc.h | 2 ++ lib/talloc/pytalloc_util.c | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/talloc/pytalloc.h b/lib/talloc/pytalloc.h index b17977a46c..be69c4d50c 100644 --- a/lib/talloc/pytalloc.h +++ b/lib/talloc/pytalloc.h @@ -32,6 +32,8 @@ typedef struct { /* Deallocate a py_talloc_Object */ void py_talloc_dealloc(PyObject* self); +int PyTalloc_Check(PyObject *); + /* Retrieve the pointer for a py_talloc_object. Like talloc_get_type() * but for py_talloc_Objects. */ diff --git a/lib/talloc/pytalloc_util.c b/lib/talloc/pytalloc_util.c index d5ef919bb2..c485cd1ca3 100644 --- a/lib/talloc/pytalloc_util.c +++ b/lib/talloc/pytalloc_util.c @@ -23,6 +23,26 @@ #include "pytalloc.h" #include +static PyTypeObject *Get_TallocType(void) +{ + static PyTypeObject *type = NULL; + PyObject *mod; + + if (type != NULL) { + return type; + } + + mod = PyImport_ImportModule("talloc"); + if (mod == NULL) { + return NULL; + } + + type = (PyTypeObject *)PyObject_GetAttrString(mod, "Object"); + Py_DECREF(mod); + + return type; +} + /** * Simple dealloc for talloc-wrapping PyObjects */ @@ -133,3 +153,10 @@ PyObject *PyString_FromString_check_null(const char *ptr) } return PyString_FromString(ptr); } + +int PyTalloc_Check(PyObject *obj) +{ + PyTypeObject *tp = Get_TallocType(); + + return PyObject_TypeCheck(obj, tp); +} -- cgit