LCOV - code coverage report
Current view: top level - Objects/clinic - memoryobject.c.h (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 72 81 88.9 %
Date: 2022-07-07 18:19:46 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*[clinic input]
       2             : preserve
       3             : [clinic start generated code]*/
       4             : 
       5             : PyDoc_STRVAR(memoryview__doc__,
       6             : "memoryview(object)\n"
       7             : "--\n"
       8             : "\n"
       9             : "Create a new memoryview object which references the given object.");
      10             : 
      11             : static PyObject *
      12             : memoryview_impl(PyTypeObject *type, PyObject *object);
      13             : 
      14             : static PyObject *
      15      644218 : memoryview(PyTypeObject *type, PyObject *args, PyObject *kwargs)
      16             : {
      17      644218 :     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      644218 :     Py_ssize_t nargs = PyTuple_GET_SIZE(args);
      23             :     PyObject *object;
      24             : 
      25      644218 :     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
      26      644218 :     if (!fastargs) {
      27           9 :         goto exit;
      28             :     }
      29      644209 :     object = fastargs[0];
      30      644209 :     return_value = memoryview_impl(type, object);
      31             : 
      32      644218 : exit:
      33      644218 :     return return_value;
      34             : }
      35             : 
      36             : PyDoc_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             : 
      45             : static PyObject *
      46             : memoryview_release_impl(PyMemoryViewObject *self);
      47             : 
      48             : static PyObject *
      49         359 : memoryview_release(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
      50             : {
      51         359 :     return memoryview_release_impl(self);
      52             : }
      53             : 
      54             : PyDoc_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_CAST(memoryview_cast), METH_FASTCALL|METH_KEYWORDS, memoryview_cast__doc__},
      62             : 
      63             : static PyObject *
      64             : memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
      65             :                      PyObject *shape);
      66             : 
      67             : static PyObject *
      68       40006 : memoryview_cast(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
      69             : {
      70       40006 :     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       40006 :     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
      75             :     PyObject *format;
      76       40006 :     PyObject *shape = NULL;
      77             : 
      78       40006 :     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
      79       40006 :     if (!args) {
      80           2 :         goto exit;
      81             :     }
      82       40004 :     if (!PyUnicode_Check(args[0])) {
      83           1 :         _PyArg_BadArgument("cast", "argument 'format'", "str", args[0]);
      84           1 :         goto exit;
      85             :     }
      86       40003 :     if (PyUnicode_READY(args[0]) == -1) {
      87           0 :         goto exit;
      88             :     }
      89       40003 :     format = args[0];
      90       40003 :     if (!noptargs) {
      91       19743 :         goto skip_optional_pos;
      92             :     }
      93       20260 :     shape = args[1];
      94       40003 : skip_optional_pos:
      95       40003 :     return_value = memoryview_cast_impl(self, format, shape);
      96             : 
      97       40006 : exit:
      98       40006 :     return return_value;
      99             : }
     100             : 
     101             : PyDoc_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             : 
     110             : static PyObject *
     111             : memoryview_toreadonly_impl(PyMemoryViewObject *self);
     112             : 
     113             : static PyObject *
     114       27597 : memoryview_toreadonly(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
     115             : {
     116       27597 :     return memoryview_toreadonly_impl(self);
     117             : }
     118             : 
     119             : PyDoc_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             : 
     128             : static PyObject *
     129             : memoryview_tolist_impl(PyMemoryViewObject *self);
     130             : 
     131             : static PyObject *
     132       66968 : memoryview_tolist(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
     133             : {
     134       66968 :     return memoryview_tolist_impl(self);
     135             : }
     136             : 
     137             : PyDoc_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_CAST(memoryview_tobytes), METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
     151             : 
     152             : static PyObject *
     153             : memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
     154             : 
     155             : static PyObject *
     156      219503 : memoryview_tobytes(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     157             : {
     158      219503 :     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      219503 :     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
     163      219503 :     const char *order = NULL;
     164             : 
     165      219503 :     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
     166      219503 :     if (!args) {
     167           0 :         goto exit;
     168             :     }
     169      219503 :     if (!noptargs) {
     170       94931 :         goto skip_optional_pos;
     171             :     }
     172      124572 :     if (args[0] == Py_None) {
     173       31143 :         order = NULL;
     174             :     }
     175       93429 :     else if (PyUnicode_Check(args[0])) {
     176             :         Py_ssize_t order_length;
     177       93429 :         order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
     178       93429 :         if (order == NULL) {
     179           0 :             goto exit;
     180             :         }
     181       93429 :         if (strlen(order) != (size_t)order_length) {
     182           0 :             PyErr_SetString(PyExc_ValueError, "embedded null character");
     183           0 :             goto exit;
     184             :         }
     185             :     }
     186             :     else {
     187           0 :         _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
     188           0 :         goto exit;
     189             :     }
     190      219503 : skip_optional_pos:
     191      219503 :     return_value = memoryview_tobytes_impl(self, order);
     192             : 
     193      219503 : exit:
     194      219503 :     return return_value;
     195             : }
     196             : 
     197             : PyDoc_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_CAST(memoryview_hex), METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
     222             : 
     223             : static PyObject *
     224             : memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
     225             :                     int bytes_per_sep);
     226             : 
     227             : static PyObject *
     228       11254 : memoryview_hex(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     229             : {
     230       11254 :     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       11254 :     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
     235       11254 :     PyObject *sep = NULL;
     236       11254 :     int bytes_per_sep = 1;
     237             : 
     238       11254 :     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
     239       11254 :     if (!args) {
     240           0 :         goto exit;
     241             :     }
     242       11254 :     if (!noptargs) {
     243       11251 :         goto skip_optional_pos;
     244             :     }
     245           3 :     if (args[0]) {
     246           3 :         sep = args[0];
     247           3 :         if (!--noptargs) {
     248           1 :             goto skip_optional_pos;
     249             :         }
     250             :     }
     251           2 :     bytes_per_sep = _PyLong_AsInt(args[1]);
     252           2 :     if (bytes_per_sep == -1 && PyErr_Occurred()) {
     253           0 :         goto exit;
     254             :     }
     255           2 : skip_optional_pos:
     256       11254 :     return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
     257             : 
     258       11254 : exit:
     259       11254 :     return return_value;
     260             : }
     261             : /*[clinic end generated code: output=48be570b5e6038e3 input=a9049054013a1b77]*/

Generated by: LCOV version 1.14