Debian Codegraph @

src/main/c/jpy_conv.c

raw symbols SCIP
1 /*
2 * Copyright 2015 Brockmann Consult GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "jpy_module.h"
18 #include "jpy_diag.h"
19 #include "jpy_jtype.h"
20 #include "jpy_conv.h"
21 #include "jpy_compat.h"
22
23
24
25 int JPy_AsJObject(JNIEnv* jenv, PyObject* pyObj, jobject* objectRef, jboolean allowJavaWrapping)
26 {
27 return JType_ConvertPythonToJavaObject(jenv, JPy_JObject, pyObj, objectRef, allowJavaWrapping);
28 }
29
30 int JPy_AsJObjectWithType(JNIEnv* jenv, PyObject* pyObj, jobject* objectRef, JPy_JType* type)
31 {
32 return JType_ConvertPythonToJavaObject(jenv, type, pyObj, objectRef, JNI_FALSE);
33 }
34
35 int JPy_AsJObjectWithClass(JNIEnv* jenv, PyObject* pyObj, jobject* objectRef, jclass classRef)
36 {
37 *objectRef = NULL;
38
39 if (pyObj == Py_None) {
40 return 0;
41 }
42
43 if (classRef != NULL) {
44 JPy_JType* valueType;
45
46 valueType = JType_GetType(jenv, classRef, JNI_FALSE); // TODO: we need to xdecref this
47 if (valueType == NULL) {
48 return -1;
49 }
50 if (JPy_AsJObjectWithType(jenv, pyObj, objectRef, valueType) < 0) {
51 return -1;
52 }
53 } else {
54 if (JPy_AsJObject(jenv, pyObj, objectRef, JNI_FALSE) < 0) {
55 return -1;
56 }
57 }
58
59 return 0;
60 }
61
62
63
64 PyObject* JPy_FromJObject(JNIEnv* jenv, jobject objectRef)
65 {
66 JPy_JType* type;
67 type = JType_GetTypeForObject(jenv, objectRef, JNI_FALSE);
68 if (type == NULL) {
69 return NULL;
70 }
71 return JPy_FromJObjectWithType(jenv, objectRef, type);
72 }
73
74 PyObject* JPy_FromJObjectWithType(JNIEnv* jenv, jobject objectRef, JPy_JType* type)
75 {
76 return JType_ConvertJavaToPythonObject(jenv, type, objectRef);
77 }
78
79
80 /**
81 * Copies the UTF, zero-terminated C-string.
82 * Caller is responsible for freeing the returned string using PyMem_Del().
83 */
84 char* JPy_CopyUTFString(const char* utfChars)
85 {
86 char* utfCharsCopy;
87
88 utfCharsCopy = PyMem_New(char, strlen(utfChars) + 1);
89 if (utfCharsCopy == NULL) {
90 PyErr_NoMemory();
91 return NULL;
92 }
93
94 strcpy(utfCharsCopy, utfChars);
95 return utfCharsCopy;
96 }
97
98 /**
99 * Copies the given jchar string used by Java into a wchar_t string used by Python.
100 * Caller is responsible for freeing the returned string using PyMem_Del().
101 */
102 wchar_t* JPy_ConvertToWCharString(const jchar* jChars, jint length)
103 {
104 wchar_t* wChars;
105 jint i;
106
107 wChars = PyMem_New(wchar_t, length + 1);
108 if (wChars == NULL) {
109 PyErr_NoMemory();
110 return NULL;
111 }
112
113 for (i = 0; i < length; i++) {
114 wChars[i] = (wchar_t) jChars[i];
115 }
116 wChars[length] = 0;
117
118 return wChars;
119 }
120
121 /**
122 * Copies the given wchar_t string used by Python into a jchar string used by Python.
123 * Caller is responsible for freeing the returned string using PyMem_Del().
124 */
125 jchar* JPy_ConvertToJCharString(const wchar_t* wChars, jint length)
126 {
127 jchar* jChars;
128 jint i;
129
130 jChars = PyMem_New(jchar, length + 1);
131 if (jChars == NULL) {
132 PyErr_NoMemory();
133 return NULL;
134 }
135
136 for (i = 0; i < length; i++) {
137 jChars[i] = (jchar) wChars[i];
138 }
139 jChars[length] = (jchar) 0;
140
141 return jChars;
142 }
143
144 /**
145 * Gets the UTF name of thie given class.
146 * Caller is responsible for freeing the returned string using Py_Del().
147 */
148 char* JPy_GetTypeName(JNIEnv* jenv, jclass classRef)
149 {
150 jstring jTypeName;
151 const char* jTypeNameChars;
152 char* typeNameCopy;
153
154 jTypeName = (*jenv)->CallObjectMethod(jenv, classRef, JPy_Class_GetName_MID);
155 JPy_ON_JAVA_EXCEPTION_RETURN(NULL);
156
157 jTypeNameChars = (*jenv)->GetStringUTFChars(jenv, jTypeName, NULL);
158 if (jTypeNameChars == NULL) {
159 PyErr_NoMemory();
160 typeNameCopy = NULL;
161 } else {
162 typeNameCopy = JPy_CopyUTFString(jTypeNameChars);
163 (*jenv)->ReleaseStringUTFChars(jenv, jTypeName, jTypeNameChars);
164 }
165 JPy_DELETE_LOCAL_REF(jTypeName);
166 return typeNameCopy;
167 }
168
169 /**
170 * Gets a string object representing the name of the given class.
171 * Returns a new reference.
172 */
173 PyObject* JPy_FromTypeName(JNIEnv* jenv, jclass classRef)
174 {
175 PyObject* pyTypeName;
176 jstring jTypeName;
177 const char* jTypeNameChars;
178
179 jTypeName = (*jenv)->CallObjectMethod(jenv, classRef, JPy_Class_GetName_MID);
180 JPy_ON_JAVA_EXCEPTION_RETURN(NULL);
181
182 jTypeNameChars = (*jenv)->GetStringUTFChars(jenv, jTypeName, NULL);
183 JPy_DIAG_PRINT(JPy_DIAG_F_TYPE, "JPy_FromTypeName: classRef=%p, jTypeNameChars=\"%s\"\n", classRef, jTypeNameChars);
184
185 if (jTypeNameChars == NULL) {
186 PyErr_NoMemory();
187 pyTypeName = NULL;
188 } else {
189 pyTypeName = Py_BuildValue("s", jTypeNameChars);
190 (*jenv)->ReleaseStringUTFChars(jenv, jTypeName, jTypeNameChars);
191 }
192 JPy_DELETE_LOCAL_REF(jTypeName);
193 return pyTypeName;
194 }
195
196
197 PyObject* JPy_FromJString(JNIEnv* jenv, jstring stringRef)
198 {
199 PyObject* returnValue;
200 const jchar* jChars;
201 jint length;
202
203 if (stringRef == NULL) {
204 Py_RETURN_NONE;
205 }
206
207 length = (*jenv)->GetStringLength(jenv, stringRef);
208 if (length == 0) {
209 return Py_BuildValue("s", "");
210 }
211
212 jChars = (*jenv)->GetStringChars(jenv, stringRef, NULL);
213 if (jChars == NULL) {
214 PyErr_NoMemory();
215 return NULL;
216 }
217
218 returnValue = JPy_FROM_WIDE_CHAR_STR(jChars, length);
219 (*jenv)->ReleaseStringChars(jenv, stringRef, jChars);
220
221 return returnValue;
222 }
223
224 /**
225 * Returns a new Java string (a local reference).
226 */
227 int JPy_AsJString(JNIEnv* jenv, PyObject* arg, jstring* stringRef)
228 {
229 Py_ssize_t length;
230 wchar_t* wChars;
231
232 if (arg == Py_None) {
233 *stringRef = NULL;
234 return 0;
235 }
236
237 wChars = JPy_AS_WIDE_CHAR_STR(arg, &length);
238 if (wChars == NULL) {
239 *stringRef = NULL;
240 return -1;
241 }
242
243 if (sizeof(wchar_t) == sizeof(jchar)) {
244 *stringRef = (*jenv)->NewString(jenv, (const jchar*) wChars, length);
245 } else {
246 jchar* jChars;
247 jChars = JPy_ConvertToJCharString(wChars, length);
248 if (jChars == NULL) {
249 goto error;
250 }
251 *stringRef = (*jenv)->NewString(jenv, jChars, length);
252 PyMem_Del(jChars);
253 }
254 if (*stringRef == NULL) {
255 PyMem_Del(wChars);
256 PyErr_NoMemory();
257 return -1;
258 }
259
260 error:
261 PyMem_Del(wChars);
262
263 return 0;
264 }