File: pyjp_field.cpp

package info (click to toggle)
python-jpype 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: python: 18,767; cpp: 17,931; java: 8,448; xml: 1,305; makefile: 154; sh: 35
file content (142 lines) | stat: -rw-r--r-- 3,884 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
/*****************************************************************************
   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.

   See NOTICE file for details.
 *****************************************************************************/
#include "jpype.h"
#include "pyjp.h"
#include "jp_field.h"

#ifdef __cplusplus
extern "C"
{
#endif

struct PyJPField
{
	PyObject_HEAD
	JPField* m_Field;
} ;

static void PyJPField_dealloc(PyJPField *self)
{
	self->m_Field = nullptr;
	Py_TYPE(self)->tp_free(self);
}

static PyObject *PyJPField_get(PyJPField *self, PyObject *obj, PyObject *type)
{
	JP_PY_TRY("PyJPField_get");
	JPContext *context = PyJPModule_getContext();
	JPJavaFrame frame = JPJavaFrame::outer(context);
	// Clear any pending interrupts if we are on the main thread.
	if (hasInterrupt())
		frame.clearInterrupt(false);
	if (self->m_Field->isStatic())
		return self->m_Field->getStaticField().keep();
	if (obj == nullptr)
		JP_RAISE(PyExc_AttributeError, "Field is not static");
	JPValue *jval = PyJPValue_getJavaSlot(obj);
	if (jval == nullptr)
		JP_RAISE(PyExc_AttributeError, "Field requires instance value");

	return self->m_Field->getField(jval->getValue().l).keep();
	JP_PY_CATCH(nullptr);
}

static int PyJPField_set(PyJPField *self, PyObject *obj, PyObject *pyvalue)
{
	JP_PY_TRY("PyJPField_set");
	JPContext *context = PyJPModule_getContext();
	JPJavaFrame frame = JPJavaFrame::outer(context);
	if (self->m_Field->isFinal())
	{
		PyErr_SetString(PyExc_AttributeError, "Field is final");
		return -1;
	}
	if (self->m_Field->isStatic())
	{
		self->m_Field->setStaticField(pyvalue);
		return 0;
	}
	if (obj == Py_None || PyJPClass_Check(obj))
	{
		PyErr_SetString(PyExc_AttributeError, "Field is not static");
		return -1;
	}
	JPValue *jval = PyJPValue_getJavaSlot(obj);
	if (jval == nullptr)
	{
		PyErr_Format(PyExc_AttributeError, "Field requires instance value, not '%s'", Py_TYPE(obj)->tp_name);
		return -1;
	}
	self->m_Field->setField(jval->getValue().l, pyvalue);
	return 0;
	JP_PY_CATCH(-1);
}

static PyObject *PyJPField_repr(PyJPField *self)
{
	JP_PY_TRY("PyJPField_repr");
	JPContext *context = PyJPModule_getContext();
	JPJavaFrame frame = JPJavaFrame::outer(context);
	return PyUnicode_FromFormat("<java field '%s' of '%s'>",
			self->m_Field->getName().c_str(),
			self->m_Field->getClass()->getCanonicalName().c_str()
			);
	JP_PY_CATCH(nullptr);
}

static PyGetSetDef fieldGetSets[] = {
	{nullptr}
};

static PyType_Slot fieldSlots[] = {
	{ Py_tp_dealloc,   (void*) PyJPField_dealloc},
	{ Py_tp_descr_get, (void*) PyJPField_get},
	{ Py_tp_descr_set, (void*) PyJPField_set},
	{ Py_tp_repr,      (void*) &PyJPField_repr},
	{ Py_tp_getset,    (void*) &fieldGetSets},
	{0}
};

PyTypeObject *PyJPField_Type = nullptr;
PyType_Spec PyJPFieldSpec = {
	"_jpype._JField",
	sizeof (PyJPField),
	0,
	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
	fieldSlots
};

#ifdef __cplusplus
}
#endif

void PyJPField_initType(PyObject* module)
{
	PyJPField_Type = (PyTypeObject*) PyType_FromSpec(&PyJPFieldSpec);
	JP_PY_CHECK();
	PyModule_AddObject(module, "_JField", (PyObject*) PyJPField_Type);
	JP_PY_CHECK();
}

JPPyObject PyJPField_create(JPField* m)
{
	JP_TRACE_IN("PyJPField_create");
	auto* self = (PyJPField*) PyJPField_Type->tp_alloc(PyJPField_Type, 0);
	JP_PY_CHECK();
	self->m_Field = m;
	return JPPyObject::claim((PyObject*) self);
	JP_TRACE_OUT; // GCOVR_EXCL_LINE
}