memoryobject.c.h 12 KB

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