Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Objects/clinic/memoryobject.c.h
12 views
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
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
PyObject *ob_item[NUM_KEYWORDS];
31
} _kwtuple = {
32
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
33
.ob_item = { &_Py_ID(object), },
34
};
35
#undef NUM_KEYWORDS
36
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
37
38
#else // !Py_BUILD_CORE
39
# define KWTUPLE NULL
40
#endif // !Py_BUILD_CORE
41
42
static const char * const _keywords[] = {"object", NULL};
43
static _PyArg_Parser _parser = {
44
.keywords = _keywords,
45
.fname = "memoryview",
46
.kwtuple = KWTUPLE,
47
};
48
#undef KWTUPLE
49
PyObject *argsbuf[1];
50
PyObject * const *fastargs;
51
Py_ssize_t nargs = PyTuple_GET_SIZE(args);
52
PyObject *object;
53
54
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
55
if (!fastargs) {
56
goto exit;
57
}
58
object = fastargs[0];
59
return_value = memoryview_impl(type, object);
60
61
exit:
62
return return_value;
63
}
64
65
PyDoc_STRVAR(memoryview__from_flags__doc__,
66
"_from_flags($type, /, object, flags)\n"
67
"--\n"
68
"\n"
69
"Create a new memoryview object which references the given object.");
70
71
#define MEMORYVIEW__FROM_FLAGS_METHODDEF \
72
{"_from_flags", _PyCFunction_CAST(memoryview__from_flags), METH_FASTCALL|METH_KEYWORDS|METH_CLASS, memoryview__from_flags__doc__},
73
74
static PyObject *
75
memoryview__from_flags_impl(PyTypeObject *type, PyObject *object, int flags);
76
77
static PyObject *
78
memoryview__from_flags(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
79
{
80
PyObject *return_value = NULL;
81
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
82
83
#define NUM_KEYWORDS 2
84
static struct {
85
PyGC_Head _this_is_not_used;
86
PyObject_VAR_HEAD
87
PyObject *ob_item[NUM_KEYWORDS];
88
} _kwtuple = {
89
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
90
.ob_item = { &_Py_ID(object), &_Py_ID(flags), },
91
};
92
#undef NUM_KEYWORDS
93
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
94
95
#else // !Py_BUILD_CORE
96
# define KWTUPLE NULL
97
#endif // !Py_BUILD_CORE
98
99
static const char * const _keywords[] = {"object", "flags", NULL};
100
static _PyArg_Parser _parser = {
101
.keywords = _keywords,
102
.fname = "_from_flags",
103
.kwtuple = KWTUPLE,
104
};
105
#undef KWTUPLE
106
PyObject *argsbuf[2];
107
PyObject *object;
108
int flags;
109
110
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
111
if (!args) {
112
goto exit;
113
}
114
object = args[0];
115
flags = _PyLong_AsInt(args[1]);
116
if (flags == -1 && PyErr_Occurred()) {
117
goto exit;
118
}
119
return_value = memoryview__from_flags_impl(type, object, flags);
120
121
exit:
122
return return_value;
123
}
124
125
PyDoc_STRVAR(memoryview_release__doc__,
126
"release($self, /)\n"
127
"--\n"
128
"\n"
129
"Release the underlying buffer exposed by the memoryview object.");
130
131
#define MEMORYVIEW_RELEASE_METHODDEF \
132
{"release", (PyCFunction)memoryview_release, METH_NOARGS, memoryview_release__doc__},
133
134
static PyObject *
135
memoryview_release_impl(PyMemoryViewObject *self);
136
137
static PyObject *
138
memoryview_release(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
139
{
140
return memoryview_release_impl(self);
141
}
142
143
PyDoc_STRVAR(memoryview_cast__doc__,
144
"cast($self, /, format, shape=<unrepresentable>)\n"
145
"--\n"
146
"\n"
147
"Cast a memoryview to a new format or shape.");
148
149
#define MEMORYVIEW_CAST_METHODDEF \
150
{"cast", _PyCFunction_CAST(memoryview_cast), METH_FASTCALL|METH_KEYWORDS, memoryview_cast__doc__},
151
152
static PyObject *
153
memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
154
PyObject *shape);
155
156
static PyObject *
157
memoryview_cast(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
158
{
159
PyObject *return_value = NULL;
160
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
161
162
#define NUM_KEYWORDS 2
163
static struct {
164
PyGC_Head _this_is_not_used;
165
PyObject_VAR_HEAD
166
PyObject *ob_item[NUM_KEYWORDS];
167
} _kwtuple = {
168
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
169
.ob_item = { &_Py_ID(format), &_Py_ID(shape), },
170
};
171
#undef NUM_KEYWORDS
172
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
173
174
#else // !Py_BUILD_CORE
175
# define KWTUPLE NULL
176
#endif // !Py_BUILD_CORE
177
178
static const char * const _keywords[] = {"format", "shape", NULL};
179
static _PyArg_Parser _parser = {
180
.keywords = _keywords,
181
.fname = "cast",
182
.kwtuple = KWTUPLE,
183
};
184
#undef KWTUPLE
185
PyObject *argsbuf[2];
186
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
187
PyObject *format;
188
PyObject *shape = NULL;
189
190
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
191
if (!args) {
192
goto exit;
193
}
194
if (!PyUnicode_Check(args[0])) {
195
_PyArg_BadArgument("cast", "argument 'format'", "str", args[0]);
196
goto exit;
197
}
198
format = args[0];
199
if (!noptargs) {
200
goto skip_optional_pos;
201
}
202
shape = args[1];
203
skip_optional_pos:
204
return_value = memoryview_cast_impl(self, format, shape);
205
206
exit:
207
return return_value;
208
}
209
210
PyDoc_STRVAR(memoryview_toreadonly__doc__,
211
"toreadonly($self, /)\n"
212
"--\n"
213
"\n"
214
"Return a readonly version of the memoryview.");
215
216
#define MEMORYVIEW_TOREADONLY_METHODDEF \
217
{"toreadonly", (PyCFunction)memoryview_toreadonly, METH_NOARGS, memoryview_toreadonly__doc__},
218
219
static PyObject *
220
memoryview_toreadonly_impl(PyMemoryViewObject *self);
221
222
static PyObject *
223
memoryview_toreadonly(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
224
{
225
return memoryview_toreadonly_impl(self);
226
}
227
228
PyDoc_STRVAR(memoryview_tolist__doc__,
229
"tolist($self, /)\n"
230
"--\n"
231
"\n"
232
"Return the data in the buffer as a list of elements.");
233
234
#define MEMORYVIEW_TOLIST_METHODDEF \
235
{"tolist", (PyCFunction)memoryview_tolist, METH_NOARGS, memoryview_tolist__doc__},
236
237
static PyObject *
238
memoryview_tolist_impl(PyMemoryViewObject *self);
239
240
static PyObject *
241
memoryview_tolist(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
242
{
243
return memoryview_tolist_impl(self);
244
}
245
246
PyDoc_STRVAR(memoryview_tobytes__doc__,
247
"tobytes($self, /, order=\'C\')\n"
248
"--\n"
249
"\n"
250
"Return the data in the buffer as a byte string.\n"
251
"\n"
252
"Order can be {\'C\', \'F\', \'A\'}. When order is \'C\' or \'F\', the data of the\n"
253
"original array is converted to C or Fortran order. For contiguous views,\n"
254
"\'A\' returns an exact copy of the physical memory. In particular, in-memory\n"
255
"Fortran order is preserved. For non-contiguous views, the data is converted\n"
256
"to C first. order=None is the same as order=\'C\'.");
257
258
#define MEMORYVIEW_TOBYTES_METHODDEF \
259
{"tobytes", _PyCFunction_CAST(memoryview_tobytes), METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
260
261
static PyObject *
262
memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
263
264
static PyObject *
265
memoryview_tobytes(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
266
{
267
PyObject *return_value = NULL;
268
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
269
270
#define NUM_KEYWORDS 1
271
static struct {
272
PyGC_Head _this_is_not_used;
273
PyObject_VAR_HEAD
274
PyObject *ob_item[NUM_KEYWORDS];
275
} _kwtuple = {
276
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
277
.ob_item = { &_Py_ID(order), },
278
};
279
#undef NUM_KEYWORDS
280
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
281
282
#else // !Py_BUILD_CORE
283
# define KWTUPLE NULL
284
#endif // !Py_BUILD_CORE
285
286
static const char * const _keywords[] = {"order", NULL};
287
static _PyArg_Parser _parser = {
288
.keywords = _keywords,
289
.fname = "tobytes",
290
.kwtuple = KWTUPLE,
291
};
292
#undef KWTUPLE
293
PyObject *argsbuf[1];
294
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
295
const char *order = NULL;
296
297
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
298
if (!args) {
299
goto exit;
300
}
301
if (!noptargs) {
302
goto skip_optional_pos;
303
}
304
if (args[0] == Py_None) {
305
order = NULL;
306
}
307
else if (PyUnicode_Check(args[0])) {
308
Py_ssize_t order_length;
309
order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
310
if (order == NULL) {
311
goto exit;
312
}
313
if (strlen(order) != (size_t)order_length) {
314
PyErr_SetString(PyExc_ValueError, "embedded null character");
315
goto exit;
316
}
317
}
318
else {
319
_PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
320
goto exit;
321
}
322
skip_optional_pos:
323
return_value = memoryview_tobytes_impl(self, order);
324
325
exit:
326
return return_value;
327
}
328
329
PyDoc_STRVAR(memoryview_hex__doc__,
330
"hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
331
"--\n"
332
"\n"
333
"Return the data in the buffer as a str of hexadecimal numbers.\n"
334
"\n"
335
" sep\n"
336
" An optional single character or byte to separate hex bytes.\n"
337
" bytes_per_sep\n"
338
" How many bytes between separators. Positive values count from the\n"
339
" right, negative values count from the left.\n"
340
"\n"
341
"Example:\n"
342
">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
343
">>> value.hex()\n"
344
"\'b901ef\'\n"
345
">>> value.hex(\':\')\n"
346
"\'b9:01:ef\'\n"
347
">>> value.hex(\':\', 2)\n"
348
"\'b9:01ef\'\n"
349
">>> value.hex(\':\', -2)\n"
350
"\'b901:ef\'");
351
352
#define MEMORYVIEW_HEX_METHODDEF \
353
{"hex", _PyCFunction_CAST(memoryview_hex), METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
354
355
static PyObject *
356
memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
357
int bytes_per_sep);
358
359
static PyObject *
360
memoryview_hex(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
361
{
362
PyObject *return_value = NULL;
363
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
364
365
#define NUM_KEYWORDS 2
366
static struct {
367
PyGC_Head _this_is_not_used;
368
PyObject_VAR_HEAD
369
PyObject *ob_item[NUM_KEYWORDS];
370
} _kwtuple = {
371
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
372
.ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
373
};
374
#undef NUM_KEYWORDS
375
#define KWTUPLE (&_kwtuple.ob_base.ob_base)
376
377
#else // !Py_BUILD_CORE
378
# define KWTUPLE NULL
379
#endif // !Py_BUILD_CORE
380
381
static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
382
static _PyArg_Parser _parser = {
383
.keywords = _keywords,
384
.fname = "hex",
385
.kwtuple = KWTUPLE,
386
};
387
#undef KWTUPLE
388
PyObject *argsbuf[2];
389
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
390
PyObject *sep = NULL;
391
int bytes_per_sep = 1;
392
393
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
394
if (!args) {
395
goto exit;
396
}
397
if (!noptargs) {
398
goto skip_optional_pos;
399
}
400
if (args[0]) {
401
sep = args[0];
402
if (!--noptargs) {
403
goto skip_optional_pos;
404
}
405
}
406
bytes_per_sep = _PyLong_AsInt(args[1]);
407
if (bytes_per_sep == -1 && PyErr_Occurred()) {
408
goto exit;
409
}
410
skip_optional_pos:
411
return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
412
413
exit:
414
return return_value;
415
}
416
/*[clinic end generated code: output=a4f6992947bcaf25 input=a9049054013a1b77]*/
417
418