-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathccpy.c
More file actions
127 lines (110 loc) · 3.19 KB
/
Copy pathccpy.c
File metadata and controls
127 lines (110 loc) · 3.19 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Created by yanwei on 18/08/2017.
*/
#include <Python.h>
#include "ccpy.h"
#ifdef USE_PYTHON_3
int CallPythonInit(const char *main_program_path, const char *python_path) {
#else
int CallPythonInit(char *main_program_path, const char *python_path) {
#endif
setenv("PYTHONPATH", python_path, 1);
#ifdef USE_PYTHON_3
wchar_t *program = Py_DecodeLocale(main_program_path, NULL);
if (program == NULL)
return CCPY_ERR_FAIL;
Py_SetProgramName(program);
PyMem_RawFree(program);
#else
Py_SetProgramName(main_program_path);
#endif
return CCPY_ERR_SUCCESS;
}
int CallPythonFile(const char *py_file_path) {
Py_Initialize();
if (!Py_IsInitialized())
return CCPY_ERR_FAIL;
FILE *fp = fopen(py_file_path, "r");
if (fp == NULL) {
Py_Finalize();
return CCPY_ERR_FILE_NOT_FOUND;
}
int iRet = PyRun_SimpleFile(fp, py_file_path);
fclose(fp);
Py_Finalize();
if (iRet == 0)
return CCPY_ERR_SUCCESS;
else
return CCPY_ERR_FAIL;
}
int CallPythonFunc(const char *py_file_path, const char *func_name, const char *input, char output[CCPY_MAX_BUFSIZE]) {
#ifdef USE_PYTHON_3
PyObject *pName;
#endif
PyObject *pModule, *pFunc;
PyObject *pArgs, *pValue;
Py_Initialize();
if (!Py_IsInitialized())
return CCPY_ERR_FAIL;
#ifdef USE_PYTHON_3
pName = PyUnicode_FromString(py_file_path);
if (pName == NULL) {
Py_Finalize();
return CCPY_ERR_FAIL;
}
pModule = PyImport_Import(pName);
Py_XDECREF(pName);
#else
pModule = PyImport_ImportModule(py_file_path);
#endif
if (pModule != NULL) {
pFunc = PyObject_GetAttrString(pModule, func_name);
if (pFunc && PyCallable_Check(pFunc)) {
if (input) {
pArgs = PyTuple_New(1);
#ifdef USE_PYTHON_3
pValue = PyUnicode_FromString(input);
#else
pValue = PyString_FromString(input);
#endif
PyTuple_SetItem(pArgs, 0, pValue);
} else
pArgs = NULL;
pValue = PyObject_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
if (pValue != NULL) {
if (output) {
#ifdef USE_PYTHON_3
PyObject *pyStr = PyUnicode_AsASCIIString(pValue);
const char *c_str = PyBytes_AS_STRING(pyStr);
strncpy(output, c_str, CCPY_MAX_BUFSIZE - 1);
Py_XDECREF(pyStr);
#else
const char *c_str = PyString_AsString(pValue);
strncpy(output, c_str, CCPY_MAX_BUFSIZE - 1);
#endif
}
Py_XDECREF(pValue);
} else {
Py_XDECREF(pFunc);
Py_XDECREF(pModule);
PyErr_Print();
Py_Finalize();
return CCPY_ERR_FAIL;
}
} else {
if (PyErr_Occurred())
PyErr_Print();
Py_Finalize();
return CCPY_ERR_FUNC_NOT_FOUND;
}
Py_XDECREF(pFunc);
Py_XDECREF(pModule);
} else {
PyErr_Print();
Py_Finalize();
return CCPY_ERR_FILE_NOT_FOUND;
}
Py_Finalize();
return CCPY_ERR_SUCCESS;
}