diff options
author | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-19 11:59:44 +0200 |
---|---|---|
committer | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-19 11:59:44 +0200 |
commit | 82813ef426d1a17df69e4daf91c7fb862e4ab26c (patch) | |
tree | b594867f80e23d30e4c713c80cc3d6504c2f1415 | |
parent | 181ddc8237bb313b61073e1e97a2e7f4b097a608 (diff) | |
download | layman-82813ef426d1a17df69e4daf91c7fb862e4ab26c.tar.gz layman-82813ef426d1a17df69e4daf91c7fb862e4ab26c.tar.bz2 layman-82813ef426d1a17df69e4daf91c7fb862e4ab26c.zip |
Fix PyObject_IsTrue calls
-rw-r--r-- | src/config.c | 5 | ||||
-rw-r--r-- | src/laymanapi.c | 51 | ||||
-rw-r--r-- | src/stringlist.c | 30 | ||||
-rw-r--r-- | src/tester.c | 17 |
4 files changed, 68 insertions, 35 deletions
diff --git a/src/config.c b/src/config.c index c0b5b79..0bb330a 100644 --- a/src/config.c +++ b/src/config.c @@ -3,6 +3,8 @@ #include "config.h" #include "internal.h" +#define debug(x) printf(x) + struct BareConfig { PyObject *object; @@ -38,7 +40,10 @@ BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) Py_DECREF(pyerr); if (!obj) + { + debug("The execution failed, Are you sure app-portage/layman-8888 is properly installed ?\n"); return NULL; + } BareConfig *ret = malloc(sizeof(BareConfig)); ret->object = obj; diff --git a/src/laymanapi.c b/src/laymanapi.c index 74e98bf..6ddbba7 100644 --- a/src/laymanapi.c +++ b/src/laymanapi.c @@ -2,7 +2,7 @@ #include "internal.h" #include "laymanapi.h" -int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *results, const char *overlay); +static int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *results, const char *overlay); struct LaymanAPI { @@ -16,8 +16,13 @@ struct LaymanAPI */ LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output) { - assert(NULL != config); - PyObject *obj = executeFunction("layman.api", "LaymanAPI", "Oii", _bareConfigObject(config), report_error, output); + PyObject *cfg; + if (!config) + cfg = Py_None; + else + cfg = _bareConfigObject(config); + + PyObject *obj = executeFunction("layman.api", "LaymanAPI", "Oii", cfg, report_error, output); if (!obj) return NULL; @@ -42,7 +47,7 @@ int laymanAPIIsRepo(LaymanAPI *l, const char* repo) Py_DECREF(obj); - return !ret; + return ret; } int laymanAPIIsInstalled(LaymanAPI *l, const char* repo) @@ -60,7 +65,7 @@ int laymanAPIIsInstalled(LaymanAPI *l, const char* repo) Py_DECREF(obj); - return !ret; + return ret; } /* @@ -114,13 +119,13 @@ int laymanAPISync(LaymanAPI* l, const char* overlay, int verbose) return 0; int ret = PyObject_IsTrue(obj); - + // ret must be 1 or 0 assert(-1 != ret); - + Py_DECREF(obj); - - return !ret; + + return ret; } /* @@ -141,7 +146,7 @@ int laymanAPIFetchRemoteList(LaymanAPI* l) Py_DECREF(obj); - return !ret; + return ret; } /* @@ -207,9 +212,9 @@ int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* res assert(NULL != tmp); results[k].text = strdup(tmp); - results[k].official = !PyObject_IsTrue(official); + results[k].official = PyObject_IsTrue(official); assert(-1 != results[k].official); - results[k].supported = !PyObject_IsTrue(supported); + results[k].supported = PyObject_IsTrue(supported); assert(-1 != results[k].supported); k++; @@ -381,9 +386,9 @@ int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *resul results[k].srcUris = listToCList(srcUris); // If official or supported is neither True or False, abort. - results[k].official = !PyObject_IsTrue(official); + results[k].official = PyObject_IsTrue(official); assert(-1 != results[k].official); - results[k].supported = !PyObject_IsTrue(supported); + results[k].supported = PyObject_IsTrue(supported); assert(-1 != results[k].supported); k++; @@ -408,14 +413,14 @@ int laymanAPIAddRepo(LaymanAPI* l, const char *repo) // Call the method PyObject *obj = PyObject_CallMethod(l->object, "delete_repos", "(s)", repo); - + // If the call returned NULL, it failed. int ret; if (!obj) ret = 0; else ret = 1; - + Py_DECREF(obj); return ret; @@ -437,14 +442,14 @@ int laymanAPIAddRepoList(LaymanAPI* l, StringList *repos) // Call the method PyObject *obj = PyObject_CallMethod(l->object, "add_repos", "(O)", pyrepos); Py_DECREF(pyrepos); - + // If the call returned NULL, it failed. int ret; if (!obj) ret = 0; else ret = 1; - + Py_DECREF(obj); return ret; @@ -462,14 +467,14 @@ int laymanAPIDeleteRepo(LaymanAPI* l, const char *repo) // Call the method PyObject *obj = PyObject_CallMethod(l->object, "delete_repos", "(s)", repo); - + // If the call returned NULL, it failed. int ret; if (!obj) ret = 0; else ret = 1; - + Py_DECREF(obj); return ret; @@ -487,18 +492,18 @@ int laymanAPIDeleteRepoList(LaymanAPI* l, StringList *repos) // Converting the C list to a python list PyObject *pyrepos = cListToPyList(repos); - + // Call the method PyObject *obj = PyObject_CallMethod(l->object, "delete_repos", "(O)", pyrepos); Py_DECREF(pyrepos); - + // If the call returned NULL, it failed. int ret; if (!obj) ret = 0; else ret = 1; - + Py_DECREF(obj); return ret; diff --git a/src/stringlist.c b/src/stringlist.c index 259eabb..915cca4 100644 --- a/src/stringlist.c +++ b/src/stringlist.c @@ -8,6 +8,8 @@ struct StringList unsigned int count; }; +// Creates a String list to use with the library. +// len is the number of strings in the list. StringList* stringListCreate(size_t len) { StringList *ret = malloc(sizeof(StringList)); @@ -17,6 +19,10 @@ StringList* stringListCreate(size_t len) return ret; } +/* + * Inserts the string str in the list l at position pos. + * Return True if it succeeded, False if not. + */ int stringListInsertAt(StringList *l, unsigned int pos, char *str) { if(!l || !l->list || l->count < pos) @@ -27,6 +33,9 @@ int stringListInsertAt(StringList *l, unsigned int pos, char *str) return 1; } +/* + * Returns the number of strings in the list + */ unsigned int stringListCount(StringList *l) { if (!l) @@ -34,6 +43,9 @@ unsigned int stringListCount(StringList *l) return l->count; } +/* + * Returns the String at position pos + */ char* stringListGetAt(StringList *l, unsigned int pos) { if (!l || !l->list || pos >= l->count) @@ -42,6 +54,9 @@ char* stringListGetAt(StringList *l, unsigned int pos) return l->list[pos]; } +/* + * Converts a Python list object to a C String list + */ StringList* listToCList(PyObject* list) { if (!list || !PyList_Check(list)) @@ -54,6 +69,8 @@ StringList* listToCList(PyObject* list) for (unsigned int i = 0; i < len; i++) { + //Item are copied so that the PyObject can be deleted after the call without + //destroying the data in the returned list. PyObject *elem = PyList_GetItem(list, i); ret->list[i] = malloc(sizeof(char) * (PyBytes_Size(elem) + 1)); strcpy(ret->list[i], PyBytes_AsString(elem)); @@ -62,6 +79,9 @@ StringList* listToCList(PyObject* list) return ret; } +/* + * Converts a C String list to a Python List object + */ PyObject* cListToPyList(StringList* list) { if (!list) @@ -76,6 +96,9 @@ PyObject* cListToPyList(StringList* list) return ret; } +/* + * Prints a C String list. + */ void stringListPrint(StringList* list) { if (!list) @@ -84,16 +107,17 @@ void stringListPrint(StringList* list) for(unsigned int i = 0; i < list->count; i++) { printf("\"%s\"", list->list[i]); + // No coma after the last item. if (i < list->count - 1) printf(", "); } } +/* + * Frees a string list and it's data + */ void stringListFree(StringList* list) { - if (!list) - return; - if (list && list->list) { for(unsigned int i = 0; i < list->count; i++) diff --git a/src/tester.c b/src/tester.c index 049cc61..98dc171 100644 --- a/src/tester.c +++ b/src/tester.c @@ -22,24 +22,23 @@ int main(int argc, char *argv[]) printf("local_list: %s\n", bareConfigGetOptionValue(cfg, "local_list"));*/ LaymanAPI *l = laymanAPICreate(cfg, 0, 0); - /*if (0 == laymanAPIFetchRemoteList(l)) + if (0 == laymanAPIFetchRemoteList(l)) { printf("Unable to fetch the remote list.\n"); ret = -1; - goto finish; - }*/ + } StringList *strs = laymanAPIGetAvailable(l, 0); printf("list:\n"); stringListPrint(strs); - + printf("\n"); unsigned int len = stringListCount(strs); //OverlayInfo *infos = calloc(len, sizeof(OverlayInfo)); //int count = laymanAPIGetAllInfos(l, strs, infos); - OverlayInfo *oi = laymanAPIGetAllInfo(l, "enlfdsightenment"); + OverlayInfo *oi = laymanAPIGetAllInfo(l, "kuroo"); if (oi) { printf("%s\n~~~~~~~~~~~~~~~~~~~~\n", oi->name); @@ -47,8 +46,8 @@ int main(int argc, char *argv[]) overlayInfoFree(*oi); free(oi); } - - for (unsigned int i = 0; i < len; i++) + + /*for (unsigned int i = 0; i < len; i++) { OverlayInfo *oi = laymanAPIGetAllInfo(l, stringListGetAt(strs, i)); if (!oi) @@ -57,15 +56,15 @@ int main(int argc, char *argv[]) printf("%s\n\n", oi->description); overlayInfoFree(*oi); free(oi); - } + }*/ printf("\n"); //free(infos); + stringListFree(strs); bareConfigFree(cfg); laymanAPIFree(l); - stringListFree(strs); interpreterFinalize(); |