File: pythonenv.h

package info (click to toggle)
python-jpype 0.6.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,560 kB
  • sloc: cpp: 11,957; python: 3,844; java: 986; ansic: 875; makefile: 149; xml: 76; sh: 62
file content (317 lines) | stat: -rw-r--r-- 8,010 bytes parent folder | download
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*****************************************************************************
   Copyright 2004 Steve M�nard

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   
*****************************************************************************/   
#ifndef _PYTHON_ENV_H_
#define _PYTHON_ENV_H_

#define PY_CHECK(op) op; { \
	PyObject* __ex = PyErr_Occurred(); \
	if (__ex) { 	\
		throw PythonException(); \
	}\
};

#if (    (PY_VERSION_HEX <  0x02070000) \
     || ((PY_VERSION_HEX >= 0x03000000) \
      && (PY_VERSION_HEX <  0x03010000)) )

    #include "capsulethunk.h"
    #define USE_CAPSULE 1
    typedef void* CAPSULE_DESTRUCTOR_ARG_TYPE;
    typedef void (*PyCapsule_Destructor)(void*);
    #define CAPSULE_EXTRACT(obj) (obj)
#else
    typedef PyObject* CAPSULE_DESTRUCTOR_ARG_TYPE;
    #define CAPSULE_EXTRACT(obj) (PyCapsule_GetPointer(obj, PyCapsule_GetName(obj)))
#endif
/**
 * Exception wrapper for python-generated exceptions
 */
class PythonException : public HostException
{
public :
	PythonException();	
	PythonException(const PythonException& ex);

	virtual ~PythonException();

	virtual string getMessage();
	
	bool isJavaException();
	PyObject* getJavaException();
	
public :
	PyObject* m_ExceptionClass;
	PyObject* m_ExceptionValue;
	
};

/** 
 * JPythonEnvHelper
 */
class JPythonEnvHelper
{
};

/** 
 * JPyErr
 */
class JPyErr : public JPythonEnvHelper
{
public :
	static void setString(PyObject*exClass, const char* str);
	static void setObject(PyObject*exClass, PyObject* obj);
	static void check()
	{
		PY_CHECK(;);
	}
};

/** 
 * JPyArg 
 */
class JPyArg : public JPythonEnvHelper
{
public :
	template<typename T1>
	static void parseTuple(PyObject* arg, const char* pattern, T1 a1)
	{
		PY_CHECK( PyArg_ParseTuple(arg, pattern, a1) );
	}

	template<typename T1, typename T2>
	static void parseTuple(PyObject* arg, const char* pattern, T1 a1, T2 a2)
	{
		PY_CHECK( PyArg_ParseTuple(arg, pattern, a1, a2) );
	}

	template<typename T1, typename T2, typename T3>
	static void parseTuple(PyObject* arg, const char* pattern, T1 a1, T2 a2, T3 a3)
	{
		PY_CHECK( PyArg_ParseTuple(arg, pattern, a1, a2, a3) );
	}

	template<typename T1, typename T2, typename T3, typename T4>
	static void parseTuple(PyObject* arg, const char* pattern, T1 a1, T2 a2, T3 a3, T4 a4)
	{
		PY_CHECK( PyArg_ParseTuple(arg, pattern, a1, a2, a3, a4)) ;
		
	}

	template<typename T1, typename T2, typename T3, typename T4, typename T5>
	static void parseTuple(PyObject* arg, const char* pattern, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
	{
		PY_CHECK( PyArg_ParseTuple(arg, pattern, a1, a2, a3, a4, a5)) ;
		
	}

private :
	static void check_exception();

};

/**
 * JPyString
 */
class JPyString : public JPythonEnvHelper
{
public :
	static bool check(PyObject* obj);
	static bool checkStrict(PyObject*);
	static bool checkUnicode(PyObject*);
	static string asString(PyObject* obj);
	static JCharString asJCharString(PyObject* obj);
	static Py_ssize_t AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *);
	static Py_UNICODE* AsUnicode(PyObject *obj);

	static PyObject* fromUnicode(const jchar*, int);
	static PyObject* fromString(const char*);
};

/**
 * JPyTuple
 */
class JPySequence : public JPythonEnvHelper
{
public :
	static bool check(PyObject* obj);
	static PyObject* newTuple(Py_ssize_t ndx);
	static PyObject* newList(Py_ssize_t ndx);
	
	static PyObject* getItem(PyObject* tuple, Py_ssize_t ndx);
	static void setItem(PyObject* tuple, Py_ssize_t ndx, PyObject* val);
};

