Debian Codegraph @

Objects/clinic/memoryobject.c.h

raw
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
10
11 PyDoc_STRVAR(memoryview__doc__,
12 "memoryview(object)\n"
13 "--\n"
14 "\n"
15 "Create a new memoryview object which references the given object.");
16
17 static PyObject *
18 memoryview_impl(PyTypeObject *type, PyObject *object);
19
20 static PyObject *
21 memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs)
22 {
23 PyObject *return_value = NULL;
24 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
25
26 #define NUM_KEYWORDS 1
27 static struct {
28 PyGC_Head _this_is_not_used;
29 PyObject_VAR_HEAD
30 Py_hash_t ob_hash;
31 PyObject *ob_item[NUM_KEYWORDS];
32 } _kwtuple = {
33 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
34 .ob_hash = -1,
35 .ob_item = { &_Py_ID(object), },
36 };
37 #undef NUM_KEYWORDS
38 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
39
40 #else // !Py_BUILD_CORE
41 # define KWTUPLE NULL
42 #endif // !Py_BUILD_CORE
43
44 static const char * const _keywords[] = {"object", NULL};
45 static _PyArg_Parser _parser = {
46 .keywords = _keywords,
47 .fname = "memoryview",
48 .kwtuple = KWTUPLE,
49 };
50 #undef KWTUPLE
51 PyObject *argsbuf[1];
52 PyObject * const *fastargs;
53 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
54 PyObject *object;
55
56 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
57 /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
58 if (!fastargs) {
59 goto exit;
60 }
61 object = fastargs[0];
62 return_value = memoryview_impl(type, object);
63
64 exit:
65 return return_value;
66 }
67
68 PyDoc_STRVAR(memoryview__from_flags__doc__,
69 "_from_flags($type, /, object, flags)\n"
70 "--\n"
71 "\n"
72 "Create a new memoryview object which references the given object.");
73
74 #define MEMORYVIEW__FROM_FLAGS_METHODDEF \
75 {"_from_flags", _PyCFunction_CAST(memoryview__from_flags), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, memoryview__from_flags__doc__},
76
77 static PyObject *
78 memoryview__from_flags_impl(PyTypeObject *type, PyObject *object, int flags);
79
80 static PyObject *
81 memoryview__from_flags(PyObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
82 {
83 PyObject *return_value = NULL;
84 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
85
86 #define NUM_KEYWORDS 2
87 static struct {
88 PyGC_Head _this_is_not_used;
89 PyObject_VAR_HEAD
90 Py_hash_t ob_hash;
91 PyObject *ob_item[NUM_KEYWORDS];
92 } _kwtuple = {
93 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
94 .ob_hash = -1,
95 .ob_item = { &_Py_ID(object), &_Py_ID(flags), },
96 };
97 #undef NUM_KEYWORDS
98 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
99
100 #else // !Py_BUILD_CORE
101 # define KWTUPLE NULL
102 #endif // !Py_BUILD_CORE
103
104 static const char * const _keywords[] = {"object", "flags", NULL};
105 static _PyArg_Parser _parser = {
106 .keywords = _keywords,
107 .fname = "_from_flags",
108 .kwtuple = KWTUPLE,
109 };
110 #undef KWTUPLE
111 PyObject *argsbuf[2];
112 PyObject *object;
113 int flags;
114
115 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
116 /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
117 if (!args) {
118 goto exit;
119 }
120 object = args[0];
121 flags = PyLong_AsInt(args[1]);
122 if (flags == -1 && PyErr_Occurred()) {
123 goto exit;
124 }
125 return_value = memoryview__from_flags_impl((PyTypeObject *)type, object, flags);
126
127 exit:
128 return return_value;
129 }
130
131 PyDoc_STRVAR(memoryview_release__doc__,
132 "release($self, /)\n"
133 "--\n"
134 "\n"
135 "Release the underlying buffer exposed by the memoryview object.");
136
137 #define MEMORYVIEW_RELEASE_METHODDEF \
138 {"release", (PyCFunction)memoryview_release, METH_NOARGS, memoryview_release__doc__},
139
140 static PyObject *
141 memoryview_release_impl(PyMemoryViewObject *self);
142
143 static PyObject *
144 memoryview_release(PyObject *self, PyObject *Py_UNUSED(ignored))
145 {
146 return memoryview_release_impl((PyMemoryViewObject *)self);
147 }
148
149 PyDoc_STRVAR(memoryview_cast__doc__,
150 "cast($self, /, format, shape=<unrepresentable>)\n"
151 "--\n"
152 "\n"
153 "Cast a memoryview to a new format or shape.");
154
155 #define MEMORYVIEW_CAST_METHODDEF \
156 {"cast", _PyCFunction_CAST(memoryview_cast), METH_FASTCALL|METH_KEYWORDS, memoryview_cast__doc__},
157
158 static PyObject *
159 memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
160 PyObject *shape);
161
162 static PyObject *
163 memoryview_cast(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
164 {
165 PyObject *return_value = NULL;
166 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
167
168 #define NUM_KEYWORDS 2
169 static struct {
170 PyGC_Head _this_is_not_used;
171 PyObject_VAR_HEAD
172 Py_hash_t ob_hash;
173 PyObject *ob_item[NUM_KEYWORDS];
174 } _kwtuple = {
175 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
176 .ob_hash = -1,
177 .ob_item = { &_Py_ID(format), &_Py_ID(shape), },
178 };
179 #undef NUM_KEYWORDS
180 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
181
182 #else // !Py_BUILD_CORE
183 # define KWTUPLE NULL
184 #endif // !Py_BUILD_CORE
185
186 static const char * const _keywords[] = {"format", "shape", NULL};
187 static _PyArg_Parser _parser = {
188 .keywords = _keywords,
189 .fname = "cast",
190 .kwtuple = KWTUPLE,
191 };
192 #undef KWTUPLE
193 PyObject *argsbuf[2];
194 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
195 PyObject *format;
196 PyObject *shape = NULL;
197
198 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
199 /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
200 if (!args) {
201 goto exit;
202 }
203 if (!PyUnicode_Check(args[0])) {
204 _PyArg_BadArgument("cast", "argument 'format'", "str", args[0]);
205 goto exit;
206 }
207 format = args[0];
208 if (!noptargs) {
209 goto skip_optional_pos;
210 }
211 shape = args[1];
212 skip_optional_pos:
213 return_value = memoryview_cast_impl((PyMemoryViewObject *)self, format, shape);
214
215 exit:
216 return return_value;
217 }
218
219 PyDoc_STRVAR(memoryview_toreadonly__doc__,
220 "toreadonly($self, /)\n"
221 "--\n"
222 "\n"
223 "Return a readonly version of the memoryview.");
224
225 #define MEMORYVIEW_TOREADONLY_METHODDEF \
226 {"toreadonly", (PyCFunction)memoryview_toreadonly, METH_NOARGS, memoryview_toreadonly__doc__},
227
228 static PyObject *
229 memoryview_toreadonly_impl(PyMemoryViewObject *self);
230
231 static PyObject *
232 memoryview_toreadonly(PyObject *self, PyObject *Py_UNUSED(ignored))
233 {
234 return memoryview_toreadonly_impl((PyMemoryViewObject *)self);
235 }
236
237 PyDoc_STRVAR(memoryview_tolist__doc__,
238 "tolist($self, /)\n"
239 "--\n"
240 "\n"
241 "Return the data in the buffer as a list of elements.");
242
243 #define MEMORYVIEW_TOLIST_METHODDEF \
244 {"tolist", (PyCFunction)memoryview_tolist, METH_NOARGS, memoryview_tolist__doc__},
245
246 static PyObject *
247 memoryview_tolist_impl(PyMemoryViewObject *self);
248
249 static PyObject *
250 memoryview_tolist(PyObject *self, PyObject *Py_UNUSED(ignored))
251 {
252 return memoryview_tolist_impl((PyMemoryViewObject *)self);
253 }
254
255 PyDoc_STRVAR(memoryview_tobytes__doc__,
256 "tobytes($self, /, order=\'C\')\n"
257 "--\n"
258 "\n"
259 "Return the data in the buffer as a byte string.\n"
260 "\n"
261 "Order can be {\'C\', \'F\', \'A\'}. When order is \'C\' or \'F\', the data of the\n"
262 "original array is converted to C or Fortran order. For contiguous views,\n"
263 "\'A\' returns an exact copy of the physical memory. In particular, in-memory\n"
264 "Fortran order is preserved. For non-contiguous views, the data is converted\n"
265 "to C first. order=None is the same as order=\'C\'.");
266
267 #define MEMORYVIEW_TOBYTES_METHODDEF \
268 {"tobytes", _PyCFunction_CAST(memoryview_tobytes), METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
269
270 static PyObject *
271 memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
272
273 static PyObject *
274 memoryview_tobytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
275 {
276 PyObject *return_value = NULL;
277 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
278
279 #define NUM_KEYWORDS 1
280 static struct {
281 PyGC_Head _this_is_not_used;
282 PyObject_VAR_HEAD
283 Py_hash_t ob_hash;
284 PyObject *ob_item[NUM_KEYWORDS];
285 } _kwtuple = {
286 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
287 .ob_hash = -1,
288 .ob_item = { &_Py_ID(order), },
289 };
290 #undef NUM_KEYWORDS
291 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
292
293 #else // !Py_BUILD_CORE
294 # define KWTUPLE NULL
295 #endif // !Py_BUILD_CORE
296
297 static const char * const _keywords[] = {"order", NULL};
298 static _PyArg_Parser _parser = {
299 .keywords = _keywords,
300 .fname = "tobytes",
301 .kwtuple = KWTUPLE,
302 };
303 #undef KWTUPLE
304 PyObject *argsbuf[1];
305 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
306 const char *order = NULL;
307
308 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
309 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
310 if (!args) {
311 goto exit;
312 }
313 if (!noptargs) {
314 goto skip_optional_pos;
315 }
316 if (args[0] == Py_None) {
317 order = NULL;
318 }
319 else if (PyUnicode_Check(args[0])) {
320 Py_ssize_t order_length;
321 order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
322 if (order == NULL) {
323 goto exit;
324 }
325 if (strlen(order) != (size_t)order_length) {
326 PyErr_SetString(PyExc_ValueError, "embedded null character");
327 goto exit;
328 }
329 }
330 else {
331 _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
332 goto exit;
333 }
334 skip_optional_pos:
335 return_value = memoryview_tobytes_impl((PyMemoryViewObject *)self, order);
336
337 exit:
338 return return_value;
339 }
340
341 PyDoc_STRVAR(memoryview_hex__doc__,
342 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
343 "--\n"
344 "\n"
345 "Return the data in the buffer as a str of hexadecimal numbers.\n"
346 "\n"
347 " sep\n"
348 " An optional single character or byte to separate hex bytes.\n"
349 " bytes_per_sep\n"
350 " How many bytes between separators. Positive values count from the\n"
351 " right, negative values count from the left.\n"
352 "\n"
353 "Example:\n"
354 ">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
355 ">>> value.hex()\n"
356 "\'b901ef\'\n"
357 ">>> value.hex(\':\')\n"
358 "\'b9:01:ef\'\n"
359 ">>> value.hex(\':\', 2)\n"
360 "\'b9:01ef\'\n"
361 ">>> value.hex(\':\', -2)\n"
362 "\'b901:ef\'");
363
364 #define MEMORYVIEW_HEX_METHODDEF \
365 {"hex", _PyCFunction_CAST(memoryview_hex), METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
366
367 static PyObject *
368 memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
369 int bytes_per_sep);
370
371 static PyObject *
372 memoryview_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
373 {
374 PyObject *return_value = NULL;
375 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
376
377 #define NUM_KEYWORDS 2
378 static struct {
379 PyGC_Head _this_is_not_used;
380 PyObject_VAR_HEAD
381 Py_hash_t ob_hash;
382 PyObject *ob_item[NUM_KEYWORDS];
383 } _kwtuple = {
384 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
385 .ob_hash = -1,
386 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
387 };
388 #undef NUM_KEYWORDS
389 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
390
391 #else // !Py_BUILD_CORE
392 # define KWTUPLE NULL
393 #endif // !Py_BUILD_CORE
394
395 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
396 static _PyArg_Parser _parser = {
397 .keywords = _keywords,
398 .fname = "hex",
399 .kwtuple = KWTUPLE,
400 };
401 #undef KWTUPLE
402 PyObject *argsbuf[2];
403 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
404 PyObject *sep = NULL;
405 int bytes_per_sep = 1;
406
407 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
408 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
409 if (!args) {
410 goto exit;
411 }
412 if (!noptargs) {
413 goto skip_optional_pos;
414 }
415 if (args[0]) {
416 sep = args[0];
417 if (!--noptargs) {
418 goto skip_optional_pos;
419 }
420 }
421 bytes_per_sep = PyLong_AsInt(args[1]);
422 if (bytes_per_sep == -1 && PyErr_Occurred()) {
423 goto exit;
424 }
425 skip_optional_pos:
426 return_value = memoryview_hex_impl((PyMemoryViewObject *)self, sep, bytes_per_sep);
427
428 exit:
429 return return_value;
430 }
431
432 PyDoc_STRVAR(memoryview_count__doc__,
433 "count($self, value, /)\n"
434 "--\n"
435 "\n"
436 "Count the number of occurrences of a value.");
437
438 #define MEMORYVIEW_COUNT_METHODDEF \
439 {"count", (PyCFunction)memoryview_count, METH_O, memoryview_count__doc__},
440
441 static PyObject *
442 memoryview_count_impl(PyMemoryViewObject *self, PyObject *value);
443
444 static PyObject *
445 memoryview_count(PyObject *self, PyObject *value)
446 {
447 PyObject *return_value = NULL;
448
449 return_value = memoryview_count_impl((PyMemoryViewObject *)self, value);
450
451 return return_value;
452 }
453
454 PyDoc_STRVAR(memoryview_index__doc__,
455 "index($self, value, start=0, stop=sys.maxsize, /)\n"
456 "--\n"
457 "\n"
458 "Return the index of the first occurrence of a value.\n"
459 "\n"
460 "Raises ValueError if the value is not present.");
461
462 #define MEMORYVIEW_INDEX_METHODDEF \
463 {"index", _PyCFunction_CAST(memoryview_index), METH_FASTCALL, memoryview_index__doc__},
464
465 static PyObject *
466 memoryview_index_impl(PyMemoryViewObject *self, PyObject *value,
467 Py_ssize_t start, Py_ssize_t stop);
468
469 static PyObject *
470 memoryview_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
471 {
472 PyObject *return_value = NULL;
473 PyObject *value;
474 Py_ssize_t start = 0;
475 Py_ssize_t stop = PY_SSIZE_T_MAX;
476
477 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
478 goto exit;
479 }
480 value = args[0];
481 if (nargs < 2) {
482 goto skip_optional;
483 }
484 if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
485 goto exit;
486 }
487 if (nargs < 3) {
488 goto skip_optional;
489 }
490 if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
491 goto exit;
492 }
493 skip_optional:
494 return_value = memoryview_index_impl((PyMemoryViewObject *)self, value, start, stop);
495
496 exit:
497 return return_value;
498 }
499 /*[clinic end generated code: output=154f4c04263ccb24 input=a9049054013a1b77]*/