1/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(memoryview__doc__,
6"memoryview(object)\n"
7"--\n"
8"\n"
9"Create a new memoryview object which references the given object.");
10
11static PyObject *
12memoryview_impl(PyTypeObject *type, PyObject *object);
13
14static PyObject *
15memoryview(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
32exit:
33 return return_value;
34}
35
36PyDoc_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
45static PyObject *
46memoryview_release_impl(PyMemoryViewObject *self);
47
48static PyObject *
49memoryview_release(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
50{
51 return memoryview_release_impl(self);
52}
53
54PyDoc_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
63static PyObject *
64memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
65 PyObject *shape);
66
67static PyObject *
68memoryview_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];
94skip_optional_pos:
95 return_value = memoryview_cast_impl(self, format, shape);
96
97exit:
98 return return_value;
99}
100
101PyDoc_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
110static PyObject *
111memoryview_toreadonly_impl(PyMemoryViewObject *self);
112
113static PyObject *
114memoryview_toreadonly(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
115{
116 return memoryview_toreadonly_impl(self);
117}
118
119PyDoc_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
128static PyObject *
129memoryview_tolist_impl(PyMemoryViewObject *self);
130
131static PyObject *
132memoryview_tolist(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
133{
134 return memoryview_tolist_impl(self);
135}
136
137PyDoc_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
152static PyObject *
153memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
154
155static PyObject *
156memoryview_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 }
190skip_optional_pos:
191 return_value = memoryview_tobytes_impl(self, order);
192
193exit:
194 return return_value;
195}
196
197PyDoc_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
223static PyObject *
224memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
225 int bytes_per_sep);
226
227static PyObject *
228memoryview_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 }
255skip_optional_pos:
256 return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
257
258exit:
259 return return_value;
260}
261/*[clinic end generated code: output=1b879bb934d18c66 input=a9049054013a1b77]*/
262