1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 PyDoc_STRVAR(memoryview__doc__,
6 "memoryview(object)\n"
7 "--\n"
8 "\n"
9 "Create a new memoryview object which references the given object.");
10
11 static PyObject *
12 memoryview_impl(PyTypeObject *type, PyObject *object);
13
14 static PyObject *
memoryview(PyTypeObject * type,PyObject * args,PyObject * kwargs)15 memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs)
16 {
17 PyObject *return_value = NULL;
18 static const char * const _keywords[] = {"object", NULL};
19 static _PyArg_Parser _parser = {NULL, _keywords, "memoryview", 0};
20 PyObject *argsbuf[1];
21 PyObject * const *fastargs;
22 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
23 PyObject *object;
24
25 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
26 if (!fastargs) {
27 goto exit;
28 }
29 object = fastargs[0];
30 return_value = memoryview_impl(type, object);
31
32 exit:
33 return return_value;
34 }
35
36 PyDoc_STRVAR(memoryview_release__doc__,
37 "release($self, /)\n"
38 "--\n"
39 "\n"
40 "Release the underlying buffer exposed by the memoryview object.");
41
42 #define MEMORYVIEW_RELEASE_METHODDEF \
43 {"release", (PyCFunction)memoryview_release, METH_NOARGS, memoryview_release__doc__},
44
45 static PyObject *
46 memoryview_release_impl(PyMemoryViewObject *self);
47
48 static PyObject *
memoryview_release(PyMemoryViewObject * self,PyObject * Py_UNUSED (ignored))49 memoryview_release(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
50 {
51 return memoryview_release_impl(self);
52 }
53
54 PyDoc_STRVAR(memoryview_cast__doc__,
55 "cast($self, /, format, shape=<unrepresentable>)\n"
56 "--\n"
57 "\n"
58 "Cast a memoryview to a new format or shape.");
59
60 #define MEMORYVIEW_CAST_METHODDEF \
61 {"cast", (PyCFunction)(void(*)(void))memoryview_cast, METH_FASTCALL|METH_KEYWORDS, memoryview_cast__doc__},
62
63 static PyObject *
64 memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
65 PyObject *shape);
66
67 static PyObject *
memoryview_cast(PyMemoryViewObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)68 memoryview_cast(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
69 {
70 PyObject *return_value = NULL;
71 static const char * const _keywords[] = {"format", "shape", NULL};
72 static _PyArg_Parser _parser = {NULL, _keywords, "cast", 0};
73 PyObject *argsbuf[2];
74 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
75 PyObject *format;
76 PyObject *shape = NULL;
77
78 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
79 if (!args) {
80 goto exit;
81 }
82 if (!PyUnicode_Check(args[0])) {
83 _PyArg_BadArgument("cast", "argument 'format'", "str", args[0]);
84 goto exit;
85 }
86 if (PyUnicode_READY(args[0]) == -1) {
87 goto exit;
88 }
89 format = args[0];
90 if (!noptargs) {
91 goto skip_optional_pos;
92 }
93 shape = args[1];
94 skip_optional_pos:
95 return_value = memoryview_cast_impl(self, format, shape);
96
97 exit:
98 return return_value;
99 }
100
101 PyDoc_STRVAR(memoryview_toreadonly__doc__,
102 "toreadonly($self, /)\n"
103 "--\n"
104 "\n"
105 "Return a readonly version of the memoryview.");
106
107 #define MEMORYVIEW_TOREADONLY_METHODDEF \
108 {"toreadonly", (PyCFunction)memoryview_toreadonly, METH_NOARGS, memoryview_toreadonly__doc__},
109
110 static PyObject *
111 memoryview_toreadonly_impl(PyMemoryViewObject *self);
112
113 static PyObject *
memoryview_toreadonly(PyMemoryViewObject * self,PyObject * Py_UNUSED (ignored))114 memoryview_toreadonly(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
115 {
116 return memoryview_toreadonly_impl(self);
117 }
118
119 PyDoc_STRVAR(memoryview_tolist__doc__,
120 "tolist($self, /)\n"
121 "--\n"
122 "\n"
123 "Return the data in the buffer as a list of elements.");
124
125 #define MEMORYVIEW_TOLIST_METHODDEF \
126 {"tolist", (PyCFunction)memoryview_tolist, METH_NOARGS, memoryview_tolist__doc__},
127
128 static PyObject *
129 memoryview_tolist_impl(PyMemoryViewObject *self);
130
131 static PyObject *
memoryview_tolist(PyMemoryViewObject * self,PyObject * Py_UNUSED (ignored))132 memoryview_tolist(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
133 {
134 return memoryview_tolist_impl(self);
135 }
136
137 PyDoc_STRVAR(memoryview_tobytes__doc__,
138 "tobytes($self, /, order=\'C\')\n"
139 "--\n"
140 "\n"
141 "Return the data in the buffer as a byte string.\n"
142 "\n"
143 "Order can be {\'C\', \'F\', \'A\'}. When order is \'C\' or \'F\', the data of the\n"
144 "original array is converted to C or Fortran order. For contiguous views,\n"
145 "\'A\' returns an exact copy of the physical memory. In particular, in-memory\n"
146 "Fortran order is preserved. For non-contiguous views, the data is converted\n"
147 "to C first. order=None is the same as order=\'C\'.");
148
149 #define MEMORYVIEW_TOBYTES_METHODDEF \
150 {"tobytes", (PyCFunction)(void(*)(void))memoryview_tobytes, METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
151
152 static PyObject *
153 memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
154
155 static PyObject *
memoryview_tobytes(PyMemoryViewObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)156 memoryview_tobytes(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
157 {
158 PyObject *return_value = NULL;
159 static const char * const _keywords[] = {"order", NULL};
160 static _PyArg_Parser _parser = {NULL, _keywords, "tobytes", 0};
161 PyObject *argsbuf[1];
162 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
163 const char *order = NULL;
164
165 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
166 if (!args) {
167 goto exit;
168 }
169 if (!noptargs) {
170 goto skip_optional_pos;
171 }
172 if (args[0] == Py_None) {
173 order = NULL;
174 }
175 else if (PyUnicode_Check(args[0])) {
176 Py_ssize_t order_length;
177 order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
178 if (order == NULL) {
179 goto exit;
180 }
181 if (strlen(order) != (size_t)order_length) {
182 PyErr_SetString(PyExc_ValueError, "embedded null character");
183 goto exit;
184 }
185 }
186 else {
187 _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
188 goto exit;
189 }
190 skip_optional_pos:
191 return_value = memoryview_tobytes_impl(self, order);
192
193 exit:
194 return return_value;
195 }
196
197 PyDoc_STRVAR(memoryview_hex__doc__,
198 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
199 "--\n"
200 "\n"
201 "Return the data in the buffer as a str of hexadecimal numbers.\n"
202 "\n"
203 " sep\n"
204 " An optional single character or byte to separate hex bytes.\n"
205 " bytes_per_sep\n"
206 " How many bytes between separators. Positive values count from the\n"
207 " right, negative values count from the left.\n"
208 "\n"
209 "Example:\n"
210 ">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
211 ">>> value.hex()\n"
212 "\'b901ef\'\n"
213 ">>> value.hex(\':\')\n"
214 "\'b9:01:ef\'\n"
215 ">>> value.hex(\':\', 2)\n"
216 "\'b9:01ef\'\n"
217 ">>> value.hex(\':\', -2)\n"
218 "\'b901:ef\'");
219
220 #define MEMORYVIEW_HEX_METHODDEF \
221 {"hex", (PyCFunction)(void(*)(void))memoryview_hex, METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
222
223 static PyObject *
224 memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
225 int bytes_per_sep);
226
227 static PyObject *
memoryview_hex(PyMemoryViewObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)228 memoryview_hex(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
229 {
230 PyObject *return_value = NULL;
231 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
232 static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0};
233 PyObject *argsbuf[2];
234 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
235 PyObject *sep = NULL;
236 int bytes_per_sep = 1;
237
238 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
239 if (!args) {
240 goto exit;
241 }
242 if (!noptargs) {
243 goto skip_optional_pos;
244 }
245 if (args[0]) {
246 sep = args[0];
247 if (!--noptargs) {
248 goto skip_optional_pos;
249 }
250 }
251 bytes_per_sep = _PyLong_AsInt(args[1]);
252 if (bytes_per_sep == -1 && PyErr_Occurred()) {
253 goto exit;
254 }
255 skip_optional_pos:
256 return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
257
258 exit:
259 return return_value;
260 }
261 /*[clinic end generated code: output=1b879bb934d18c66 input=a9049054013a1b77]*/
262