summaryrefslogtreecommitdiffstats
path: root/contrib/tools/swig/Lib/python/pybuffer.i
blob: 9ebc36a8c4e6b31243cad73e6b144c586dad855f (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Implementing buffer protocol typemaps */

/* %pybuffer_mutable_binary(TYPEMAP, SIZE)
 *
 * Macro for functions accept mutable buffer pointer with a size.
 * This can be used for both input and output. For example:
 * 
 *      %pybuffer_mutable_binary(char *buff, int size);
 *      void foo(char *buff, int size) {
 *        for(int i=0; i<size; ++i)
 *          buff[i]++;  
 *      }
 */

/* Note that in Py_LIMITED_API case we have no choice, but to use deprecated
 * functions, as they provides the only way to access buffer data with limited
 * API, which doesn't include Py_buffer definition. We also disable the
 * warnings about doing this because they're not useful in our case.
 */

%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
  int res; Py_ssize_t size = 0; void *buf = 0;
%#ifndef Py_LIMITED_API
  Py_buffer view;
  res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
%#else
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic push
    %#pragma GCC diagnostic ignored "-Wdeprecated"
    %#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  %#elif defined(_MSC_VER)
    %#pragma warning(push)
    %#pragma warning(disable: 4996)
  %#endif
  res = PyObject_AsWriteBuffer($input, &buf, &size);
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic pop
  %#elif defined(_MSC_VER)
    %#pragma warning(pop)
  %#endif
%#endif
  if (res < 0) {
    PyErr_Clear();
    %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  }
%#ifndef Py_LIMITED_API
  size = view.len;
  buf = view.buf;
  PyBuffer_Release(&view);
%#endif
  $1 = ($1_ltype) buf;
  $2 = ($2_ltype) (size/sizeof($*1_type));
}
%enddef

/* %pybuffer_mutable_string(TYPEMAP)
 *
 * Macro for functions accept mutable zero terminated string pointer.
 * This can be used for both input and output. For example:
 * 
 *      %pybuffer_mutable_string(char *str);
 *      void foo(char *str) {
 *        while(*str) {
 *          *str = toupper(*str);
 *          str++;
 *      }
 */

%define %pybuffer_mutable_string(TYPEMAP)
%typemap(in) (TYPEMAP) {
  int res; void *buf = 0;
%#ifndef Py_LIMITED_API
  Py_buffer view;
  res = PyObject_GetBuffer($input, &view, PyBUF_WRITABLE);
%#else
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic push
    %#pragma GCC diagnostic ignored "-Wdeprecated"
    %#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  %#elif defined(_MSC_VER)
    %#pragma warning(push)
    %#pragma warning(disable: 4996)
  %#endif
  Py_ssize_t size;
  res = PyObject_AsWriteBuffer($input, &buf, &size);
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic pop
  %#elif defined(_MSC_VER)
    %#pragma warning(pop)
  %#endif
%#endif
  if (res < 0) {
    PyErr_Clear();
    %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
  }
%#ifndef Py_LIMITED_API
  buf = view.buf;
  PyBuffer_Release(&view);
%#endif
  $1 = ($1_ltype) buf;
}
%enddef

/* pybuffer_binary(TYPEMAP, SIZE)
 *
 * Macro for functions accept read only buffer pointer with a size.
 * This must be used for input. For example:
 * 
 *      %pybuffer_binary(char *buff, int size);
 *      int foo(char *buff, int size) {
 *        int count = 0;
 *        for(int i=0; i<size; ++i)
 *          if (0==buff[i]) count++;
 *        return count;
 *      }
 */

%define %pybuffer_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) {
  int res; Py_ssize_t size = 0; const void *buf = 0;
%#ifndef Py_LIMITED_API
  Py_buffer view;
  res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
%#else
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic push
    %#pragma GCC diagnostic ignored "-Wdeprecated"
    %#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  %#elif defined(_MSC_VER)
    %#pragma warning(push)
    %#pragma warning(disable: 4996)
  %#endif
  res = PyObject_AsReadBuffer($input, &buf, &size);
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic pop
  %#elif defined(_MSC_VER)
    %#pragma warning(pop)
  %#endif
%#endif
  if (res < 0) {
    PyErr_Clear();
    %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  }
%#ifndef Py_LIMITED_API
  size = view.len;
  buf = view.buf;
  PyBuffer_Release(&view);
%#endif
  $1 = ($1_ltype) buf;
  $2 = ($2_ltype) (size / sizeof($*1_type));
}
%enddef

/* %pybuffer_string(TYPEMAP)
 *
 * Macro for functions accept read only zero terminated string pointer.
 * This can be used for input. For example:
 * 
 *      %pybuffer_string(char *str);
 *      int foo(char *str) {
 *        int count = 0;
 *        while(*str) {
 *          if (isalnum(*str))
 *            count++;
 *          str++;
 *      }
 */

%define %pybuffer_string(TYPEMAP)
%typemap(in) (TYPEMAP) {
  int res; const void *buf = 0;
%#ifndef Py_LIMITED_API
  Py_buffer view;
  res = PyObject_GetBuffer($input, &view, PyBUF_CONTIG_RO);
%#else
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic push
    %#pragma GCC diagnostic ignored "-Wdeprecated"
    %#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  %#elif defined(_MSC_VER)
    %#pragma warning(push)
    %#pragma warning(disable: 4996)
  %#endif
  Py_ssize_t size;
  res = PyObject_AsReadBuffer($input, &buf, &size);
  %#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
    %#pragma GCC diagnostic pop
  %#elif defined(_MSC_VER)
    %#pragma warning(pop)
  %#endif
%#endif
  if (res < 0) {
    PyErr_Clear();
    %argument_fail(res, "(TYPEMAP)", $symname, $argnum);
  }
%#ifndef Py_LIMITED_API
  buf = view.buf;
  PyBuffer_Release(&view);
%#endif
  $1 = ($1_ltype) buf;
}
%enddef