OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
TestingPythonExtensions.h
Go to the documentation of this file.
1#pragma once
2#pragma once
3#include <Python.h>
5
7{
8
9 static PyObject* GetFunction(PyObject* self, PyObject* args)
10 {
11 PythonObjectBuilder pyObBuilder;
12 std::string scriptName = pyObBuilder.getStringValue(PyTuple_GetItem(args, 0), "Parameter 1");
13 std::string functionName = pyObBuilder.getStringValue(PyTuple_GetItem(args, 1), "Parameter 2");
14 PyObject* module = PyImport_ImportModule(scriptName.c_str());
15 assert(module != nullptr);
16 PyObject* function = PyObject_GetAttrString(module, functionName.c_str());
17 assert(function != nullptr);
18 return function;
19 }
20
21 static PyObject* WithOneParameter(PyObject* self, PyObject* args)
22 {
23 int command;
24 if (!PyArg_ParseTuple(args, "i", &command))
25 {
26 return NULL;
27 }
28 return PyLong_FromLong(command + 13);
29 }
30
31 static PyObject* WithoutParameter(PyObject* self, PyObject* args)
32 {
33 return PyUnicode_FromString("Hello from the extension");
34 }
35
36 static PyObject* WithMultipleParameter(PyObject* self, PyObject* args)
37 {
38 PythonObjectBuilder pyObBuilder;
39 CPythonObjectBorrowed param1(PyTuple_GetItem(args, 0));
40 int parameter1 = pyObBuilder.getInt32Value(param1, "Parameter 1");
41 CPythonObjectBorrowed param2(PyTuple_GetItem(args, 1));
42 int parameter2 = pyObBuilder.getInt32Value(param2,"Parameter 2");
43 CPythonObjectBorrowed param3(PyTuple_GetItem(args, 2));
44 std::string parameter3 = pyObBuilder.getStringValue(param3,"Parameter 3");
45
46 int result = (parameter1 + parameter2) * 2;
47 if (parameter3 == "Suppe")
48 {
49 result += 7;
50 }
51
52 return pyObBuilder.setInt32(result);
53 }
54
55 static PyMethodDef OTMethods[] = {
56
57 {"WithOneParameter", WithOneParameter, METH_VARARGS, "Test function with one parameter."},
58 {"WithoutParameter", WithoutParameter, METH_NOARGS, "Test function without parameter."},
59 {"WithMultipleParameter", WithMultipleParameter, METH_VARARGS, "Test function with multiple parameter."},
60 {"GetFunction", GetFunction, METH_VARARGS, "Test function getter."},
61
62 {NULL, NULL, 0, NULL} /* Sentinel */
63 };
64
65 static std::string testModuleDescription = "This module holds functions that are used for unit tests.";
66
67 static struct PyModuleDef TestModule = {
68 PyModuleDef_HEAD_INIT,
69 "InitialTestModule", /* name of module */
70 testModuleDescription.c_str(), /* module documentation, may be NULL */
71 -1, /* size of per-interpreter state of the module,
72 or -1 if the module keeps state in global variables. */
74 };
75
76 PyMODINIT_FUNC
78 {
79 return PyModule_Create(&TestModule);
80 }
81
82};
Builder to transfer typically used PyObjects to c++ types and vice versa.
int result
Definition dllmain.cpp:82
Definition CPythonObjectBorrowed.h:15
PyObjects don't distingiush between float and double and int32 and int64. All floating point values a...
Definition PythonObjectBuilder.h:28
int32_t getInt32Value(const CPythonObject &pValue, const std::string &varName)
Definition PythonObjectBuilder.cpp:173
std::string getStringValue(const CPythonObject &pValue, const std::string &varName)
Definition PythonObjectBuilder.cpp:191
CPythonObjectNew setInt32(const int32_t value)
Definition PythonObjectBuilder.cpp:507
Definition TestingPythonExtensions.h:7
static PyObject * WithMultipleParameter(PyObject *self, PyObject *args)
Definition TestingPythonExtensions.h:36
static std::string testModuleDescription
Definition TestingPythonExtensions.h:65
static PyMethodDef OTMethods[]
Definition TestingPythonExtensions.h:55
static PyObject * WithoutParameter(PyObject *self, PyObject *args)
Definition TestingPythonExtensions.h:31
PyMODINIT_FUNC PyInit_Testing(void)
Definition TestingPythonExtensions.h:77
static struct PyModuleDef TestModule
Definition TestingPythonExtensions.h:67
static PyObject * WithOneParameter(PyObject *self, PyObject *args)
Definition TestingPythonExtensions.h:21
static PyObject * GetFunction(PyObject *self, PyObject *args)
Definition TestingPythonExtensions.h:9