Debian Codegraph @

python/cproton.i

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 %module cproton
20 %{
21 /* Includes the header in the wrapper code */
22 #if defined(_WIN32) && ! defined(__CYGWIN__)
23 #include <winsock2.h>
24 #endif
25
26 /* TODO: Remove once pn_work_head() and related have been removed from Proton */
27 #define PN_USE_DEPRECATED_API 1
28
29 #include <proton/engine.h>
30 #include <proton/message.h>
31 #include <proton/object.h>
32 #include <proton/sasl.h>
33 #include <proton/messenger.h>
34 #include <proton/ssl.h>
35
36 /*
37 NOTE: According to ccache-swig man page: "Known problems are using
38 preprocessor directives within %inline blocks and the use of ’#pragma SWIG’."
39 This includes using macros in an %inline section.
40
41 Keep preprocessor directives and macro expansions in the normal header section.
42 */
43
44 PN_HANDLE(PNI_PYTRACER);
45 %}
46
47 %include <cstring.i>
48
49 %cstring_output_allocate_size(char **ALLOC_OUTPUT, size_t *ALLOC_SIZE, free(*$1));
50 %cstring_output_maxsize(char *OUTPUT, size_t MAX_OUTPUT_SIZE)
51
52 %include <pybuffer.i>
53 %pybuffer_binary(const char *BIN_IN, size_t BIN_LEN)
54
55 // Typemap for methods that return binary data:
56 // force the return type as binary - this is necessary for Python3
57 %typemap(in,noblock=1,fragment=SWIG_AsVal_frag(size_t)) (char *BIN_OUT, size_t *BIN_SIZE)
58 (int res, size_t n, char *buff = 0, $*2_ltype size) {
59 res = SWIG_AsVal(size_t)($input, &n);
60 if (!SWIG_IsOK(res)) {
61 %argument_fail(res, "(char *BIN_OUT, size_t *BIN_SIZE)", $symname, $argnum);
62 }
63 buff= %new_array(n+1, char);
64 $1 = %static_cast(buff, $1_ltype);
65 size = %numeric_cast(n,$*2_ltype);
66 $2 = &size;
67 }
68 %typemap(freearg,noblock=1,match="in")(char *BIN_OUT, size_t *BIN_SIZE) {
69 if (buff$argnum) %delete_array(buff$argnum);
70 }
71 %typemap(argout,noblock=1) (char *BIN_OUT, size_t *BIN_SIZE) {
72 %append_output(PyBytes_FromStringAndSize($1,*$2));
73 }
74
75 // Typemap for those methods that return variable length text data in a buffer
76 // provided as a parameter. If the method fails we must avoid attempting to
77 // decode the contents of the buffer as it does not carry valid text data.
78 %typemap(in,noblock=1,fragment=SWIG_AsVal_frag(size_t)) (char *VTEXT_OUT, size_t *VTEXT_SIZE)
79 (int res, size_t n, char *buff = 0, $*2_ltype size) {
80 res = SWIG_AsVal(size_t)($input, &n);
81 if (!SWIG_IsOK(res)) {
82 %argument_fail(res, "(char *VTEXT_OUT, size_t *VTEXT_SIZE)", $symname, $argnum);
83 }
84 buff = %new_array(n+1, char);
85 $1 = %static_cast(buff, $1_ltype);
86 size = %numeric_cast(n,$*2_ltype);
87 $2 = &size;
88 }
89 %typemap(freearg,noblock=1,match="in")(char *VTEXT_OUT, size_t *VTEXT_SIZE) {
90 if (buff$argnum) %delete_array(buff$argnum);
91 }
92 %typemap(argout,noblock=1,fragment="SWIG_FromCharPtrAndSize") (char *VTEXT_OUT, size_t *VTEXT_SIZE) {
93 %append_output(SWIG_FromCharPtrAndSize($1,*$2));
94 }
95
96
97 // These are not used/needed in the python binding
98 %ignore pn_dtag;
99 %ignore pn_message_id;
100 %ignore pn_message_correlation_id;
101
102 %ignore pn_list;
103 %ignore pn_list_size;
104 %ignore pn_list_get;
105 %ignore pn_list_set;
106 %ignore pn_list_add;
107 %ignore pn_list_pop;
108 %ignore pn_list_index;
109 %ignore pn_list_remove;
110 %ignore pn_list_del;
111 %ignore pn_list_clear;
112 %ignore pn_list_iterator;
113 %ignore pn_list_minpush;
114 %ignore pn_list_minpop;
115
116 %ignore pn_map;
117 %ignore pn_map_size;
118 %ignore pn_map_put;
119 %ignore pn_map_get;
120 %ignore pn_map_del;
121 %ignore pn_map_head;
122 %ignore pn_map_next;
123 %ignore pn_map_key;
124 %ignore pn_map_value;
125
126 %ignore pn_hash;
127 %ignore pn_hash_size;
128 %ignore pn_hash_put;
129 %ignore pn_hash_get;
130 %ignore pn_hash_del;
131 %ignore pn_hash_head;
132 %ignore pn_hash_next;
133 %ignore pn_hash_key;
134 %ignore pn_hash_value;
135
136 %ignore pn_stringn;
137 %ignore pn_string_size;
138 %ignore pn_string_set;
139 %ignore pn_string_setn;
140 %ignore pn_string_put;
141 %ignore pn_string_clear;
142 %ignore pn_string_format;
143 %ignore pn_string_vformat;
144 %ignore pn_string_addf;
145 %ignore pn_string_vaddf;
146 %ignore pn_string_grow;
147 %ignore pn_string_buffer;
148 %ignore pn_string_capacity;
149 %ignore pn_string_resize;
150 %ignore pn_string_copy;
151
152 %ignore pn_iterator;
153 %ignore pn_iterator_start;
154 %ignore pn_iterator_next;
155
156 %typemap(in) pn_handle_t {
157 $1 = PyLong_AsVoidPtr($input);
158 }
159
160 %typemap(out) pn_handle_t {
161 $result = PyLong_FromVoidPtr((void*)$1);
162 }
163
164
165 %typemap(in) pn_bytes_t {
166 if ($input == Py_None) {
167 $1.start = NULL;
168 $1.size = 0;
169 } else {
170 $1.start = PyBytes_AsString($input);
171
172 if (!$1.start) {
173 return NULL;
174 }
175 $1.size = PyBytes_Size($input);
176 }
177 }
178
179 %typemap(out) pn_bytes_t {
180 $result = PyBytes_FromStringAndSize($1.start, $1.size);
181 }
182
183 %typemap(in) pn_msgid_t {
184 if (PyTuple_Check($input)) {
185 pn_type_t type = PyLong_AsUnsignedLong(PyTuple_GetItem($input, 0));
186 PyObject* obj = PyTuple_GetItem($input, 1);
187 switch (type) {
188 case PN_NULL:
189 break;
190 case PN_ULONG:
191 $1.u.as_ulong = PyLong_AsUnsignedLong(obj);
192 break;
193 case PN_UUID:
194 if (PyBytes_Check(obj)) {
195 memmove(&$1.u.as_uuid, PyBytes_AsString(obj), (PyBytes_Size(obj) < 16 ? PyBytes_Size(obj) : 16));
196 break;
197 }
198 PyErr_SetString(PyExc_TypeError, "Require bytes for uuid id");
199 SWIG_fail;
200 case PN_BINARY:
201 if (PyBytes_Check(obj)) {
202 $1.u.as_bytes = (pn_bytes_t){.size=PyBytes_Size(obj), .start=PyBytes_AsString(obj)};
203 break;
204 }
205 PyErr_SetString(PyExc_TypeError, "Require bytes for binary id");
206 SWIG_fail;
207 case PN_STRING:
208 if (PyBytes_Check(obj)) {
209 $1.u.as_bytes = (pn_bytes_t){.size=PyBytes_Size(obj), .start=PyBytes_AsString(obj)};
210 break;
211 }
212 PyErr_SetString(PyExc_TypeError, "Require bytes for string id");
213 SWIG_fail;
214 default:
215 PyErr_SetString(PyExc_ValueError, "Invalid type selector for id");
216 SWIG_fail;
217 }
218 $1.type = type;
219 } else if (PyLong_Check($input)) {
220 $1.type = PN_ULONG;
221 $1.u.as_ulong = PyLong_AsUnsignedLong($input);
222 } else if (PyBytes_Check($input)) {
223 $1.type = PN_BINARY;
224 $1.u.as_bytes = (pn_bytes_t){.size=PyBytes_Size($input), .start=PyBytes_AsString($input)};
225 } else if (PyUnicode_Check($input)) {
226 $1.type = PN_STRING;
227 Py_ssize_t utf8size;
228 const char *utf8 = PyUnicode_AsUTF8AndSize($input, &utf8size);
229 $1.u.as_bytes = (pn_bytes_t){.size=utf8size, .start=utf8};
230 } else if ($input == Py_None) {
231 $1.type = PN_NULL;
232 } else {
233 PyErr_SetString(PyExc_TypeError, "Cannot convert to id");
234 SWIG_fail;
235 }
236 }
237
238 %typemap(out) pn_msgid_t {
239 switch ($1.type) {
240 case PN_NULL:
241 Py_INCREF(Py_None);
242 $result = Py_None;
243 break;
244 case PN_ULONG:
245 $result = PyLong_FromUnsignedLong($1.u.as_ulong);
246 break;
247 case PN_BINARY:
248 $result = PyBytes_FromStringAndSize($1.u.as_bytes.start, $1.u.as_bytes.size);
249 break;
250 case PN_STRING:
251 $result = PyUnicode_FromStringAndSize($1.u.as_bytes.start, $1.u.as_bytes.size);
252 break;
253 case PN_UUID:
254 $result = PyTuple_New(2);
255 PyTuple_SetItem($result, 0, PyLong_FromUnsignedLong($1.type));
256 PyTuple_SetItem($result, 1, PyBytes_FromStringAndSize($1.u.as_uuid.bytes, 16));
257 break;
258 // Theses 2 cases are for compatibility with the broken ruby binding
259 case PN_INT: {
260 int32_t v = $1.u.as_int;
261 if (v>=0) {
262 $result = PyLong_FromLong(v);
263 break;
264 }
265 Py_INCREF(Py_None);
266 $result = Py_None;
267 break;
268 }
269 case PN_LONG: {
270 int64_t v = $1.u.as_long;
271 if (v>=0) {
272 $result = PyLong_FromLong(v);
273 break;
274 }
275 }
276 default:
277 Py_INCREF(Py_None);
278 $result = Py_None;
279 break;
280 }
281 }
282
283 %typemap(out) pn_delivery_tag_t {
284 $result = PyBytes_FromStringAndSize($1.bytes, $1.size);
285 }
286
287 %typemap(in) pn_uuid_t {
288 memset($1.bytes, 0, 16);
289 if ($input == Py_None) {
290 ; // Already zeroed out
291 } else {
292 const char* b = PyBytes_AsString($input);
293 if (b) {
294 memmove($1.bytes, b, (PyBytes_Size($input) < 16 ? PyBytes_Size($input) : 16));
295 } else {
296 return NULL;
297 }
298 }
299 }
300
301 %typemap(out) pn_uuid_t {
302 $result = PyBytes_FromStringAndSize($1.bytes, 16);
303 }
304
305 %apply pn_uuid_t { pn_decimal128_t };
306
307 int pn_message_encode(pn_message_t *msg, char *BIN_OUT, size_t *BIN_SIZE);
308 %ignore pn_message_encode;
309
310 int pn_message_decode(pn_message_t *msg, const char *BIN_IN, size_t BIN_LEN);
311 %ignore pn_message_decode;
312
313 ssize_t pn_link_send(pn_link_t *transport, const char *BIN_IN, size_t BIN_LEN);
314 %ignore pn_link_send;
315
316 %rename(pn_link_recv) wrap_pn_link_recv;
317 %inline %{
318 ssize_t wrap_pn_link_recv(pn_link_t *link, char *BIN_OUT, size_t *BIN_SIZE) {
319 ssize_t sz = pn_link_recv(link, BIN_OUT, *BIN_SIZE);
320 if (sz >= 0) {
321 *BIN_SIZE = sz;
322 } else {
323 *BIN_SIZE = 0;
324 }
325 return sz;
326 }
327 %}
328 %ignore pn_link_recv;
329
330 ssize_t pn_transport_push(pn_transport_t *transport, const char *BIN_IN, size_t BIN_LEN);
331 %ignore pn_transport_push;
332
333 %rename(pn_transport_peek) wrap_pn_transport_peek;
334 %inline %{
335 ssize_t wrap_pn_transport_peek(pn_transport_t *transport, char *BIN_OUT, size_t *BIN_SIZE) {
336 ssize_t sz = pn_transport_peek(transport, BIN_OUT, *BIN_SIZE);
337 if (sz >= 0) {
338 *BIN_SIZE = sz;
339 } else {
340 *BIN_SIZE = 0;
341 }
342 return sz;
343 }
344 %}
345 %ignore pn_transport_peek;
346
347 %rename(pn_delivery) wrap_pn_delivery;
348 %inline %{
349 pn_delivery_t *wrap_pn_delivery(pn_link_t *link, char *STRING, size_t LENGTH) {
350 return pn_delivery(link, pn_dtag(STRING, LENGTH));
351 }
352 %}
353 %ignore pn_delivery;
354
355 %rename(pn_delivery_tag) wrap_pn_delivery_tag;
356 %inline %{
357 void wrap_pn_delivery_tag(pn_delivery_t *delivery, char **ALLOC_OUTPUT, size_t *ALLOC_SIZE) {
358 pn_delivery_tag_t tag = pn_delivery_tag(delivery);
359 *ALLOC_OUTPUT = (char *) malloc(tag.size);
360 *ALLOC_SIZE = tag.size;
361 memcpy(*ALLOC_OUTPUT, tag.start, tag.size);
362 }
363 %}
364 %ignore pn_delivery_tag;
365
366 ssize_t pn_data_decode(pn_data_t *data, const char *BIN_IN, size_t BIN_LEN);
367 %ignore pn_data_decode;
368
369 %rename(pn_data_encode) wrap_pn_data_encode;
370 %inline %{
371 ssize_t wrap_pn_data_encode(pn_data_t *data, char *BIN_OUT, size_t *BIN_SIZE) {
372 ssize_t sz = pn_data_encode(data, BIN_OUT, *BIN_SIZE);
373 if (sz >= 0) {
374 *BIN_SIZE = sz;
375 } else {
376 *BIN_SIZE = 0;
377 }
378 return sz;
379 }
380 %}
381 %ignore pn_data_encode;
382
383 %rename(pn_data_format) wrap_pn_data_format;
384 %inline %{
385 int wrap_pn_data_format(pn_data_t *data, char *VTEXT_OUT, size_t *VTEXT_SIZE) {
386 int err = pn_data_format(data, VTEXT_OUT, VTEXT_SIZE);
387 if (err) *VTEXT_SIZE = 0;
388 return err;
389 }
390 %}
391 %ignore pn_data_format;
392
393 bool pn_ssl_get_cipher_name(pn_ssl_t *ssl, char *OUTPUT, size_t MAX_OUTPUT_SIZE);
394 %ignore pn_ssl_get_cipher_name;
395
396 bool pn_ssl_get_protocol_name(pn_ssl_t *ssl, char *OUTPUT, size_t MAX_OUTPUT_SIZE);
397 %ignore pn_ssl_get_protocol_name;
398
399 char* pn_ssl_get_remote_subject_subfield(pn_ssl_t *ssl, pn_ssl_cert_subject_subfield field);
400 %ignore pn_ssl_get_remote_subject_subfield;
401
402 int pn_ssl_get_cert_fingerprint(pn_ssl_t *ssl, char *OUTPUT, size_t MAX_OUTPUT_SIZE, pn_ssl_hash_alg hash_alg);
403 %ignore pn_ssl_get_cert_fingerprint;
404
405 %rename(pn_ssl_get_peer_hostname) wrap_pn_ssl_get_peer_hostname;
406 %inline %{
407 int wrap_pn_ssl_get_peer_hostname(pn_ssl_t *ssl, char *VTEXT_OUT, size_t *VTEXT_SIZE) {
408 int err = pn_ssl_get_peer_hostname(ssl, VTEXT_OUT, VTEXT_SIZE);
409 if (err) *VTEXT_SIZE = 0;
410 return err;
411 }
412 %}
413 %ignore pn_ssl_get_peer_hostname;
414
415
416 %immutable PN_PYREF;
417 %inline %{
418 extern const pn_class_t PN_PYREF[];
419
420 #define CID_pn_pyref CID_pn_void
421 #define pn_pyref_new NULL
422 #define pn_pyref_initialize NULL
423 #define pn_pyref_finalize NULL
424 #define pn_pyref_free NULL
425 #define pn_pyref_hashcode pn_void_hashcode
426 #define pn_pyref_compare pn_void_compare
427 #define pn_pyref_inspect pn_void_inspect
428
429 static void pn_pyref_incref(void *object) {
430 PyObject* p = (PyObject*) object;
431 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
432 Py_XINCREF(p);
433 SWIG_PYTHON_THREAD_END_BLOCK;
434 }
435
436 static void pn_pyref_decref(void *object) {
437 PyObject* p = (PyObject*) object;
438 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
439 Py_XDECREF(p);
440 SWIG_PYTHON_THREAD_END_BLOCK;
441 }
442
443 static int pn_pyref_refcount(void *object) {
444 return 1;
445 }
446
447 static const pn_class_t *pn_pyref_reify(void *object) {
448 return PN_PYREF;
449 }
450
451 const pn_class_t PN_PYREF[] = {PN_METACLASS(pn_pyref)};
452
453 void *pn_py2void(PyObject *object) {
454 return object;
455 }
456
457 PyObject *pn_void2py(void *object) {
458 if (object) {
459 PyObject* p = (PyObject*) object;
460 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
461 Py_INCREF(p);
462 SWIG_PYTHON_THREAD_END_BLOCK;
463 return p;
464 } else {
465 Py_RETURN_NONE;
466 }
467 }
468
469 PyObject *pn_cast_pn_void(void *object) {
470 return pn_void2py(object);
471 }
472
473 void pn_pytracer(pn_transport_t *transport, const char *message) {
474 PyObject *pytracer = (PyObject *) pn_record_get(pn_transport_attachments(transport), PNI_PYTRACER);
475 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
476 PyObject *pytrans = SWIG_NewPointerObj(transport, SWIGTYPE_p_pn_transport_t, 0);
•477 PyObject *pymsg = PyUnicode_FromString(message);
478 PyObject *result = PyObject_CallFunctionObjArgs(pytracer, pytrans, pymsg, NULL);
479 if (!result) {
480 PyErr_PrintEx(true);
481 }
482 Py_XDECREF(pytrans);
483 Py_XDECREF(pymsg);
484 Py_XDECREF(result);
485 SWIG_PYTHON_THREAD_END_BLOCK;
486 }
487
488 void pn_transport_set_pytracer(pn_transport_t *transport, PyObject *obj) {
489 pn_record_t *record = pn_transport_attachments(transport);
490 pn_record_def(record, PNI_PYTRACER, PN_PYREF);
491 pn_record_set(record, PNI_PYTRACER, obj);
492 pn_transport_set_tracer(transport, pn_pytracer);
493 }
494
495 PyObject *pn_transport_get_pytracer(pn_transport_t *transport) {
496 pn_record_t *record = pn_transport_attachments(transport);
497 PyObject *obj = (PyObject *)pn_record_get(record, PNI_PYTRACER);
498 if (obj) {
499 Py_XINCREF(obj);
500 return obj;
501 } else {
502 Py_RETURN_NONE;
503 }
504 }
505
506 %}
507
508 %include "proton/cproton.i"