[lcd4linux @ 2005-05-04 05:22:12 by reinelt]

* replaced fprintf(stderr,...  with error()
* corrected a "dangling reference" memory problem
* removed some PyErr_Print() spam
* fixed a segmentation fault that occured when python module was not
found
* improved error messages

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@538 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
This commit is contained in:
reinelt
2005-05-04 05:22:12 +00:00
parent 854e711689
commit 13238c06a2

View File

@@ -1,4 +1,4 @@
/* $Id: plugin_python.c,v 1.2 2005/05/03 11:13:24 reinelt Exp $
/* $Id: plugin_python.c,v 1.3 2005/05/04 05:22:12 reinelt Exp $
*
* Python plugin
*
@@ -23,6 +23,14 @@
*
*
* $Log: plugin_python.c,v $
* Revision 1.3 2005/05/04 05:22:12 reinelt
* * replaced fprintf(stderr,... with error()
* * corrected a "dangling reference" memory problem
* * removed some PyErr_Print() spam
* * fixed a segmentation fault that occured when python module was not
* found
* * improved error messages
*
* Revision 1.2 2005/05/03 11:13:24 reinelt
* rearranged autoconf a bit,
* libX11 will be linked only if really needed (i.e. when the X11 driver has been selected)
@@ -38,19 +46,12 @@
*
* int plugin_init_python (void)
* adds a python interpreter
*
*
*/
#include <Python.h>
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include "debug.h"
#include "plugin.h"
#include "hash.h"
/*
* Executes a python function specified by function name and module.
@@ -58,10 +59,12 @@
* This method is more or less a copy of an example found in the python
* documentation. Kudos goes to Guido van Rossum and Fred L. Drake.
*
* Returns a char* directly from PyString_AsString() !!! DO NOT DEALLOCATE !!!
* Fixme: this function should be able to accept and receive any types
* of arguments supported by the evaluator. Right now only strings are accepted.
*/
static const char*
pyt_exec_str(const char* module, const char* function, int argc, const char* argv[]) {
static void
pyt_exec_str(RESULT *result, const char* module, const char* function, int argc, const char* argv[]) {
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
@@ -88,8 +91,9 @@ pyt_exec_str(const char* module, const char* function, int argc, const char* arg
if (!pValue) {
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument %s\n", argv[i]);
return NULL;
error("Cannot convert argument \"%s\" to python format", argv[i]);
SetResult(&result, R_STRING, "");
return;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, i, pValue);
@@ -98,40 +102,39 @@ pyt_exec_str(const char* module, const char* function, int argc, const char* arg
Py_DECREF(pArgs);
if (pValue != NULL) {
rv = PyString_AsString(pValue);
//printf("Result of call: %s\n", rv);
SetResult(&result, R_STRING, rv);
Py_DECREF(pValue);
return rv;
/* rv is now a 'dangling reference' */
return;
}
else {
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return NULL;
error("Python call failed (\"%s.%s\")", module, function);
SetResult(&result, R_STRING, "");
return;
}
/* pDict and pFunc are borrowed and must not be Py_DECREF-ed */
}
else {
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
error("Can not find python function \"%s.%s\"", module, function);
}
Py_DECREF(pModule);
}
else {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
return NULL;
error("Failed to load python module \"%s\"", module);
}
return NULL;
SetResult(&result, R_STRING, "");
return;
}
static int python_cleanup_responsibility = 0;
static void my_exec (RESULT *result, RESULT *module, RESULT *function, RESULT *arg )
{
/* Fixme: a plugin should be able to accept any number of arguments, don't know how
to code that (yet) */
const char* args[] = {R2S(arg)};
const char* value = pyt_exec_str(R2S(module),R2S(function),1,args);
SetResult(&result, R_STRING, value);
pyt_exec_str(result, R2S(module), R2S(function), 1, args);
}
int plugin_init_python (void)