From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1RBYYp-0003t1-8i for garchives@archives.gentoo.org; Wed, 05 Oct 2011 20:59:27 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6C2CE21C189; Wed, 5 Oct 2011 20:56:52 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 2EC6121C189 for ; Wed, 5 Oct 2011 20:56:52 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 890E61B4045 for ; Wed, 5 Oct 2011 20:56:51 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id ABD0B8004B for ; Wed, 5 Oct 2011 20:56:50 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <40928302b905a329555de0fde1d592463049b97c.dol-sen@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: src/ X-VCS-Repository: proj/layman X-VCS-Files: src/interpreter.c X-VCS-Directories: src/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: 40928302b905a329555de0fde1d592463049b97c Date: Wed, 5 Oct 2011 20:56:50 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 7418a906751bdd1d2a1cf0060217c245 commit: 40928302b905a329555de0fde1d592463049b97c Author: Detlev Casanova gmail com> AuthorDate: Tue Jul 6 15:57:13 2010 +0000 Commit: Brian Dolbec gmail com> CommitDate: Tue Jul 6 15:57:13 2010 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/layman.git;a=3D= commit;h=3D40928302 Add examples for method execution from an instance of an object --- src/interpreter.c | 135 +++++++++++++++++++++++------------------------= ----- 1 files changed, 60 insertions(+), 75 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index dd0ebd3..5d5bca5 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -40,28 +40,12 @@ void insert(PyObjectList* list, PyObject *object) list->count++; } =20 -PyObject *elementAt(PyObjectList* list, int pos) -{ - if (!list || list->count < pos) - return NULL; - - PyObjectListElem *node =3D list->root; - for (int i =3D 0; i < pos; i++) - { - node =3D node->next; - } - if (!node) - return 0; - - return node->object; -} - PyObject *moduleNamed(const char *name, PyObjectList *list) { PyObjectListElem *node =3D list->root; while (node) { - if (strcmp(PyModule_GetName(node->object), name)) + if (strcmp(PyModule_GetName(node->object), name) =3D=3D 0) return node->object; node =3D node->next; } @@ -69,24 +53,6 @@ PyObject *moduleNamed(const char *name, PyObjectList *= list) return NULL; } =20 -PyObject *toTuple(PyObjectList *list) -{ - if (!list) - return NULL; - - PyObject *ret =3D PyTuple_New(list->count); - PyObjectListElem *node =3D list->root; - int i =3D 0; - while (node) - { - if (node->object) - PyTuple_SetItem(ret, i++, node->object); - node =3D node->next; - } - - return ret; -} - int listCount(PyObjectList *list) { return (list ? list->count : 0); @@ -112,9 +78,7 @@ void freeList(PyObjectList *list, int deref) =20 /* * Interpreter - */ - -/* + * * A Python interpreter object keeps the context like the loaded modules= . */ =20 @@ -145,8 +109,9 @@ void freeInterpreter(Interpreter *inter) * @param interpreter Python interpreter object on which the function sh= ould be ran * @param module name of the python module in which the function is * @param funcName the function name to call - * @param arg_types printf() like list of arguments TODO:explain more --= > See Python documentation + * @param arg_types printf() like list of arguments. See Python document= ation * @param ... arguments for the function + * FIXME:why are default arguments necessary ? */ =20 PyObject *executeFunction(Interpreter *interpreter, const char *module, = const char *funcName, const char* format, ...) @@ -154,38 +119,17 @@ PyObject *executeFunction(Interpreter *interpreter,= const char *module, const ch if (!Py_IsInitialized()) Py_Initialize(); =20 - // Make arguments - // FIXME: use Py_BuildValue. - // FIXME: is it possible to pass this function's arguments to another f= unction ? - PyObjectList *argList =3D createObjectList(); - int i =3D 0; - while (format[i] !=3D '\0') + // Make argument list + PyObject *args; + if (format =3D=3D NULL) + args =3D Py_None; + else { - while(format[i] !=3D '%' && format[i] !=3D '\0') - i++; - =09 - if (format[i] =3D=3D '\0') - break; - - PyObject *arg =3D NULL; - switch(format[++i]) - { - case 'd': //number - break; - case 's': //string - break; - case 'P': //PyObject - break; - default: //skip or abort ? - break; - } - - insert(argList, arg); - i++; - } + va_list listArgs; + va_start(listArgs, format); =20 - PyObject *args =3D toTuple(argList); - freeList(argList, 1); + args =3D Py_VaBuildValue(format, listArgs); + } =20 // Look for the module. PyObject *mod =3D 0; @@ -196,36 +140,77 @@ PyObject *executeFunction(Interpreter *interpreter,= const char *module, const ch if (!mod) { mod =3D PyImport_ImportModule(module); + if (!mod) + return NULL; insert(interpreter->modules, mod); } =20 - // Look for the function - PyObject *dict =3D PyModule_GetDict(mod); - PyObject *func =3D PyDict_GetItemString(dict, funcName); + /*printf("mod: %p ", mod); + PyObject_Print(mod, stdout, 0); + printf("\n");*/ =20 + // Look for the function + PyObject *func =3D PyObject_GetAttrString(mod, funcName); if (!PyCallable_Check(func)) return NULL; =20 // Call the function - PyObject *val =3D PyObject_CallObject(func, args); + /*printf("func: %p\n", func); + PyObject_Print(func, stdout, 0); + printf("\n");*/ + + PyObject *val =3D PyObject_Call(func, args, NULL); =20 // Add return value object in a local list so it can be DECREF'ed when = the interpreter is deleted. // TODO - Py_DECREF(args); =20 return val; } +/* +PyObject *executeMethod(PyObject *object, const char *methName, const ch= ar* format, ...) +{ + if (!Py_IsInitialized()) + Py_Initialize(); + + // Make argument list + PyObject *args; + if (format =3D=3D NULL) + args =3D Py_None; + else + { + va_list listArgs; + va_start(listArgs, format); =20 + args =3D Py_VaBuildValue(format, listArgs); + } + + PyObject *ret =3D PyObject_CallMethod(object, methName, ) +}*/ =20 int main(int argc, char *argv[]) { Interpreter *in =3D createInterpreter(); =20 - PyObject *ret =3D executeFunction(in, "portage.data", "portage_group_wa= rning", ""); + executeFunction(in, "portage.data", "portage_group_warning", NULL); + + PyObject *arg1 =3D executeFunction(in, "portage", "pkgsplit", "sI", "ap= p-portage/kuroo4-4.2", 1); + PyObject *arg2 =3D executeFunction(in, "portage", "pkgsplit", "sI", "ap= p-portage/kuroo4-4.1", 1); + PyObject *ret =3D executeFunction(in, "portage", "pkgcmp", "OO", arg1, = arg2); =20 if (!ret) printf("failed :-( \n"); + else + printf("Return %ld\n", PyLong_AsLong(ret)); + + Py_DECREF(ret); + Py_DECREF(arg1); + Py_DECREF(arg2); +=09 + PyObject *tbz =3D executeFunction(in, "portage.output", "ProgressBar", = "sIs", "titre", 100, "status"); + ret =3D PyObject_CallMethod(tbz, "inc", "I", 25); + Py_DECREF(tbz); + Py_DECREF(ret); =20 freeInterpreter(in); =20