changeset 33:ba47a3d709d7

- pydc: added support to get libhandle's path
author Tassilo Philipp
date Sat, 11 Apr 2020 20:00:25 +0200
parents 2089026debae
children 2682a627168c
files python/pydc/pydc.py python/pydc/pydcext.c
diffstat 2 files changed, 38 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/python/pydc/pydc.py	Sat Apr 11 18:02:33 2020 +0200
+++ b/python/pydc/pydc.py	Sat Apr 11 20:00:25 2020 +0200
@@ -9,6 +9,9 @@
 def free(libhandle):
   pydcext.free(libhandle)
 
+def get_path(libhandle):
+  return pydcext.get_path(libhandle)
+
 def call(funcptr,signature,*arguments):
   return pydcext.call(funcptr,signature,arguments)
 
--- a/python/pydc/pydcext.c	Sat Apr 11 18:02:33 2020 +0200
+++ b/python/pydc/pydcext.c	Sat Apr 11 20:00:25 2020 +0200
@@ -133,6 +133,36 @@
 	Py_RETURN_NONE;
 }
 
+/* get_path function */
+
+static PyObject*
+pydc_get_path(PyObject* self, PyObject* args)
+{
+	PyObject* pcobj;
+	PyObject* retobj;
+	void* libhandle;
+	char* path;
+	int path_bufSize;
+
+	if (!PyArg_ParseTuple(args, "O", &pcobj))
+		return PyErr_Format(PyExc_RuntimeError, "argument mismatch");
+
+	libhandle = (pcobj == Py_None)?NULL:DcPyCObject_AsVoidPtr(pcobj);
+	path_bufSize = dlGetLibraryPath(libhandle, NULL, 0);
+	if (!path_bufSize)
+		return PyErr_Format(PyExc_RuntimeError, "library path cannot be found");
+
+	path = malloc(path_bufSize);
+	if (path_bufSize != dlGetLibraryPath(libhandle, path, path_bufSize)) {
+		free(path);
+		return PyErr_Format(PyExc_RuntimeError, "library path cannot be queried");
+	}
+
+	retobj = Py_BuildValue("s", path);  // !new ref!  @@@ UTF-8 input...
+	free(path);
+	return retobj;
+}
+
 
 #include "dyncall.h"
 #include "dyncall_signature.h"
@@ -401,10 +431,11 @@
 PY_MOD_INIT_FUNC_NAME(void)
 {
 	static PyMethodDef pydcMethods[] = {
-		{"load", pydc_load, METH_VARARGS, "load library"},
-		{"find", pydc_find, METH_VARARGS, "find symbols"},
-		{"free", pydc_free, METH_VARARGS, "free library"},
-		{"call", pydc_call, METH_VARARGS, "call function"},
+		{"load",     pydc_load,     METH_VARARGS, "load library"    },
+		{"find",     pydc_find,     METH_VARARGS, "find symbols"    },
+		{"free",     pydc_free,     METH_VARARGS, "free library"    },
+		{"get_path", pydc_get_path, METH_VARARGS, "get library path"},
+		{"call",     pydc_call,     METH_VARARGS, "call function"   },
 		{NULL,NULL,0,NULL}
 	};