2#include <structmember.h>
14int _init(PyObject* self, PyObject *args, PyObject *kwds) {
15 PyCoefficient* py_coeff =
reinterpret_cast<PyCoefficient*
>(self);
17 if (py_coeff == NULL) {
18 PyErr_SetString(PyExc_TypeError,
19 "Failed to resolve self as PolynomialCoefficient in __init__");
24 if (py_coeff->coeff != NULL) {
25 delete py_coeff->coeff;
26 py_coeff->coeff = NULL;
33 static char *kwlist[] = {
const_cast<char*
>(
"index_by_vector"),
34 const_cast<char*
>(
"output_axis"),
35 const_cast<char*
>(
"coefficient_value"),
37 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"Oid", kwlist,
38 &py_index, &value_axis, &coefficient)) {
44 if (!PyList_Check(py_index)) {
45 PyErr_SetString(PyExc_TypeError,
46 "Failed to resolve index as a list");
49 size_t list_size = PyList_Size(py_index);
50 std::vector<int> index(list_size);
52 for (
size_t i = 0; i < list_size; ++i) {
53 PyObject* py_value = PyList_GetItem(py_index, i);
54 index[i] = int(PyLong_AsLong(py_value));
55 if (PyErr_Occurred() != NULL) {
62 }
catch (std::exception& exc) {
63 PyErr_SetString(PyExc_RuntimeError, (&exc)->what());
69PyObject *
_alloc(PyTypeObject *
type, Py_ssize_t nitems) {
70 void* void_coeff = malloc(
sizeof(PyCoefficient));
71 PyCoefficient* coeff =
reinterpret_cast<PyCoefficient*
>(void_coeff);
74 Py_TYPE(coeff) =
type;
75 return reinterpret_cast<PyObject*
>(coeff);
78PyObject *
_new(PyTypeObject *
type, Py_ssize_t nitems) {
82void _free(PyCoefficient * self) {
84 if (self->coeff != NULL)
94static PyMemberDef _members[] = {
98static PyMethodDef _methods[] = {
103std::string(
"PolynomialCoefficient docstring\n");
105static PyTypeObject PyCoefficientType = {
106 PyObject_HEAD_INIT(NULL)
107 "polynomial_coefficient.PolynomialCoefficient",
108 sizeof(PyCoefficient),
125 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
150 "polynomial_coefficient module contains the PolynomialCoefficient class";
152static struct PyModuleDef polynomial_coefficient_def = {
153 PyModuleDef_HEAD_INIT,
154 "polynomial_coefficient",
166 PyPolynomialCoefficient::PyCoefficientType.tp_new = PyType_GenericNew;
167 if (PyType_Ready(&PyPolynomialCoefficient::PyCoefficientType) < 0)
170 PyObject* module = PyModule_Create(&polynomial_coefficient_def);
174 PyTypeObject* polynomial_coeff_type =
175 &PyPolynomialCoefficient::PyCoefficientType;
176 Py_INCREF(polynomial_coeff_type);
177 PyModule_AddObject(module,
"PolynomialCoefficient",
178 reinterpret_cast<PyObject*
>(polynomial_coeff_type));
int _init(PyObject *self, PyObject *args, PyObject *kwds)
PyObject * _alloc(PyTypeObject *type, Py_ssize_t nitems)
std::string class_docstring
void _free(PyCoefficient *self)
void _dealloc(PyCoefficient *self)
PyObject * _new(PyTypeObject *type, Py_ssize_t nitems)
boost::function< boost::tuple< double, bool >(arguments_t)> type
PolynomialCoefficient represents a coefficient in a multi-dimensional polynomial.
PyMODINIT_FUNC PyInit_polynomial_coefficient(void)
const char * module_docstring