Debian Codegraph @

native/python/include/pyjp.h

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #ifndef PYJP_H
17 #define PYJP_H
18 #include <Python.h>
19 #include "jpype.h"
20 #include "jp_pythontypes.h"
21
22 class JPStackInfo;
23 #ifdef JP_TRACING_ENABLE
24 #define JP_PY_TRY(...) \
25 JPypeTracer _trace(__VA_ARGS__); \
26 try { do {} while(0)
27 #define JP_PY_CATCH(...) \
28 } catch(...) { \
29 PyJPModule_rethrow(JP_STACKINFO()); } \
30 return __VA_ARGS__
31 #define JP_PY_CATCH_NONE(...) } catch(...) {} return __VA_ARGS__
32 #else
33 #ifndef JP_INSTRUMENTATION
34 #define JP_PY_TRY(...) try { do {} while(0)
35 #else
36 #define JP_PY_TRY(...) JP_TRACE_IN(__VA_ARGS__)
37 #endif
38 #define JP_PY_CATCH(...) } catch(...) \
39 { PyJPModule_rethrow(JP_STACKINFO()); } \
40 return __VA_ARGS__
41 #define JP_PY_CATCH_NONE(...) } catch(...) {} return __VA_ARGS__
42 #endif
43
44 // Macro to all after executing a Python command that can result in
45 // a failure to convert it to an exception.
46 #define JP_PY_CHECK() { if (PyErr_Occurred() != 0) JP_RAISE_PYTHON(); } // GCOVR_EXCL_LINE
47
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #endif
52
53 // Needed to write common code with older versions
54 #ifndef Py_TRASHCAN_BEGIN
55 // Introduced in Python 3.8
56 #define Py_TRASHCAN_BEGIN(X, Y)
57 #define Py_TRASHCAN_END
58 #endif
59
60 PyMODINIT_FUNC PyInit__jpype();
61
62 /**
63 * Set the current exception as the cause of a new exception.
64 *
65 * @param exception
66 * @param str
67 */
68 void PyJP_SetStringWithCause(PyObject *exception, const char *str);
69
70 /**
71 * Get a new reference to a method or property in the type dictionary without
72 * dereferencing.
73 *
74 * @param type
75 * @param attr_name
76 * @return
77 */
78 PyObject* PyJP_GetAttrDescriptor(PyTypeObject *type, PyObject *attr_name);
79
80 /**
81 * Fast check to see if a type derives from another.
82 *
83 * This depends on the MRO order. It is useful of our base types where
84 * the order is fixed.
85 *
86 * @param type
87 * @param obj
88 * @return 1 if object derives from type.
89 */
90 int PyJP_IsInstanceSingle(PyObject* obj, PyTypeObject* type);
91 int PyJP_IsSubClassSingle(PyTypeObject* type, PyTypeObject* obj);
92
93 struct PyJPArray
94 {
95 PyObject_HEAD
96 JPArray *m_Array;
97 JPArrayView *m_View;
98 } ;
99
100 struct PyJPClassHints
101 {
102 PyObject_HEAD
103 JPClassHints *m_Hints;
104 } ;
105
106 struct PyJPProxy
107 {
108 PyObject_HEAD
109 JPProxy* m_Proxy;
110 PyObject* m_Target;
111 PyObject* m_Dispatch;
112 bool m_Convert;
113 } ;
114
115 struct JPConversionInfo
116 {
117 PyObject *ret;
118 PyObject *exact;
119 PyObject *implicit;
120 PyObject *attributes;
121 PyObject *expl;
122 PyObject *none;
123 } ;
124
125
126 // JPype types
127 extern PyTypeObject *PyJPArray_Type;
128 extern PyTypeObject *PyJPArrayPrimitive_Type;
129 extern PyTypeObject *PyJPBuffer_Type;
130 extern PyTypeObject *PyJPClass_Type;
131 extern PyTypeObject *PyJPComparable_Type;
132 extern PyTypeObject *PyJPMethod_Type;
133 extern PyTypeObject *PyJPObject_Type;
134 extern PyTypeObject *PyJPProxy_Type;
135 extern PyTypeObject *PyJPException_Type;
136 extern PyTypeObject *PyJPNumberLong_Type;
137 extern PyTypeObject *PyJPNumberFloat_Type;
138 extern PyTypeObject *PyJPNumberBool_Type;
139 extern PyTypeObject *PyJPChar_Type;
140
141
142 // JPype resources
143 extern PyObject *PyJPModule;
144 extern PyObject *_JArray;
145 extern PyObject *_JChar;
146 extern PyObject *_JObject;
147 extern PyObject *_JInterface;
148 extern PyObject *_JException;
149 extern PyObject *_JClassPre;
150 extern PyObject *_JClassPost;
151 extern PyObject *_JClassDoc;
152 extern PyObject *_JMethodDoc;
153 extern PyObject *_JMethodAnnotations;
154 extern PyObject *_JMethodCode;
155 extern PyObject *_JObjectKey;
156 extern PyObject *_JVMNotRunning;
157 extern PyObject* PyJPClassMagic;
158
159 extern JPContext* JPContext_global;
160
161 // Class wrapper functions
162 int PyJPClass_Check(PyObject* obj);
163 PyObject *PyJPClass_FromSpecWithBases(PyType_Spec *spec, PyObject *bases);
164
165 // Class methods to add to the spec tables
166 PyObject *PyJPValue_alloc(PyTypeObject* type, Py_ssize_t nitems );
167 void PyJPValue_free(void* obj);
168 void PyJPValue_finalize(void* obj);
169 int PyJPValue_traverse(PyObject *self, visitproc visit, void *arg);
170 int PyJPValue_clear(PyObject *self);
171
172 // Generic methods that operate on any object with a Java slot
173 PyObject *PyJPValue_str(PyObject* self);
174 bool PyJPValue_hasJavaSlot(PyTypeObject* type);
175 Py_ssize_t PyJPValue_getJavaSlotOffset(PyObject* self);
176 JPValue *PyJPValue_getJavaSlot(PyObject* obj);
177
178 // Access point for creating classes
179 PyObject *PyJPModule_getClass(PyObject* module, PyObject *obj);
180 PyObject *PyJPValue_getattro(PyObject *obj, PyObject *name);
181 int PyJPValue_setattro(PyObject *self, PyObject *name, PyObject *value);
182 PyObject *PyJPChar_Create(PyTypeObject *type, Py_UCS2 p);
183
184 #ifdef __cplusplus
185 }
186 #endif
187
188 void PyJPClass_hook(JPJavaFrame &frame, JPClass* cls);
189
190 // C++ methods
191 JPPyObject PyJPArray_create(JPJavaFrame &frame, PyTypeObject* wrapper, const JPValue& value);
192 JPPyObject PyJPBuffer_create(JPJavaFrame &frame, PyTypeObject *type, const JPValue & value);
193 JPPyObject PyJPClass_create(JPJavaFrame &frame, JPClass* cls);
194 JPPyObject PyJPNumber_create(JPJavaFrame &frame, JPPyObject& wrapper, const JPValue& value);
195 JPPyObject PyJPField_create(JPField* m);
196 JPPyObject PyJPMethod_create(JPMethodDispatch *m, PyObject *instance);
197
198 JPClass* PyJPClass_getJPClass(PyObject* obj);
199 JPProxy* PyJPProxy_getJPProxy(PyObject* obj);
200 void PyJPModule_rethrow(const JPStackInfo& info);
201 void PyJPValue_assignJavaSlot(JPJavaFrame &frame, PyObject* obj, const JPValue& value);
202 bool PyJPValue_isSetJavaSlot(PyObject* self);
203 JPPyObject PyTrace_FromJavaException(JPJavaFrame& frame, jthrowable th, jthrowable prev);
204 void PyJPException_normalize(JPJavaFrame frame, JPPyObject exc, jthrowable th, jthrowable enclosing);
205
206 #define _ASSERT_JVM_RUNNING(context) assertJVMRunning((JPContext*)context, JP_STACKINFO())
207
208 /**
209 * Use this when getting the context where the context must be running.
210 *
211 * The context needs to be accessed before accessing and JPClass* or other
212 * internal structured. Those resources are owned by the JVM and thus
213 * will be deleted when the JVM is shutdown. This method will throw if the
214 * JVM is not running.
215 *
216 * If the context may or many not be running access JPContext_global directly.
217 */
218 inline JPContext* PyJPModule_getContext()
219 {
220 #ifdef JP_INSTRUMENTATION
221 PyJPModuleFault_throw(compile_hash("PyJPModule_getContext"));
222 #endif
223 JPContext* context = JPContext_global;
224 _ASSERT_JVM_RUNNING(context); // GCOVR_EXCL_LINE
225 return context;
226 }
227 void PyJPModule_loadResources(PyObject* module);
228
229 #endif /* PYJP_H */