source: swig/python/chunk.i @ b1f5833

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since b1f5833 was bc62c32, checked in by Mike Ryan <mikeryan@…>, 13 years ago

check type of data being passed as an abac_chunk in the python. handle
strings and byte arrays correctly, and raise type errors for all other
data types
fixes #15

  • Property mode set to 100644
File size: 896 bytes
Line 
1// bytearrays are new as of 2.6
2%{
3#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 6) || PY_MAJOR_VERSION > 2
4#define MODERN_PYTHON
5#endif
6%}
7
8// abac_chunk_t is a bytearray or a string
9%typemap(in) abac_chunk_t %{
10    if (PyString_Check($input))
11        PyString_AsStringAndSize(
12            $input,
13            (char **)&$1.ptr,
14            &$1.len
15        );
16#ifdef MODERN_PYTHON
17    else if (PyByteArray_Check($input)) {
18        $1.ptr = (unsigned char *)PyByteArray_AS_STRING($input);
19        $1.len = PyByteArray_GET_SIZE($input);
20    }
21#endif
22    else
23        SWIG_exception(SWIG_TypeError, "Expected string or byte array");
24%}
25
26%typemap(out) abac_chunk_t %{
27#ifdef MODERN_PYTHON
28    $result = PyByteArray_FromStringAndSize(
29        (const char *)$1.ptr,
30        $1.len
31    );
32#else
33    $result = PyString_FromStringAndSize(
34        (const char *)$1.ptr,
35        $1.len
36    );
37#endif
38%}
Note: See TracBrowser for help on using the repository browser.