Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start adding a more useful python module around libpacemaker #3813

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion python/pacemaker/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2023-2024 the Pacemaker project contributors
# Copyright 2023-2025 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
Expand All @@ -9,6 +9,13 @@

include $(top_srcdir)/mk/common.mk

pyexec_LTLIBRARIES = _pcmksupport.la

_pcmksupport_la_SOURCES = pcmksupport.c
_pcmksupport_la_CPPFLAGS = $(PYTHON_CFLAGS)
_pcmksupport_la_LDFLAGS = $(AM_LDFLAGS) -module -avoid-version -export-symbols-regex PyInit__pcmksupport
_pcmksupport_la_LIBADD = $(top_builddir)/lib/pacemaker/libpacemaker.la

pkgpython_PYTHON = __init__.py \
_library.py \
exitstatus.py
Expand Down
82 changes: 82 additions & 0 deletions python/pacemaker/pcmksupport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2025 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
* This source code is licensed under the GNU Lesser General Public License
* version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
*/

#include <Python.h>
#include <libxml/tree.h>

#include <pacemaker.h>

/* This file defines a c-based low level module that wraps libpacemaker
* functions and returns python objects. This is necessary because most
* libpacemaker functions return an xmlNode **, which needs to be coerced
* through the PyCapsule type into something that libxml2's python
* bindings can work with.
*/

/* Base exception class for any errors in the _pcmksupport module */
static PyObject *PacemakerError;

PyMODINIT_FUNC PyInit__pcmksupport(void);

static PyObject *
py_list_standards(PyObject *self, PyObject *args)
{
int rc;
xmlNodePtr xml = NULL;

if (!PyArg_ParseTuple(args, "")) {
return NULL;
}

rc = pcmk_list_standards(&xml);
if (rc != pcmk_rc_ok) {
PyErr_SetString(PacemakerError, pcmk_rc_str(rc));
return NULL;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure what to do here - on the one hand, pcmk_* functions return a standard Pacemaker return code, so that seems like what we should raise. However, we don't expose those return codes in the python module at the moment (which... maybe we should do that?). On the other hand, the returned XML already contains the exit code. However, that's the kind of value a process should return, not a function.

}

return PyCapsule_New(xml, "xmlNodePtr", NULL);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the commit message, I think these functions could almost certainly be auto-generated by a quick script based on a single line description of their name, argument types, and return value.

static PyMethodDef pcmksupportMethods[] = {
{ "list_standards", py_list_standards, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};

static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_pcmksupport",
NULL,
-1,
pcmksupportMethods,
NULL,
NULL,
NULL,
NULL
};

PyMODINIT_FUNC
PyInit__pcmksupport(void)
{
PyObject *module = PyModule_Create(&moduledef);

if (module == NULL) {
return NULL;
}

/* Add the base exception to the module */
PacemakerError = PyErr_NewException("_pcmksupport.PacemakerError", NULL, NULL);

/* FIXME: When we can support Python >= 3.10, we can use PyModule_AddObjectRef */
if (PyModule_AddObject(module, "PacemakerError", PacemakerError) < 0) {
Py_XDECREF(PacemakerError);
return NULL;
}

return module;
}