From a2c4ca958c73ee870850f2e51d62f76a1453aaeb Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 24 May 2011 19:51:40 +0200 Subject: src/report-python/report.c: Py wrappers around include/report/report.h API Signed-off-by: Denys Vlasenko --- src/report-python/Makefile.am | 1 + src/report-python/README | 44 ++++++++++++++++++++++++++++++ src/report-python/__init__.py | 2 +- src/report-python/common.h | 15 ++++++---- src/report-python/report.c | 59 ++++++++++++++++++++++++++++++++++++++++ src/report-python/reportmodule.c | 33 +++++++++++----------- 6 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 src/report-python/README create mode 100644 src/report-python/report.c diff --git a/src/report-python/Makefile.am b/src/report-python/Makefile.am index c7b2dd67..c1b37596 100644 --- a/src/report-python/Makefile.am +++ b/src/report-python/Makefile.am @@ -11,6 +11,7 @@ _pyreport_la_SOURCES = \ problem_data.c \ dump_dir.c \ run_event.c \ + report.c \ common.h _pyreport_la_CPPFLAGS = \ -I$(srcdir)/../include/report -I$(srcdir)/../include \ diff --git a/src/report-python/README b/src/report-python/README new file mode 100644 index 00000000..1c9c3a27 --- /dev/null +++ b/src/report-python/README @@ -0,0 +1,44 @@ +Currently (2011-05), include/report/*.h are: + +dump_dir.h +event_config.h +problem_data.h +report.h +run_event.h + +and we wrap all of them except event_config.h. + +Python wrappers for C types and functions declared in include/report/FOO.h +should be implemented in corresponding FOO.c file in this directory. + +Their (C-level) declarations should go to common.h. + +Note that methods don't have to be declared in common.h: +they can be static functions inside FOO.c, and exposed to the rest +of the world via PyTypeObject instance. In FOO.c: + +static PyObject *p_method_name(PyObject *pself, PyObject *args) +... +static PyMethodDef p_FOO_methods[] = { +{ "method_name", p_method_name, METH_VARARGS, NULL } +... +}; +PyTypeObject p_FOO_type = { + .tp_methods = p_FOO_methods, +... +}; + +and only p_FOO_type needs to be declared in common.h. + +Similarly, (de)allocators, attr getters/setters also can be static functions +and be hooked into p_FOO_type. + +However, non-method functions can't be static. + + +File reportmodule.c contains the initialization function which should +initialize types (p_FOO_type objects) and hook up finctions from every +FOO.c so that they are usable from python code. + +Python wrappers for C constants (enums, defines) are created directly +by reportmodule.c. diff --git a/src/report-python/__init__.py b/src/report-python/__init__.py index 23c8becb..78beff6d 100644 --- a/src/report-python/__init__.py +++ b/src/report-python/__init__.py @@ -127,4 +127,4 @@ def report(cd, io_unused): """ def report(pd, io_unused): - result = report_problem_data(pd) + result = report_problem(pd) diff --git a/src/report-python/common.h b/src/report-python/common.h index ee56ad9a..713aa2f2 100644 --- a/src/report-python/common.h +++ b/src/report-python/common.h @@ -31,11 +31,6 @@ extern PyTypeObject p_problem_data_type; extern PyTypeObject p_dump_dir_type; extern PyTypeObject p_run_event_state_type; -/* module-level functions */ -PyObject *p_dd_opendir(PyObject *module, PyObject *args); -PyObject *p_dd_create(PyObject *module, PyObject *args); -PyObject *p_delete_dump_dir(PyObject *pself, PyObject *args); - /* python objects' struct defs */ typedef struct { PyObject_HEAD @@ -46,3 +41,13 @@ typedef struct { PyObject_HEAD problem_data_t *cd; } p_problem_data; + +/* module-level functions */ +/* for include/report/dump_dir.h */ +PyObject *p_dd_opendir(PyObject *module, PyObject *args); +PyObject *p_dd_create(PyObject *module, PyObject *args); +PyObject *p_delete_dump_dir(PyObject *pself, PyObject *args); +/* for include/report/report.h */ +PyObject *p_report_problem_in_dir(PyObject *pself, PyObject *args); +PyObject *p_report_problem_in_memory(PyObject *pself, PyObject *args); +PyObject *p_report_problem(PyObject *pself, PyObject *args); diff --git a/src/report-python/report.c b/src/report-python/report.c new file mode 100644 index 00000000..0bdd4076 --- /dev/null +++ b/src/report-python/report.c @@ -0,0 +1,59 @@ +/* + Copyright (C) 2010 Abrt team. + Copyright (C) 2010 RedHat inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include + +#include "common.h" + +/* C: int report_problem_in_dir(const char *dirname, int flags); */ +PyObject *p_report_problem_in_dir(PyObject *pself, PyObject *args) +{ + const char *dirname; + int flags; + if (!PyArg_ParseTuple(args, "si", &dirname, &flags)) + { + return NULL; + } + int r = report_problem_in_dir(dirname, flags); + return Py_BuildValue("i", r); +} + +/* C: int report_problem_in_memory(problem_data_t *pd, int flags); */ +PyObject *p_report_problem_in_memory(PyObject *pself, PyObject *args) +{ + p_problem_data *pd; + int flags; + if (!PyArg_ParseTuple(args, "O!i", &p_problem_data_type, &pd, &flags)) + { + return NULL; + } + int r = report_problem_in_memory(pd->cd, flags); + return Py_BuildValue("i", r); +} + +/* C: int report_problem(problem_data_t *pd); */ +PyObject *p_report_problem(PyObject *pself, PyObject *args) +{ + p_problem_data *pd; + if (!PyArg_ParseTuple(args, "O!", &p_problem_data_type, &pd)) + { + return NULL; + } + int r = report_problem(pd->cd); + return Py_BuildValue("i", r); +} diff --git a/src/report-python/reportmodule.c b/src/report-python/reportmodule.c index 1cae783f..539c66aa 100644 --- a/src/report-python/reportmodule.c +++ b/src/report-python/reportmodule.c @@ -22,21 +22,16 @@ PyObject *ReportError; - -static PyObject *p_report(PyObject *pself, PyObject *problem_data) -{ - p_problem_data *p_pd = (p_problem_data*)problem_data; - report(p_pd->cd); - //FIXME return status as integer object - Py_RETURN_NONE; -} - static PyMethodDef module_methods[] = { /* method_name, func, flags, doc_string */ - { "dd_opendir" , p_dd_opendir , METH_VARARGS }, - { "dd_create" , p_dd_create , METH_VARARGS }, - { "delete_dump_dir", p_delete_dump_dir, METH_VARARGS }, - { "report_problem_data" , p_report, METH_O}, + /* for include/report/dump_dir.h */ + { "dd_opendir" , p_dd_opendir , METH_VARARGS }, + { "dd_create" , p_dd_create , METH_VARARGS }, + { "delete_dump_dir" , p_delete_dump_dir , METH_VARARGS }, + /* for include/report/report.h */ + { "report_problem_in_dir" , p_report_problem_in_dir , METH_VARARGS }, + { "report_problem_in_memory" , p_report_problem_in_memory, METH_VARARGS }, + { "report_problem" , p_report_problem , METH_VARARGS }, { NULL } }; @@ -75,20 +70,26 @@ init_pyreport(void) Py_INCREF(ReportError); PyModule_AddObject(m, "error", ReportError); - /* init type objects */ + /* init type objects and constants */ + /* for include/report/problem_data.h */ Py_INCREF(&p_problem_data_type); PyModule_AddObject(m, "problem_data", (PyObject *)&p_problem_data_type); PyModule_AddObject(m, "CD_FLAG_BIN" , Py_BuildValue("i", CD_FLAG_BIN )); PyModule_AddObject(m, "CD_FLAG_TXT" , Py_BuildValue("i", CD_FLAG_TXT )); PyModule_AddObject(m, "CD_FLAG_ISEDITABLE" , Py_BuildValue("i", CD_FLAG_ISEDITABLE )); PyModule_AddObject(m, "CD_FLAG_ISNOTEDITABLE", Py_BuildValue("i", CD_FLAG_ISNOTEDITABLE)); - + /* for include/report/dump_dir.h */ Py_INCREF(&p_dump_dir_type); PyModule_AddObject(m, "dump_dir", (PyObject *)&p_dump_dir_type); PyModule_AddObject(m, "DD_FAIL_QUIETLY_ENOENT" , Py_BuildValue("i", DD_FAIL_QUIETLY_ENOENT )); PyModule_AddObject(m, "DD_FAIL_QUIETLY_EACCES" , Py_BuildValue("i", DD_FAIL_QUIETLY_EACCES )); PyModule_AddObject(m, "DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE", Py_BuildValue("i", DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE)); - + /* for include/report/run_event.h */ Py_INCREF(&p_run_event_state_type); PyModule_AddObject(m, "run_event_state", (PyObject *)&p_run_event_state_type); + /* for include/report/report.h */ + PyModule_AddObject(m, "LIBREPORT_NOWAIT" , Py_BuildValue("i", LIBREPORT_NOWAIT )); + PyModule_AddObject(m, "LIBREPORT_WAIT" , Py_BuildValue("i", LIBREPORT_WAIT )); + PyModule_AddObject(m, "LIBREPORT_ANALYZE" , Py_BuildValue("i", LIBREPORT_ANALYZE )); + PyModule_AddObject(m, "LIBREPORT_RELOAD_DATA", Py_BuildValue("i", LIBREPORT_RELOAD_DATA)); } -- cgit