-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (84 loc) · 2.2 KB
/
main.cpp
File metadata and controls
98 lines (84 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <Python.h>
#include <iostream>
int sym_ex(const char *input_expression, const char *output_expression)
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
const char *file = "sympy";
const char *function = "simplify";
Py_Initialize();
// PyRun_SimpleString("from sympy import *\n");
pName = PyUnicode_FromString(file);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL)
{
pFunc = PyObject_GetAttrString(pModule, function);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc))
{
pArgs = PyTuple_New(1);
pValue = PyUnicode_FromString(input_expression);
printf("the string passed %s\n", input_expression);
if (!pValue)
{
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL)
{
std::clog << PyLong_AsLong(pValue) << std::endl;
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr, "Call failed\n");
return 1;
}
}
else
{
if (PyErr_Occurred()) PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", function);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else
{
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", file);
return 1;
}
Py_Finalize();
return 0;
}
int main(int argc, char *argv[])
{
wchar_t *name = L"ProgramName";
Py_SetProgramName(name); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");
PyRun_SimpleString("print(sys.version)");
Py_Finalize();
/// uncomment if you want to test importing sympy
/// Note for running this code you need to install sympy in python that
/// library is using you can get that version by running top code
// *************************************** using sympy
// char *input_expression = "2 + 2";
// char *output_expression = new char[250];
// sym_ex(input_expression, output_expression);
return 0;
}