diff python/pydc/pydc.c @ 60:8e905c0798c7

- p2Z() helper func - import fix for test code avoiding potential circular import
author Tassilo Philipp
date Wed, 03 Aug 2022 15:38:07 +0200
parents 2725de59454a
children c5a69c454963
line wrap: on
line diff
--- a/python/pydc/pydc.c	Tue Jun 22 18:49:02 2021 +0200
+++ b/python/pydc/pydc.c	Wed Aug 03 15:38:07 2022 +0200
@@ -733,6 +733,20 @@
 }
 
 
+/* helper creating a string from a pointer handle (Py_ssize_t, must point to
+ * string data); this makes it easier to use C functions that allocate memory,
+ * retrieve the handle via 'p' in order to call free() on it later, and get the
+ * string it points to
+ */
+static PyObject*
+pydc_p2Z(PyObject* self, PyObject* args)
+{
+	size_t p;
+	if(PyArg_ParseTuple(args, "n", &p))
+		return Py_BuildValue("s", (const char*)p);
+
+	Py_RETURN_NONE;
+}
 
 
 // module deinit
@@ -772,6 +786,7 @@
 		{"call",          pydc_call,          METH_VARARGS, "call function"    },
 		{"new_callback",  pydc_new_callback,  METH_VARARGS, "new callback obj" }, // @@@ doc: only functions, not every callable, and only with positional args
 		{"free_callback", pydc_free_callback, METH_VARARGS, "free callback obj"},
+		{"p2Z",           pydc_p2Z,           METH_VARARGS, "ptr to C-string"  }, // helper func
 		{NULL,NULL,0,NULL}
 	};