source: swig/python/chunk.i @ 756011e

abac0-leak
Last change on this file since 756011e was 52ce995, checked in by Ted Faber <faber@…>, 11 years ago

Memory leak: python routine duplicates memory.

  • Property mode set to 100644
File size: 917 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            (Py_ssize_t *)&$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    $result = PyString_FromStringAndSize(
28        (const char *)$1.ptr,
29        $1.len
30    );
31    /* python duplicates the chunk memory, so we need to free it.  The chunk
32     * structure itself is on the stack.*/
33    free($1.ptr);
34%}
Note: See TracBrowser for help on using the repository browser.