(root)/
Python-3.12.0/
Objects/
clinic/
memoryobject.c.h
       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      if (PyUnicode_READY(args[0]) == -1) {
     199          goto exit;
     200      }
     201      format = args[0];
     202      if (!noptargs) {
     203          goto skip_optional_pos;
     204      }
     205      shape = args[1];
     206  skip_optional_pos:
     207      return_value = memoryview_cast_impl(self, format, shape);
     208  
     209  exit:
     210      return return_value;
     211  }
     212  
     213  PyDoc_STRVAR(memoryview_toreadonly__doc__,
     214  "toreadonly($self, /)\n"
     215  "--\n"
     216  "\n"
     217  "Return a readonly version of the memoryview.");
     218  
     219  #define MEMORYVIEW_TOREADONLY_METHODDEF    \
     220      {"toreadonly", (PyCFunction)memoryview_toreadonly, METH_NOARGS, memoryview_toreadonly__doc__},
     221  
     222  static PyObject *
     223  memoryview_toreadonly_impl(PyMemoryViewObject *self);
     224  
     225  static PyObject *
     226  memoryview_toreadonly(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
     227  {
     228      return memoryview_toreadonly_impl(self);
     229  }
     230  
     231  PyDoc_STRVAR(memoryview_tolist__doc__,
     232  "tolist($self, /)\n"
     233  "--\n"
     234  "\n"
     235  "Return the data in the buffer as a list of elements.");
     236  
     237  #define MEMORYVIEW_TOLIST_METHODDEF    \
     238      {"tolist", (PyCFunction)memoryview_tolist, METH_NOARGS, memoryview_tolist__doc__},
     239  
     240  static PyObject *
     241  memoryview_tolist_impl(PyMemoryViewObject *self);
     242  
     243  static PyObject *
     244  memoryview_tolist(PyMemoryViewObject *self, PyObject *Py_UNUSED(ignored))
     245  {
     246      return memoryview_tolist_impl(self);
     247  }
     248  
     249  PyDoc_STRVAR(memoryview_tobytes__doc__,
     250  "tobytes($self, /, order=\'C\')\n"
     251  "--\n"
     252  "\n"
     253  "Return the data in the buffer as a byte string.\n"
     254  "\n"
     255  "Order can be {\'C\', \'F\', \'A\'}. When order is \'C\' or \'F\', the data of the\n"
     256  "original array is converted to C or Fortran order. For contiguous views,\n"
     257  "\'A\' returns an exact copy of the physical memory. In particular, in-memory\n"
     258  "Fortran order is preserved. For non-contiguous views, the data is converted\n"
     259  "to C first. order=None is the same as order=\'C\'.");
     260  
     261  #define MEMORYVIEW_TOBYTES_METHODDEF    \
     262      {"tobytes", _PyCFunction_CAST(memoryview_tobytes), METH_FASTCALL|METH_KEYWORDS, memoryview_tobytes__doc__},
     263  
     264  static PyObject *
     265  memoryview_tobytes_impl(PyMemoryViewObject *self, const char *order);
     266  
     267  static PyObject *
     268  memoryview_tobytes(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     269  {
     270      PyObject *return_value = NULL;
     271      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     272  
     273      #define NUM_KEYWORDS 1
     274      static struct {
     275          PyGC_Head _this_is_not_used;
     276          PyObject_VAR_HEAD
     277          PyObject *ob_item[NUM_KEYWORDS];
     278      } _kwtuple = {
     279          .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
     280          .ob_item = { &_Py_ID(order), },
     281      };
     282      #undef NUM_KEYWORDS
     283      #define KWTUPLE (&_kwtuple.ob_base.ob_base)
     284  
     285      #else  // !Py_BUILD_CORE
     286      #  define KWTUPLE NULL
     287      #endif  // !Py_BUILD_CORE
     288  
     289      static const char * const _keywords[] = {"order", NULL};
     290      static _PyArg_Parser _parser = {
     291          .keywords = _keywords,
     292          .fname = "tobytes",
     293          .kwtuple = KWTUPLE,
     294      };
     295      #undef KWTUPLE
     296      PyObject *argsbuf[1];
     297      Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
     298      const char *order = NULL;
     299  
     300      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
     301      if (!args) {
     302          goto exit;
     303      }
     304      if (!noptargs) {
     305          goto skip_optional_pos;
     306      }
     307      if (args[0] == Py_None) {
     308          order = NULL;
     309      }
     310      else if (PyUnicode_Check(args[0])) {
     311          Py_ssize_t order_length;
     312          order = PyUnicode_AsUTF8AndSize(args[0], &order_length);
     313          if (order == NULL) {
     314              goto exit;
     315          }
     316          if (strlen(order) != (size_t)order_length) {
     317              PyErr_SetString(PyExc_ValueError, "embedded null character");
     318              goto exit;
     319          }
     320      }
     321      else {
     322          _PyArg_BadArgument("tobytes", "argument 'order'", "str or None", args[0]);
     323          goto exit;
     324      }
     325  skip_optional_pos:
     326      return_value = memoryview_tobytes_impl(self, order);
     327  
     328  exit:
     329      return return_value;
     330  }
     331  
     332  PyDoc_STRVAR(memoryview_hex__doc__,
     333  "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n"
     334  "--\n"
     335  "\n"
     336  "Return the data in the buffer as a str of hexadecimal numbers.\n"
     337  "\n"
     338  "  sep\n"
     339  "    An optional single character or byte to separate hex bytes.\n"
     340  "  bytes_per_sep\n"
     341  "    How many bytes between separators.  Positive values count from the\n"
     342  "    right, negative values count from the left.\n"
     343  "\n"
     344  "Example:\n"
     345  ">>> value = memoryview(b\'\\xb9\\x01\\xef\')\n"
     346  ">>> value.hex()\n"
     347  "\'b901ef\'\n"
     348  ">>> value.hex(\':\')\n"
     349  "\'b9:01:ef\'\n"
     350  ">>> value.hex(\':\', 2)\n"
     351  "\'b9:01ef\'\n"
     352  ">>> value.hex(\':\', -2)\n"
     353  "\'b901:ef\'");
     354  
     355  #define MEMORYVIEW_HEX_METHODDEF    \
     356      {"hex", _PyCFunction_CAST(memoryview_hex), METH_FASTCALL|METH_KEYWORDS, memoryview_hex__doc__},
     357  
     358  static PyObject *
     359  memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
     360                      int bytes_per_sep);
     361  
     362  static PyObject *
     363  memoryview_hex(PyMemoryViewObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     364  {
     365      PyObject *return_value = NULL;
     366      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     367  
     368      #define NUM_KEYWORDS 2
     369      static struct {
     370          PyGC_Head _this_is_not_used;
     371          PyObject_VAR_HEAD
     372          PyObject *ob_item[NUM_KEYWORDS];
     373      } _kwtuple = {
     374          .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
     375          .ob_item = { &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
     376      };
     377      #undef NUM_KEYWORDS
     378      #define KWTUPLE (&_kwtuple.ob_base.ob_base)
     379  
     380      #else  // !Py_BUILD_CORE
     381      #  define KWTUPLE NULL
     382      #endif  // !Py_BUILD_CORE
     383  
     384      static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL};
     385      static _PyArg_Parser _parser = {
     386          .keywords = _keywords,
     387          .fname = "hex",
     388          .kwtuple = KWTUPLE,
     389      };
     390      #undef KWTUPLE
     391      PyObject *argsbuf[2];
     392      Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
     393      PyObject *sep = NULL;
     394      int bytes_per_sep = 1;
     395  
     396      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
     397      if (!args) {
     398          goto exit;
     399      }
     400      if (!noptargs) {
     401          goto skip_optional_pos;
     402      }
     403      if (args[0]) {
     404          sep = args[0];
     405          if (!--noptargs) {
     406              goto skip_optional_pos;
     407          }
     408      }
     409      bytes_per_sep = _PyLong_AsInt(args[1]);
     410      if (bytes_per_sep == -1 && PyErr_Occurred()) {
     411          goto exit;
     412      }
     413  skip_optional_pos:
     414      return_value = memoryview_hex_impl(self, sep, bytes_per_sep);
     415  
     416  exit:
     417      return return_value;
     418  }
     419  /*[clinic end generated code: output=01613814112cedd7 input=a9049054013a1b77]*/