-
Notifications
You must be signed in to change notification settings - Fork 343
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
clumens
wants to merge
4
commits into
ClusterLabs:main
Choose a base branch
from
clumens:python-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−12
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59ff1d3
Build: configure: Add python libraries and cflags.
clumens 805fb09
Feature: python: Add a low level C-based python module wrapper.
clumens 50a4f6e
Feature: python: Add a python module wrapping libpacemaker.
clumens 9d692bd
Build: Update build process for new python module.
clumens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
return PyCapsule_New(xml, "xmlNodePtr", NULL); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.