s4-pyrpc: added py_return_ndr_struct()
authorAndrew Tridgell <tridge@samba.org>
Wed, 15 Sep 2010 08:49:06 +0000 (18:49 +1000)
committerAndrew Tridgell <tridge@samba.org>
Wed, 15 Sep 2010 21:24:01 +0000 (07:24 +1000)
This can be used to return structures from other python interfaces as
python objects

Pair-Programmed-With: Andrew Bartlett <abartlet@samba.org>

source4/librpc/rpc/pyrpc_util.c
source4/librpc/rpc/pyrpc_util.h

index 2af868c0c2c37a32c7e65950bccd0c923f814bac..f3911eeb9ab6652e99faaa7c88ba6bda7889e341 100644 (file)
@@ -240,3 +240,34 @@ void PyErr_SetDCERPCStatus(struct dcerpc_pipe *p, NTSTATUS status)
 }
 
 
+/*
+  take a NDR structure that has a type in a python module and return
+  it as a python object
+
+  r is the NDR structure pointer (a C structure)
+
+  r_ctx is the context that is a parent of r. It will be referenced by
+  the resulting python object
+ */
+PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
+                              TALLOC_CTX *r_ctx, void *r)
+{
+       PyTypeObject *py_type;
+       PyObject *module;
+
+       if (r == NULL) {
+               Py_RETURN_NONE;
+       }
+
+       module = PyImport_ImportModule(module_name);
+       if (module == NULL) {
+               return NULL;
+       }
+
+       py_type = (PyTypeObject *)PyObject_GetAttrString(module, type_name);
+       if (py_type == NULL) {
+               return NULL;
+       }
+
+       return py_talloc_reference_ex(py_type, r_ctx, r);
+}
index abd7f6ba7b9853e3fa23b734336caed0c5636282..1efe112a11232739f6d88b06baaee39113d20007 100644 (file)
@@ -55,4 +55,7 @@ bool py_check_dcerpc_type(PyObject *obj, const char *module, const char *typenam
 bool PyInterface_AddNdrRpcMethods(PyTypeObject *object, const struct PyNdrRpcMethodDef *mds);
 PyObject *py_dcerpc_interface_init_helper(PyTypeObject *type, PyObject *args, PyObject *kwargs, const struct ndr_interface_table *table);
 
+PyObject *py_return_ndr_struct(const char *module_name, const char *type_name,
+                              TALLOC_CTX *r_ctx, void *r);
+
 #endif /* __PYRPC_UTIL_H__ */