/**
 * JPyObject
 */
class JPyObject : public JPythonEnvHelper
{
public :
	static Py_ssize_t length(PyObject* obj);
	static bool hasAttr(PyObject*, PyObject*);
	static PyObject* getAttr(PyObject*, PyObject*);
	static PyObject* call(PyObject*, PyObject*, PyObject*);
	static PyObject* getAttrString(PyObject*, const char*);
	static void setAttrString(PyObject*, const char*, PyObject *);
	static bool isInstance(PyObject* obj, PyObject* t);
	static bool isSubclass(PyObject* obj, PyObject* t);
	static bool isMemoryView(PyObject* obj);
	static void AsPtrAndSize(PyObject *obj, char **buffer, Py_ssize_t *);
};

class JPyInt : public JPythonEnvHelper
{
public :
	static PyObject* fromLong(long l);	
	static bool check(PyObject*);
	static long asLong(PyObject*);
};

class JPyLong : public JPythonEnvHelper
{
public :
	static PyObject* fromLongLong(PY_LONG_LONG l);	
	static bool check(PyObject*);
	static PY_LONG_LONG asLongLong(PyObject*);
};

class JPyBoolean : public JPythonEnvHelper
{
public :
	static bool isTrue(PyObject* o)
	{
		return o == Py_True;
	}
	
	static bool isFalse(PyObject* o)
	{
		return o == Py_False;
	}

	static PyObject* getTrue();	
	static PyObject* getFalse();		
};

class JPyFloat : public JPythonEnvHelper
{
public :
	static PyObject* fromDouble(double l);	
	static bool check(PyObject*);
	static double asDouble(PyObject*);
};

class JPyDict : public JPythonEnvHelper
{
public :
	static bool check(PyObject* obj);
	static bool contains(PyObject*, PyObject*);
	static PyObject* getItem(PyObject*, PyObject*);
	static PyObject* getKeys(PyObject*);
	static PyObject* copy(PyObject*);

	static PyObject* newInstance();
	static void setItemString(PyObject*, PyObject*, const char*);
};

class JPyCObject : public JPythonEnvHelper
{
public :
	static bool check(PyObject* obj);
	static PyObject* fromVoid(void* data, PyCapsule_Destructor destr);
	static PyObject* fromVoidAndDesc(void* data, const char* desc, PyCapsule_Destructor destr);
	static void* asVoidPtr(PyObject*);
	static void* getDesc(PyObject*);
};

class JPyType : public JPythonEnvHelper
{
public :
	static bool check(PyObject* obj);
	static bool isSubclass(PyObject*, PyObject*);
};

class JPyHelper : public JPythonEnvHelper
{
public :
	static void dumpSequenceRefs(PyObject* seq, const char* comment); 
};

#undef PY_CHECK

#define PY_STANDARD_CATCH \
catch(JavaException& ex) \
{ \
	try { \
		JPypeJavaException::errorOccurred(); \
	} \
	catch(...) \
	{ \
		JPEnv::getHost()->setRuntimeException("An unknown error occured while handling a Java Exception"); \
	}\
}\
catch(JPypeException& ex)\
{\
	try { \
		JPEnv::getHost()->setRuntimeException(ex.getMsg()); \
	} \
	catch(...) \
	{ \
		JPEnv::getHost()->setRuntimeException("An unknown error occured while handling a JPype Exception"); \
	}\
}\
catch(PythonException& ex) \
{ \
} \
catch(...) \
{\
	JPEnv::getHost()->setRuntimeException("Unknown Exception"); \
} \

#define PY_LOGGING_CATCH \
catch(JavaException& ex) \
{ \
	try { \
	cout << "Java error occured : " << ex.message << endl; \
		JPypeJavaException::errorOccurred(); \
	} \
	catch(...) \
	{ \
		JPEnv::getHost()->setRuntimeException("An unknown error occured while handling a Java Exception"); \
	}\
}\
catch(JPypeException& ex)\
{\
	try { \
		cout << "JPype error occured" << endl; \
		JPEnv::getHost()->setRuntimeException(ex.getMsg()); \
	} \
	catch(...) \
	{ \
		JPEnv::getHost()->setRuntimeException("An unknown error occured while handling a JPype Exception"); \
	}\
}\
catch(PythonException& ex) \
{ \
	cout << "Pyhton error occured" << endl; \
} \
catch(...) \
{\
	cout << "Unknown error occured" << endl; \
	JPEnv::getHost()->setRuntimeException("Unknown Exception"); \
} \

#endif // _PYTHON_ENV_H_