diff options
-rw-r--r-- | src/dataconnect.c | 71 | ||||
-rw-r--r-- | src/tester.c | 13 |
2 files changed, 61 insertions, 23 deletions
diff --git a/src/dataconnect.c b/src/dataconnect.c index 63d0e1a..a82004e 100644 --- a/src/dataconnect.c +++ b/src/dataconnect.c @@ -1,6 +1,40 @@ #include "internal.h" #include "dataconnect.h" +/** + * Helper function to convert anytype of Python string (Unicode or not) into a C string. + * The object won't be DECREF'd but the returned char will be a copy of the data. + * The Python Object can then be safely DECREF'd. + */ +static char* pyStringToString(PyObject *obj) +{ + if (!obj) + return NULL; + + if (PyString_Check(obj)) + { + return strdup(PyString_AsString(obj)); + } + else if(PyUnicode_Check(obj)) + { + PyObject *tmp = PyUnicode_AsUTF8String(obj); + char *ret = strdup(PyString_AsString(tmp)); + Py_DECREF(tmp); + return ret; + } + else if (PyBytes_Check(obj)) + { + return strdup(PyBytes_AsString(obj)); + } + + + return NULL; +} + +/** + * All returned values, except for int, must be freed by the application, the library won't manage that. + */ + StringList* portageGetVersions(const char *pkg, int include_masked) { PyObject *obj = executeFunction("portage.api.data_connect", "get_versions", "(zI)", pkg, include_masked); @@ -59,7 +93,7 @@ char* portageBestVersion(StringList *pkgs) return NULL; } - char *ret = strdup(PyString_AsString(obj)); + char *ret = pyStringToString(obj); Py_DECREF(obj); return ret; @@ -69,7 +103,7 @@ char* portageGetBestEbuild(const char *pkg) { assert(pkg); PyObject *obj = executeFunction("portage.api.data_connect", "get_best_ebuild", "(z)", pkg); - if (!obj || !PyUnicode_Check(obj)) + if (!obj) { if (obj) { @@ -78,10 +112,11 @@ char* portageGetBestEbuild(const char *pkg) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); + char *ret = pyStringToString(obj); + //PyObject *tmp = PyUnicode_AsUTF8String(obj); + //char *ret = strdup(PyString_AsString(tmp)); - Py_DECREF(tmp); + //Py_DECREF(tmp); Py_DECREF(obj); return ret; @@ -101,10 +136,8 @@ char* portageGetDepEbuild(const char *pkg) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); + char *ret = pyStringToString(obj); - Py_DECREF(tmp); Py_DECREF(obj); return ret; @@ -145,10 +178,8 @@ char* portageGetMaskingReason(const char *pkg) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); - - Py_DECREF(tmp); + char *ret = pyStringToString(obj); + Py_DECREF(obj); return ret; @@ -188,7 +219,7 @@ char* portageGetPackageSizeString(const char *pkg) return NULL; } - char* ret = strdup(PyString_AsString(obj)); + char *ret = pyStringToString(obj); Py_DECREF(obj); @@ -227,10 +258,8 @@ char* portageGetOverlay(const char *pkg) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); - - Py_DECREF(tmp); + char *ret = pyStringToString(obj); + Py_DECREF(obj); return ret; @@ -249,10 +278,8 @@ char* portageGetOverlayNameFromPath(const char *path) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); + char *ret = pyStringToString(obj); - Py_DECREF(tmp); Py_DECREF(obj); return ret; @@ -274,10 +301,8 @@ char* portageGetOverlayNameFromPkg(const char *pkg) return NULL; } - PyObject *tmp = PyUnicode_AsUTF8String(obj); - char *ret = strdup(PyString_AsString(tmp)); + char *ret = pyStringToString(obj); - Py_DECREF(tmp); Py_DECREF(obj); return ret; diff --git a/src/tester.c b/src/tester.c index fee6ca5..d9fbbc8 100644 --- a/src/tester.c +++ b/src/tester.c @@ -36,6 +36,19 @@ int main(int argc, char *argv[]) portageSettingsFree(ps); + char *bp = portageGetBestEbuild("kde-base/kdelibs"); + printf("kdelibs best package = %s\n", bp); + + char *oname = portageGetOverlay(bp); + printf("kdelibs overlay name = %s\n", oname); + free(oname); + free(bp); + + StringList *resolved = portageGetResolvedPkgs(); + stringListPrint(resolved); + printf("\n"); + stringListFree(resolved); + portageFinalize(); return ret; |