Debian Codegraph @

native/python/pyjp_monitor.cpp

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 #include "jpype.h"
17 #include "pyjp.h"
18 #include "jp_monitor.h"
19 #include "jp_stringtype.h"
20
21 #ifdef __cplusplus
22 extern "C"
23 {
24 #endif
25
26 struct PyJPMonitor
27 {
28 PyObject_HEAD
29 JPMonitor *m_Monitor;
30 } ;
31
32 static int PyJPMonitor_init(PyJPMonitor *self, PyObject *args)
33 {
34 JP_PY_TRY("PyJPMonitor_init");
35 self->m_Monitor = nullptr;
36 JPContext *context = PyJPModule_getContext();
37 JPJavaFrame frame = JPJavaFrame::outer();
38
39 PyObject* value;
40
41 if (!PyArg_ParseTuple(args, "O", &value))
42 return -1;
43
44 JPValue *v1 = PyJPValue_getJavaSlot(value);
45 if (v1 == nullptr)
46 {
47 PyErr_SetString(PyExc_TypeError, "Java object is required.");
48 return -1;
49 }
50
51 if (v1->getClass() == context->_java_lang_String)
52 {
53 PyErr_SetString(PyExc_TypeError, "Java strings cannot be used to synchronize.");
54 return -1;
55 }
56
57 if ((v1->getClass())->isPrimitive())
58 {
59 PyErr_SetString(PyExc_TypeError, "Java primitives cannot be used to synchronize.");
60 return -1;
61 }
62
63 if (v1->getValue().l == nullptr)
64 {
65 PyErr_SetString(PyExc_TypeError, "Java null cannot be used to synchronize.");
66 return -1;
67 }
68
69 self->m_Monitor = new JPMonitor(v1->getValue().l);
70 return 0;
71 JP_PY_CATCH(-1);
72 }
73
74 static void PyJPMonitor_dealloc(PyJPMonitor *self)
75 {
76 JP_PY_TRY("PyJPMonitor_dealloc");
77 delete self->m_Monitor;
78 Py_TYPE(self)->tp_free(self);
79 JP_PY_CATCH(); // GCOVR_EXCL_LINE
80 }
81
82 static PyObject *PyJPMonitor_str(PyJPMonitor *self)
83 {
84 JP_PY_TRY("PyJPMonitor_str");
85 return PyUnicode_FromFormat("<java monitor>");
86 JP_PY_CATCH(nullptr);
87 }
88
89 static PyObject *PyJPMonitor_enter(PyJPMonitor *self, PyObject *args)
90 {
91 JP_PY_TRY("PyJPMonitor_enter");
92 JPJavaFrame frame = JPJavaFrame::outer();
93 self->m_Monitor->enter();
94 Py_RETURN_NONE;
95 JP_PY_CATCH(nullptr);
96 }
97
98 static PyObject *PyJPMonitor_exit(PyJPMonitor *self, PyObject *args)
99 {
100 JP_PY_TRY("PyJPMonitor_exit");
101 JPJavaFrame frame = JPJavaFrame::outer();
102 self->m_Monitor->exit();
103 Py_RETURN_NONE;
104 JP_PY_CATCH(nullptr);
105 }
106
107 static PyMethodDef monitorMethods[] = {
108 {"__enter__", (PyCFunction) (&PyJPMonitor_enter), METH_NOARGS, ""},
109 {"__exit__", (PyCFunction) (&PyJPMonitor_exit), METH_VARARGS, ""},
110 {nullptr},
111 };
112
113 static PyType_Slot monitorSlots[] = {
114 { Py_tp_init, (void*) PyJPMonitor_init},
115 { Py_tp_dealloc, (void*) PyJPMonitor_dealloc},
116 { Py_tp_str, (void*) PyJPMonitor_str},
117 { Py_tp_methods, (void*) &monitorMethods},
118 {0}
119 };
120
121 PyType_Spec PyJPMonitorSpec = {
122 "_jpype._JMonitor",
123 sizeof (PyJPMonitor),
124 0,
125 Py_TPFLAGS_DEFAULT,
126 monitorSlots
127 };
128
129 PyTypeObject* PyJPMonitor_Type = nullptr;
130
131 #ifdef __cplusplus
132 }
133 #endif
134
135 void PyJPMonitor_initType(PyObject* module)
136 {
137 PyJPMonitor_Type = (PyTypeObject*) PyType_FromSpec(&PyJPMonitorSpec);
138 JP_PY_CHECK(); // GCOVR_EXCL_LINE
139 PyModule_AddObject(module, "_JMonitor", (PyObject*) PyJPMonitor_Type);
140 JP_PY_CHECK(); // GCOVR_EXCL_LINE
141 }