Coverage Report

Created: 2025-07-04 06:49

/src/cpython/Objects/clinic/memoryobject.c.h
Line
Count
Source (jump to first uncovered line)
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
730
{
23
730
    PyObject *return_value = NULL;
24
730
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
25
26
730
    #define NUM_KEYWORDS 1
27
730
    static struct {
28
730
        PyGC_Head _this_is_not_used;
29
730
        PyObject_VAR_HEAD
30
730
        Py_hash_t ob_hash;
31
730
        PyObject *ob_item[NUM_KEYWORDS];
32
730
    } _kwtuple = {
33
730
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
34
730
        .ob_hash = -1,
35
730
        .ob_item = { &_Py_ID(object), },
36
730
    };
37
730
    #undef NUM_KEYWORDS
38
730
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
39
40
    #else  // !Py_BUILD_CORE
41
    #  define KWTUPLE NULL
42
    #endif  // !Py_BUILD_CORE
43
44
730
    static const char * const _keywords[] = {"object", NULL};
45
730
    static _PyArg_Parser _parser = {
46
730
        .keywords = _keywords,
47
730
        .fname = "memoryview",
48
730
        .kwtuple = KWTUPLE,
49
730
    };
50
730
    #undef KWTUPLE
51
730
    PyObject *argsbuf[1];
52
730
    PyObject * const *fastargs;
53
730
    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
54
730
    PyObject *object;
55
56
730
    fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,
57
730
            /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
58
730
    if (!fastargs) {
59
0
        goto exit;
60
0
    }
61
730
    object = fastargs[0];
62
730
    return_value = memoryview_impl(type, object);
63
64
730
exit:
65
730
    return return_value;
66
730
}
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
0
{
83
0
    PyObject *return_value = NULL;
84
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
85
86
0
    #define NUM_KEYWORDS 2
87
0
    static struct {
88
0
        PyGC_Head _this_is_not_used;
89
0
        PyObject_VAR_HEAD
90
0
        Py_hash_t ob_hash;
91
0
        PyObject *ob_item[NUM_KEYWORDS];
92
0
    } _kwtuple = {
93
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
94
0
        .ob_hash = -1,
95
0
        .ob_item = { &_Py_ID(object), &_Py_ID(flags), },
96
0
    };
97
0
    #undef NUM_KEYWORDS
98
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
99
100
    #else  // !Py_BUILD_CORE
101
    #  define KWTUPLE NULL
102
    #endif  // !Py_BUILD_CORE
103
104
0
    static const char * const _keywords[] = {"object", "flags", NULL};
105
0
    static _PyArg_Parser _parser = {
106
0
        .keywords = _keywords,
107
0
        .fname = "_from_flags",
108
0
        .kwtuple = KWTUPLE,
109
0
    };
110
0
    #undef KWTUPLE
111
0
    PyObject *argsbuf[2];
112
0
    PyObject *object;
113
0
    int flags;
114
115
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
116
0
            /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
117
0
    if (!args) {
118
0
        goto exit;
119
0
    }
120
0
    object = args[0];
121
0
    flags = PyLong_AsInt(args[1]);
122
0
    if (flags == -1 && PyErr_Occurred()) {
123
0
        goto exit;
124
0
    }
125
0
    return_value = memoryview__from_flags_impl((PyTypeObject *)type, object, flags);
126
127
0
exit:
128
0
    return return_value;
129
0
}
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
0
{
146
0
    return memoryview_release_impl((PyMemoryViewObject *)self);
147
0
}
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
4
{
165
4
    PyObject *return_value = NULL;
166
4
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
167
168
4
    #define NUM_KEYWORDS 2
169
4
    static struct {
170
4
        PyGC_Head _this_is_not_used;
171
4
        PyObject_VAR_HEAD
172
4
        Py_hash_t ob_hash;
173
4
        PyObject *ob_item[NUM_KEYWORDS];
174
4
    } _kwtuple = {
175
4
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
176
4
        .ob_hash = -1,
177
4
        .ob_item = { &_Py_ID(format), &_Py_ID(shape), },
178
4
    };
179
4
    #undef NUM_KEYWORDS
180
4
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
181
182
    #else  // !Py_BUILD_CORE
183
    #  define KWTUPLE NULL
184
    #endif  // !Py_BUILD_CORE
185
186
4
    static const char * const _keywords[] = {"format", "shape", NULL};
187
4
    static _PyArg_Parser _parser = {
188
4
        .keywords = _keywords,
189
4
        .fname = "cast",
190
4
        .kwtuple = KWTUPLE,
191
4
    };
192
4
    #undef KWTUPLE
193
4
    PyObject *argsbuf[2];
194
4
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
195
4
    PyObject *format;
196
4
    PyObject *shape = NULL;
197
198
4
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
199
4
            /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
200
4
    if (!args) {
201
0
        goto exit;
202
0
    }
203
4
    if (!PyUnicode_Check(args[0])) {
204
0
        _PyArg_BadArgument("cast", "argument 'format'", "str", args[0]);
205
0
        goto exit;
206
0
    }
207
4
    format = args[0];
208
4
    if (!noptargs) {
209
4
        goto skip_optional_pos;
210
4
    }
211
0
    shape = args[1];
212
4
skip_optional_pos:
213
4
    return_value = memoryview_cast_impl((PyMemoryViewObject *)self, format, shape);
214
215
4
exit:
216
4
    return return_value;
217
4
}
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
0
{
234
0
    return memoryview_toreadonly_impl((PyMemoryViewObject *)self);
235
0
}
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
4
{
252
4
    return memoryview_tolist_impl((PyMemoryViewObject *)self);
253
4
}
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
0
{
276
0
    PyObject *return_value = NULL;
277
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
278
279
0
    #define NUM_KEYWORDS 1
280
0
    static struct {
281
0
        PyGC_Head _this_is_not_used;
282
0
        PyObject_VAR_HEAD
283
0
        Py_hash_t ob_hash;
284
0
        PyObject *ob_item[NUM_KEYWORDS];
285
0
    } _kwtuple = {
286
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
287
0
        .ob_hash = -1,
288
0
        .ob_item = { &_Py_ID(order), },
289
0
    };
290
0
    #undef NUM_KEYWORDS
291
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
292
293
    #else  // !Py_BUILD_CORE
294
    #  define KWTUPLE NULL
295
    #endif  // !Py_BUILD_CORE
296
297
0
    static const char * const _keywords[] = {"order", NULL};
298
0
    static _PyArg_Parser _parser = {
299
0
        .keywords = _keywords,
300
0
        .fname = "tobytes",
301
0
        .kwtuple = KWTUPLE,
302
0
    };
303
0
    #undef KWTUPLE
304
0
    PyObject *argsbuf[1];
305
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
306
0
    const char *order = NULL;
307
308
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
309
0
            /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
310
0
    if (!args) {
311
0
        goto exit;
312
0
    }
313
0
    if (!noptargs) {
314
0
        goto skip_optional_pos;
315
0
    }
316
0
    if (args[0] == Py_None) {
317
0
        order = NULL;
318
0
    }
319
0
    else if (PyUnicode_Check(args[0])) {
320
0
        Py_ssize_t order_length;
321
0
        order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
322
0
        if (order == NULL) {
323
0
            goto exit;
324
0
        }
325
0
        if (strlen(order) != (size_t)order_length) {
326
0
            PyErr_SetString(PyExc_ValueError, "embedded null character");
327
0
            goto exit;
328
0
        }
329
0
    }
330
0
    else {
331
0
        _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
332
0
        goto exit;
333
0
    }
334
0
skip_optional_pos:
335
0
    return_value = memoryview_tobytes_impl((PyMemoryViewObject *)self, order);
336
337
0
exit:
338
0
    return return_value;
339
0
}
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
0
{
374
0
    PyObject *return_value = NULL;
375
0
    #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
376
377
0
    #define NUM_KEYWORDS 2
378
0
    static struct {
379
0
        PyGC_Head _this_is_not_used;
380
0
        PyObject_VAR_HEAD
381
0
        Py_hash_t ob_hash;
382
0
        PyObject *ob_item[NUM_KEYWORDS];
383
0
    } _kwtuple = {
384
0
        .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
385
0
        .ob_hash = -1,
386
0
        .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
387
0
    };
388
0
    #undef NUM_KEYWORDS
389
0
    #define KWTUPLE (&_kwtuple.ob_base.ob_base)
390
391
    #else  // !Py_BUILD_CORE
392
    #  define KWTUPLE NULL
393
    #endif  // !Py_BUILD_CORE
394
395
0
    static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
396
0
    static _PyArg_Parser _parser = {
397
0
        .keywords = _keywords,
398
0
        .fname = "hex",
399
0
        .kwtuple = KWTUPLE,
400
0
    };
401
0
    #undef KWTUPLE
402
0
    PyObject *argsbuf[2];
403
0
    Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
404
0
    PyObject *sep = NULL;
405
0
    int bytes_per_sep = 1;
406
407
0
    args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
408
0
            /*minpos*/ 0, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
409
0
    if (!args) {
410
0
        goto exit;
411
0
    }
412
0
    if (!noptargs) {
413
0
        goto skip_optional_pos;
414
0
    }
415
0
    if (args[0]) {
416
0
        sep = args[0];
417
0
        if (!--noptargs) {
418
0
            goto skip_optional_pos;
419
0
        }
420
0
    }
421
0
    bytes_per_sep = PyLong_AsInt(args[1]);
422
0
    if (bytes_per_sep == -1 && PyErr_Occurred()) {
423
0
        goto exit;
424
0
    }
425
0
skip_optional_pos:
426
0
    return_value = memoryview_hex_impl((PyMemoryViewObject *)self, sep, bytes_per_sep);
427
428
0
exit:
429
0
    return return_value;
430
0
}
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
0
{
447
0
    PyObject *return_value = NULL;
448
449
0
    return_value = memoryview_count_impl((PyMemoryViewObject *)self, value);
450
451
0
    return return_value;
452
0
}
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
0
{
472
0
    PyObject *return_value = NULL;
473
0
    PyObject *value;
474
0
    Py_ssize_t start = 0;
475
0
    Py_ssize_t stop = PY_SSIZE_T_MAX;
476
477
0
    if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
478
0
        goto exit;
479
0
    }
480
0
    value = args[0];
481
0
    if (nargs < 2) {
482
0
        goto skip_optional;
483
0
    }
484
0
    if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
485
0
        goto exit;
486
0
    }
487
0
    if (nargs < 3) {
488
0
        goto skip_optional;
489
0
    }
490
0
    if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
491
0
        goto exit;
492
0
    }
493
0
skip_optional:
494
0
    return_value = memoryview_index_impl((PyMemoryViewObject *)self, value, start, stop);
495
496
0
exit:
497
0
    return return_value;
498
0
}
499
/*[clinic end generated code: output=154f4c04263ccb24 input=a9049054013a1b77]*/