aboutsummaryrefslogtreecommitdiff
path: root/lib/python/python.cpp
blob: b7a4cf38251320313631fd6498abc9b176198b3f (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <lib/base/eerror.h>
                /* avoid warnigs :) */
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
extern "C" void init_enigma();
extern "C" void eBaseInit(void);
extern void bsodFatal();

#define SKIP_PART2
#include <lib/python/python.h>
#undef SKIP_PART2

#ifdef PYTHON_REFCOUNT_DEBUG
ePyObject &ePyObject::operator=(PyObject *ob)
{
	m_ob=ob;
	m_file=0;
	m_line=0;
	m_from=m_to=0;
	m_erased=false;
	return *this;
}

ePyObject &ePyObject::operator=(const ePyObject &ob)
{
	m_ob=ob.m_ob;
	m_file=ob.m_file;
	m_line=ob.m_line;
	m_from=ob.m_from;
	m_to=ob.m_to;
	m_erased=ob.m_erased;
	return *this;
}

ePyObject::operator PyObject*()
{
	if (m_ob)
	{
		if (!m_erased && m_ob->ob_refcnt > 0)
			return m_ob;
		eDebug("invalid access PyObject %s with refcount <= 0 %d",
			m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	return 0;
}

void ePyObject::incref(const char *file, int line)
{
	if (!m_ob)
	{
		eDebug("invalid incref python object with null pointer %s %d!!!", file, line);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	if (m_erased || m_ob->ob_refcnt <= 0)
	{
		eDebug("invalid incref %s python object with refcounting value %d in file %s line %d!!!",
			m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	if (m_ob->ob_refcnt == 0x7FFFFFFF)
	{
		eDebug("invalid incref %s python object with refcounting value %d (MAX_INT!!!) in file %s line %d!!!",
			m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	m_file = file;
	m_line = line;
	m_from = m_ob->ob_refcnt;
	m_to = m_from+1;
	Py_INCREF(m_ob);
}

void ePyObject::decref(const char *file, int line)
{
	if (!m_ob)
	{
		eDebug("invalid decref python object with null pointer %s %d!!!", file, line);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	if (m_erased || m_ob->ob_refcnt <= 0)
	{
		eDebug("invalid decref %s python object with refcounting value %d in file %s line %d!!!",
			m_erased ? "deleted" : "undeleted", m_ob->ob_refcnt, file, line);
		if (m_file)
			eDebug("last modified in file %s line %d from %d to %d",
				m_file, m_line, m_from, m_to);
		bsodFatal();
	}
	m_file = file;
	m_line = line;
	m_from = m_ob->ob_refcnt;
	m_to = m_from-1;
	m_erased = !m_to;
	Py_DECREF(m_ob);
}
#endif  // PYTHON_REFCOUNT_DEBUG

#define SKIP_PART1
#include <lib/python/python.h>
#undef SKIP_PART1

ePython::ePython()
{
//	Py_VerboseFlag = 1;
	
//	Py_OptimizeFlag = 1;
	
	Py_Initialize();
	PyEval_InitThreads();

	init_enigma();
	eBaseInit();
}

ePython::~ePython()
{
	Py_Finalize();
}

int ePython::execFile(const char *file)
{
	FILE *fp = fopen(file, "r");
	if (!fp)
		return -ENOENT;
	int ret = PyRun_SimpleFile(fp, file);
	fclose(fp);
	return ret;
}

int ePython::execute(const std::string &pythonfile, const std::string &funcname)
{
	ePyObject pName, pModule, pDict, pFunc, pArgs, pValue;
	pName = PyString_FromString(pythonfile.c_str());

	pModule = PyImport_Import(pName);
	Py_DECREF(pName);
	
	if (pModule)
	{
		pDict = PyModule_GetDict(pModule);
		
		pFunc = PyDict_GetItemString(pDict, funcname.c_str());
		
		if (pFunc && PyCallable_Check(pFunc))
		{
			pArgs = PyTuple_New(0);
				// implement arguments..
			pValue = PyObject_CallObject(pFunc, pArgs);
			Py_DECREF(pArgs);
			if (pValue)
			{
				printf("Result of call: %ld\n", PyInt_AsLong(pValue));
				Py_DECREF(pValue);
			} else
			{
				Py_DECREF(pModule);
				PyErr_Print();
				return 1;
			}
		}
	} else
	{
		if (PyErr_Occurred())
			PyErr_Print();
		return 1;
	}
	return 0;
}

int ePython::call(ePyObject pFunc, ePyObject pArgs)
{
	int res = -1;
	ePyObject pValue;
	if (pFunc && PyCallable_Check(pFunc))
	{
		pValue = PyObject_CallObject(pFunc, pArgs);
 		if (pValue)
		{
			if (PyInt_Check(pValue))
				res = PyInt_AsLong(pValue);
			else
				res = 0;
			Py_DECREF(pValue);
		} else
		{
		 	PyErr_Print();
			ePyObject FuncStr = PyObject_Str(pFunc);
			ePyObject ArgStr = PyObject_Str(pArgs);
		 	eDebug("(PyObject_CallObject(%s,%s) failed)", PyString_AS_STRING(FuncStr), PyString_AS_STRING(ArgStr));
			Py_DECREF(FuncStr);
			Py_DECREF(ArgStr);
		 	bsodFatal();
		}
	}
	return res;
}

ePyObject ePython::resolve(const std::string &pythonfile, const std::string &funcname)
{
	ePyObject pName, pModule, pDict, pFunc;

	pName = PyString_FromString(pythonfile.c_str());

	pModule = PyImport_Import(pName);
	Py_DECREF(pName);
	
	if (pModule)
	{
		pDict = PyModule_GetDict(pModule);
		pFunc = PyDict_GetItemString(pDict, funcname.c_str());
		Py_XINCREF(pFunc);
		Py_DECREF(pModule);
	} else if (PyErr_Occurred())
		PyErr_Print();
	return pFunc;
}