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\n"
262 "the original array is converted to C or Fortran order. For\n"
263 "contiguous views, \'A\' returns an exact copy of the physical memory.\n"
264 "In particular, in-memory Fortran order is preserved. For\n"
265 "non-contiguous views, the data is converted to C first. order=None\n"
266 "is the same as order=\'C\'.");
267
268 #define MEMORYVIEW_TOBYTES_METHODDEF \
269 {"tobytes", _PyCFunction_CAST(memoryview_tobytes), METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
270
271 static PyObject *
272 memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
273
274 static PyObject *
275 memoryview_tobytes(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
276 {
277 PyObject *return_value = NULL;
278 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
279
280 #define NUM_KEYWORDS 1
281 static struct {
282 PyGC_Head _this_is_not_used;
283 PyObject_VAR_HEAD
284 Py_hash_t ob_hash;
285 PyObject *ob_item[NUM_KEYWORDS];
286 } _kwtuple = {
287 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
288 .ob_hash = -1,
289 .ob_item = { &_Py_ID(order), },
290 };
291 #undef NUM_KEYWORDS
292 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
293
294 #else // !Py_BUILD_CORE
295 # define KWTUPLE NULL
296 #endif // !Py_BUILD_CORE
297
298 static const char * const _keywords[] = {"order", NULL};
299 static _PyArg_Parser _parser = {
300 .keywords = _keywords,
301 .fname = "tobytes",
302 .kwtuple = KWTUPLE,
303 };
304 #undef KWTUPLE
305 PyObject *argsbuf[1];
306 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
307 const char *order = NULL;
308
309 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
310 /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
311 if (!args) {
312 goto exit;
313 }
314 if (!noptargs) {
315 goto skip_optional_pos;
316 }
317 if (args[0] == Py_None) {
318 order = NULL;
319 }
320 else if (PyUnicode_Check(args[0])) {
321 Py_ssize_t order_length;
322 order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
323 if (order == NULL) {
324 goto exit;
325 }
326 if (strlen(order) != (size_t)order_length) {
327 PyErr_SetString(PyExc_ValueError, "embedded null character");
328 goto exit;
329 }
330 }
331 else {
332 _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
333 goto exit;
334 }
335 skip_optional_pos:
336 return_value = memoryview_tobytes_impl((PyMemoryViewObject *)self, order);
337
338 exit:
339 return return_value;
340 }
341
342 PyDoc_STRVAR(memoryview_hex__doc__,
343 "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
344 "--\n"
345 "\n"
346 "Return the data in the buffer as a str of hexadecimal numbers.\n"
347 "\n"
348 " sep\n"
349 " An optional single character or byte to separate hex bytes.\n"
350 " bytes_per_sep\n"
351 " How many bytes between separators. Positive values count from\n"
352 " the right, negative values count from the left.\n"
353 "\n"
354 "Example:\n"
355 ">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
356 ">>> value.hex()\n"
357 "\'b901ef\'\n"
358 ">>> value.hex(\':\')\n"
359 "\'b9:01:ef\'\n"
360 ">>> value.hex(\':\', 2)\n"
361 "\'b9:01ef\'\n"
362 ">>> value.hex(\':\', -2)\n"
363 "\'b901:ef\'");
364
365 #define MEMORYVIEW_HEX_METHODDEF \
366 {"hex", _PyCFunction_CAST(memoryview_hex), METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
367
368 static PyObject *
369 memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
370 int bytes_per_sep);
371
372 static PyObject *
373 memoryview_hex(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
374 {
375 PyObject *return_value = NULL;
376 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
377
378 #define NUM_KEYWORDS 2
379 static struct {
380 PyGC_Head _this_is_not_used;
381 PyObject_VAR_HEAD
382 Py_hash_t ob_hash;
383 PyObject *ob_item[NUM_KEYWORDS];
384 } _kwtuple = {
385 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
386 .ob_hash = -1,
387 .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
388 };
389 #undef NUM_KEYWORDS
390 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
391
392 #else // !Py_BUILD_CORE
393 # define KWTUPLE NULL
394 #endif // !Py_BUILD_CORE
395
396 static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
397 static _PyArg_Parser _parser = {
398 .keywords = _keywords,
399 .fname = "hex",
400 .kwtuple = KWTUPLE,
401 };
402 #undef KWTUPLE
403 PyObject *argsbuf[2];
404 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
405 PyObject *sep = NULL;
406 int bytes_per_sep = 1;
407
408 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
409 /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
410 if (!args) {
411 goto exit;
412 }
413 if (!noptargs) {
414 goto skip_optional_pos;
415 }
416 if (args[0]) {
417 sep = args[0];
418 if (!--noptargs) {
419 goto skip_optional_pos;
420 }
421 }
422 bytes_per_sep = PyLong_AsInt(args[1]);
423 if (bytes_per_sep == -1 && PyErr_Occurred()) {
424 goto exit;
425 }
426 skip_optional_pos:
427 return_value = memoryview_hex_impl((PyMemoryViewObject *)self, sep, bytes_per_sep);
428
429 exit:
430 return return_value;
431 }
432
433 PyDoc_STRVAR(memoryview_count__doc__,
434 "count($self, value, /)\n"
435 "--\n"
436 "\n"
437 "Count the number of occurrences of a value.");
438
439 #define MEMORYVIEW_COUNT_METHODDEF \
440 {"count", (PyCFunction)memoryview_count, METH_O, memoryview_count__doc__},
441
442 static PyObject *
443 memoryview_count_impl(PyMemoryViewObject *self, PyObject *value);
444
445 static PyObject *
446 memoryview_count(PyObject *self, PyObject *value)
447 {
448 PyObject *return_value = NULL;
449
450 return_value = memoryview_count_impl((PyMemoryViewObject *)self, value);
451
452 return return_value;
453 }
454
455 PyDoc_STRVAR(memoryview_index__doc__,
456 "index($self, value, start=0, stop=sys.maxsize, /)\n"
457 "--\n"
458 "\n"
459 "Return the index of the first occurrence of a value.\n"
460 "\n"
461 "Raises ValueError if the value is not present.");
462
463 #define MEMORYVIEW_INDEX_METHODDEF \
464 {"index", _PyCFunction_CAST(memoryview_index), METH_FASTCALL, memoryview_index__doc__},
465
466 static PyObject *
467 memoryview_index_impl(PyMemoryViewObject *self, PyObject *value,
468 Py_ssize_t start, Py_ssize_t stop);
469
470 static PyObject *
471 memoryview_index(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
472 {
473 PyObject *return_value = NULL;
474 PyObject *value;
475 Py_ssize_t start = 0;
476 Py_ssize_t stop = PY_SSIZE_T_MAX;
477
478 if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
479 goto exit;
480 }
481 value = args[0];
482 if (nargs < 2) {
483 goto skip_optional;
484 }
485 if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
486 goto exit;
487 }
488 if (nargs < 3) {
489 goto skip_optional;
490 }
491 if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
492 goto exit;
493 }
494 skip_optional:
495 return_value = memoryview_index_impl((PyMemoryViewObject *)self, value, start, stop);
496
497 exit:
498 return return_value;
499 }
500 /*[clinic end generated code: output=6d0886bee65a8f9f input=a9049054013a1b77]*/