LCOV - code coverage report
Current view: top level - Objects/clinic - memoryobject.c.h (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 26 81 32.1 %
Date: 2023-03-20 08:15:36 Functions: 3 7 42.9 %
Branches: 14 72 19.4 %

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

Generated by: LCOV version 1.14