bytesio.c.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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(_io_BytesIO_readable__doc__,
  9. "readable($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Returns True if the IO object can be read.");
  13. #define _IO_BYTESIO_READABLE_METHODDEF \
  14. {"readable", (PyCFunction)_io_BytesIO_readable, METH_NOARGS, _io_BytesIO_readable__doc__},
  15. static PyObject *
  16. _io_BytesIO_readable_impl(bytesio *self);
  17. static PyObject *
  18. _io_BytesIO_readable(bytesio *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return _io_BytesIO_readable_impl(self);
  21. }
  22. PyDoc_STRVAR(_io_BytesIO_writable__doc__,
  23. "writable($self, /)\n"
  24. "--\n"
  25. "\n"
  26. "Returns True if the IO object can be written.");
  27. #define _IO_BYTESIO_WRITABLE_METHODDEF \
  28. {"writable", (PyCFunction)_io_BytesIO_writable, METH_NOARGS, _io_BytesIO_writable__doc__},
  29. static PyObject *
  30. _io_BytesIO_writable_impl(bytesio *self);
  31. static PyObject *
  32. _io_BytesIO_writable(bytesio *self, PyObject *Py_UNUSED(ignored))
  33. {
  34. return _io_BytesIO_writable_impl(self);
  35. }
  36. PyDoc_STRVAR(_io_BytesIO_seekable__doc__,
  37. "seekable($self, /)\n"
  38. "--\n"
  39. "\n"
  40. "Returns True if the IO object can be seeked.");
  41. #define _IO_BYTESIO_SEEKABLE_METHODDEF \
  42. {"seekable", (PyCFunction)_io_BytesIO_seekable, METH_NOARGS, _io_BytesIO_seekable__doc__},
  43. static PyObject *
  44. _io_BytesIO_seekable_impl(bytesio *self);
  45. static PyObject *
  46. _io_BytesIO_seekable(bytesio *self, PyObject *Py_UNUSED(ignored))
  47. {
  48. return _io_BytesIO_seekable_impl(self);
  49. }
  50. PyDoc_STRVAR(_io_BytesIO_flush__doc__,
  51. "flush($self, /)\n"
  52. "--\n"
  53. "\n"
  54. "Does nothing.");
  55. #define _IO_BYTESIO_FLUSH_METHODDEF \
  56. {"flush", (PyCFunction)_io_BytesIO_flush, METH_NOARGS, _io_BytesIO_flush__doc__},
  57. static PyObject *
  58. _io_BytesIO_flush_impl(bytesio *self);
  59. static PyObject *
  60. _io_BytesIO_flush(bytesio *self, PyObject *Py_UNUSED(ignored))
  61. {
  62. return _io_BytesIO_flush_impl(self);
  63. }
  64. PyDoc_STRVAR(_io_BytesIO_getbuffer__doc__,
  65. "getbuffer($self, /)\n"
  66. "--\n"
  67. "\n"
  68. "Get a read-write view over the contents of the BytesIO object.");
  69. #define _IO_BYTESIO_GETBUFFER_METHODDEF \
  70. {"getbuffer", _PyCFunction_CAST(_io_BytesIO_getbuffer), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _io_BytesIO_getbuffer__doc__},
  71. static PyObject *
  72. _io_BytesIO_getbuffer_impl(bytesio *self, PyTypeObject *cls);
  73. static PyObject *
  74. _io_BytesIO_getbuffer(bytesio *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  75. {
  76. if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
  77. PyErr_SetString(PyExc_TypeError, "getbuffer() takes no arguments");
  78. return NULL;
  79. }
  80. return _io_BytesIO_getbuffer_impl(self, cls);
  81. }
  82. PyDoc_STRVAR(_io_BytesIO_getvalue__doc__,
  83. "getvalue($self, /)\n"
  84. "--\n"
  85. "\n"
  86. "Retrieve the entire contents of the BytesIO object.");
  87. #define _IO_BYTESIO_GETVALUE_METHODDEF \
  88. {"getvalue", (PyCFunction)_io_BytesIO_getvalue, METH_NOARGS, _io_BytesIO_getvalue__doc__},
  89. static PyObject *
  90. _io_BytesIO_getvalue_impl(bytesio *self);
  91. static PyObject *
  92. _io_BytesIO_getvalue(bytesio *self, PyObject *Py_UNUSED(ignored))
  93. {
  94. return _io_BytesIO_getvalue_impl(self);
  95. }
  96. PyDoc_STRVAR(_io_BytesIO_isatty__doc__,
  97. "isatty($self, /)\n"
  98. "--\n"
  99. "\n"
  100. "Always returns False.\n"
  101. "\n"
  102. "BytesIO objects are not connected to a TTY-like device.");
  103. #define _IO_BYTESIO_ISATTY_METHODDEF \
  104. {"isatty", (PyCFunction)_io_BytesIO_isatty, METH_NOARGS, _io_BytesIO_isatty__doc__},
  105. static PyObject *
  106. _io_BytesIO_isatty_impl(bytesio *self);
  107. static PyObject *
  108. _io_BytesIO_isatty(bytesio *self, PyObject *Py_UNUSED(ignored))
  109. {
  110. return _io_BytesIO_isatty_impl(self);
  111. }
  112. PyDoc_STRVAR(_io_BytesIO_tell__doc__,
  113. "tell($self, /)\n"
  114. "--\n"
  115. "\n"
  116. "Current file position, an integer.");
  117. #define _IO_BYTESIO_TELL_METHODDEF \
  118. {"tell", (PyCFunction)_io_BytesIO_tell, METH_NOARGS, _io_BytesIO_tell__doc__},
  119. static PyObject *
  120. _io_BytesIO_tell_impl(bytesio *self);
  121. static PyObject *
  122. _io_BytesIO_tell(bytesio *self, PyObject *Py_UNUSED(ignored))
  123. {
  124. return _io_BytesIO_tell_impl(self);
  125. }
  126. PyDoc_STRVAR(_io_BytesIO_read__doc__,
  127. "read($self, size=-1, /)\n"
  128. "--\n"
  129. "\n"
  130. "Read at most size bytes, returned as a bytes object.\n"
  131. "\n"
  132. "If the size argument is negative, read until EOF is reached.\n"
  133. "Return an empty bytes object at EOF.");
  134. #define _IO_BYTESIO_READ_METHODDEF \
  135. {"read", _PyCFunction_CAST(_io_BytesIO_read), METH_FASTCALL, _io_BytesIO_read__doc__},
  136. static PyObject *
  137. _io_BytesIO_read_impl(bytesio *self, Py_ssize_t size);
  138. static PyObject *
  139. _io_BytesIO_read(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  140. {
  141. PyObject *return_value = NULL;
  142. Py_ssize_t size = -1;
  143. if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
  144. goto exit;
  145. }
  146. if (nargs < 1) {
  147. goto skip_optional;
  148. }
  149. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  150. goto exit;
  151. }
  152. skip_optional:
  153. return_value = _io_BytesIO_read_impl(self, size);
  154. exit:
  155. return return_value;
  156. }
  157. PyDoc_STRVAR(_io_BytesIO_read1__doc__,
  158. "read1($self, size=-1, /)\n"
  159. "--\n"
  160. "\n"
  161. "Read at most size bytes, returned as a bytes object.\n"
  162. "\n"
  163. "If the size argument is negative or omitted, read until EOF is reached.\n"
  164. "Return an empty bytes object at EOF.");
  165. #define _IO_BYTESIO_READ1_METHODDEF \
  166. {"read1", _PyCFunction_CAST(_io_BytesIO_read1), METH_FASTCALL, _io_BytesIO_read1__doc__},
  167. static PyObject *
  168. _io_BytesIO_read1_impl(bytesio *self, Py_ssize_t size);
  169. static PyObject *
  170. _io_BytesIO_read1(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  171. {
  172. PyObject *return_value = NULL;
  173. Py_ssize_t size = -1;
  174. if (!_PyArg_CheckPositional("read1", nargs, 0, 1)) {
  175. goto exit;
  176. }
  177. if (nargs < 1) {
  178. goto skip_optional;
  179. }
  180. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  181. goto exit;
  182. }
  183. skip_optional:
  184. return_value = _io_BytesIO_read1_impl(self, size);
  185. exit:
  186. return return_value;
  187. }
  188. PyDoc_STRVAR(_io_BytesIO_readline__doc__,
  189. "readline($self, size=-1, /)\n"
  190. "--\n"
  191. "\n"
  192. "Next line from the file, as a bytes object.\n"
  193. "\n"
  194. "Retain newline. A non-negative size argument limits the maximum\n"
  195. "number of bytes to return (an incomplete line may be returned then).\n"
  196. "Return an empty bytes object at EOF.");
  197. #define _IO_BYTESIO_READLINE_METHODDEF \
  198. {"readline", _PyCFunction_CAST(_io_BytesIO_readline), METH_FASTCALL, _io_BytesIO_readline__doc__},
  199. static PyObject *
  200. _io_BytesIO_readline_impl(bytesio *self, Py_ssize_t size);
  201. static PyObject *
  202. _io_BytesIO_readline(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  203. {
  204. PyObject *return_value = NULL;
  205. Py_ssize_t size = -1;
  206. if (!_PyArg_CheckPositional("readline", nargs, 0, 1)) {
  207. goto exit;
  208. }
  209. if (nargs < 1) {
  210. goto skip_optional;
  211. }
  212. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  213. goto exit;
  214. }
  215. skip_optional:
  216. return_value = _io_BytesIO_readline_impl(self, size);
  217. exit:
  218. return return_value;
  219. }
  220. PyDoc_STRVAR(_io_BytesIO_readlines__doc__,
  221. "readlines($self, size=None, /)\n"
  222. "--\n"
  223. "\n"
  224. "List of bytes objects, each a line from the file.\n"
  225. "\n"
  226. "Call readline() repeatedly and return a list of the lines so read.\n"
  227. "The optional size argument, if given, is an approximate bound on the\n"
  228. "total number of bytes in the lines returned.");
  229. #define _IO_BYTESIO_READLINES_METHODDEF \
  230. {"readlines", _PyCFunction_CAST(_io_BytesIO_readlines), METH_FASTCALL, _io_BytesIO_readlines__doc__},
  231. static PyObject *
  232. _io_BytesIO_readlines_impl(bytesio *self, PyObject *arg);
  233. static PyObject *
  234. _io_BytesIO_readlines(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  235. {
  236. PyObject *return_value = NULL;
  237. PyObject *arg = Py_None;
  238. if (!_PyArg_CheckPositional("readlines", nargs, 0, 1)) {
  239. goto exit;
  240. }
  241. if (nargs < 1) {
  242. goto skip_optional;
  243. }
  244. arg = args[0];
  245. skip_optional:
  246. return_value = _io_BytesIO_readlines_impl(self, arg);
  247. exit:
  248. return return_value;
  249. }
  250. PyDoc_STRVAR(_io_BytesIO_readinto__doc__,
  251. "readinto($self, buffer, /)\n"
  252. "--\n"
  253. "\n"
  254. "Read bytes into buffer.\n"
  255. "\n"
  256. "Returns number of bytes read (0 for EOF), or None if the object\n"
  257. "is set not to block and has no data to read.");
  258. #define _IO_BYTESIO_READINTO_METHODDEF \
  259. {"readinto", (PyCFunction)_io_BytesIO_readinto, METH_O, _io_BytesIO_readinto__doc__},
  260. static PyObject *
  261. _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer);
  262. static PyObject *
  263. _io_BytesIO_readinto(bytesio *self, PyObject *arg)
  264. {
  265. PyObject *return_value = NULL;
  266. Py_buffer buffer = {NULL, NULL};
  267. if (PyObject_GetBuffer(arg, &buffer, PyBUF_WRITABLE) < 0) {
  268. PyErr_Clear();
  269. _PyArg_BadArgument("readinto", "argument", "read-write bytes-like object", arg);
  270. goto exit;
  271. }
  272. if (!PyBuffer_IsContiguous(&buffer, 'C')) {
  273. _PyArg_BadArgument("readinto", "argument", "contiguous buffer", arg);
  274. goto exit;
  275. }
  276. return_value = _io_BytesIO_readinto_impl(self, &buffer);
  277. exit:
  278. /* Cleanup for buffer */
  279. if (buffer.obj) {
  280. PyBuffer_Release(&buffer);
  281. }
  282. return return_value;
  283. }
  284. PyDoc_STRVAR(_io_BytesIO_truncate__doc__,
  285. "truncate($self, size=None, /)\n"
  286. "--\n"
  287. "\n"
  288. "Truncate the file to at most size bytes.\n"
  289. "\n"
  290. "Size defaults to the current file position, as returned by tell().\n"
  291. "The current file position is unchanged. Returns the new size.");
  292. #define _IO_BYTESIO_TRUNCATE_METHODDEF \
  293. {"truncate", _PyCFunction_CAST(_io_BytesIO_truncate), METH_FASTCALL, _io_BytesIO_truncate__doc__},
  294. static PyObject *
  295. _io_BytesIO_truncate_impl(bytesio *self, Py_ssize_t size);
  296. static PyObject *
  297. _io_BytesIO_truncate(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  298. {
  299. PyObject *return_value = NULL;
  300. Py_ssize_t size = self->pos;
  301. if (!_PyArg_CheckPositional("truncate", nargs, 0, 1)) {
  302. goto exit;
  303. }
  304. if (nargs < 1) {
  305. goto skip_optional;
  306. }
  307. if (!_Py_convert_optional_to_ssize_t(args[0], &size)) {
  308. goto exit;
  309. }
  310. skip_optional:
  311. return_value = _io_BytesIO_truncate_impl(self, size);
  312. exit:
  313. return return_value;
  314. }
  315. PyDoc_STRVAR(_io_BytesIO_seek__doc__,
  316. "seek($self, pos, whence=0, /)\n"
  317. "--\n"
  318. "\n"
  319. "Change stream position.\n"
  320. "\n"
  321. "Seek to byte offset pos relative to position indicated by whence:\n"
  322. " 0 Start of stream (the default). pos should be >= 0;\n"
  323. " 1 Current position - pos may be negative;\n"
  324. " 2 End of stream - pos usually negative.\n"
  325. "Returns the new absolute position.");
  326. #define _IO_BYTESIO_SEEK_METHODDEF \
  327. {"seek", _PyCFunction_CAST(_io_BytesIO_seek), METH_FASTCALL, _io_BytesIO_seek__doc__},
  328. static PyObject *
  329. _io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence);
  330. static PyObject *
  331. _io_BytesIO_seek(bytesio *self, PyObject *const *args, Py_ssize_t nargs)
  332. {
  333. PyObject *return_value = NULL;
  334. Py_ssize_t pos;
  335. int whence = 0;
  336. if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
  337. goto exit;
  338. }
  339. {
  340. Py_ssize_t ival = -1;
  341. PyObject *iobj = _PyNumber_Index(args[0]);
  342. if (iobj != NULL) {
  343. ival = PyLong_AsSsize_t(iobj);
  344. Py_DECREF(iobj);
  345. }
  346. if (ival == -1 && PyErr_Occurred()) {
  347. goto exit;
  348. }
  349. pos = ival;
  350. }
  351. if (nargs < 2) {
  352. goto skip_optional;
  353. }
  354. whence = _PyLong_AsInt(args[1]);
  355. if (whence == -1 && PyErr_Occurred()) {
  356. goto exit;
  357. }
  358. skip_optional:
  359. return_value = _io_BytesIO_seek_impl(self, pos, whence);
  360. exit:
  361. return return_value;
  362. }
  363. PyDoc_STRVAR(_io_BytesIO_write__doc__,
  364. "write($self, b, /)\n"
  365. "--\n"
  366. "\n"
  367. "Write bytes to file.\n"
  368. "\n"
  369. "Return the number of bytes written.");
  370. #define _IO_BYTESIO_WRITE_METHODDEF \
  371. {"write", (PyCFunction)_io_BytesIO_write, METH_O, _io_BytesIO_write__doc__},
  372. PyDoc_STRVAR(_io_BytesIO_writelines__doc__,
  373. "writelines($self, lines, /)\n"
  374. "--\n"
  375. "\n"
  376. "Write lines to the file.\n"
  377. "\n"
  378. "Note that newlines are not added. lines can be any iterable object\n"
  379. "producing bytes-like objects. This is equivalent to calling write() for\n"
  380. "each element.");
  381. #define _IO_BYTESIO_WRITELINES_METHODDEF \
  382. {"writelines", (PyCFunction)_io_BytesIO_writelines, METH_O, _io_BytesIO_writelines__doc__},
  383. PyDoc_STRVAR(_io_BytesIO_close__doc__,
  384. "close($self, /)\n"
  385. "--\n"
  386. "\n"
  387. "Disable all I/O operations.");
  388. #define _IO_BYTESIO_CLOSE_METHODDEF \
  389. {"close", (PyCFunction)_io_BytesIO_close, METH_NOARGS, _io_BytesIO_close__doc__},
  390. static PyObject *
  391. _io_BytesIO_close_impl(bytesio *self);
  392. static PyObject *
  393. _io_BytesIO_close(bytesio *self, PyObject *Py_UNUSED(ignored))
  394. {
  395. return _io_BytesIO_close_impl(self);
  396. }
  397. PyDoc_STRVAR(_io_BytesIO___init____doc__,
  398. "BytesIO(initial_bytes=b\'\')\n"
  399. "--\n"
  400. "\n"
  401. "Buffered I/O implementation using an in-memory bytes buffer.");
  402. static int
  403. _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue);
  404. static int
  405. _io_BytesIO___init__(PyObject *self, PyObject *args, PyObject *kwargs)
  406. {
  407. int return_value = -1;
  408. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  409. #define NUM_KEYWORDS 1
  410. static struct {
  411. PyGC_Head _this_is_not_used;
  412. PyObject_VAR_HEAD
  413. PyObject *ob_item[NUM_KEYWORDS];
  414. } _kwtuple = {
  415. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  416. .ob_item = { &_Py_ID(initial_bytes), },
  417. };
  418. #undef NUM_KEYWORDS
  419. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  420. #else // !Py_BUILD_CORE
  421. # define KWTUPLE NULL
  422. #endif // !Py_BUILD_CORE
  423. static const char * const _keywords[] = {"initial_bytes", NULL};
  424. static _PyArg_Parser _parser = {
  425. .keywords = _keywords,
  426. .fname = "BytesIO",
  427. .kwtuple = KWTUPLE,
  428. };
  429. #undef KWTUPLE
  430. PyObject *argsbuf[1];
  431. PyObject * const *fastargs;
  432. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  433. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  434. PyObject *initvalue = NULL;
  435. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf);
  436. if (!fastargs) {
  437. goto exit;
  438. }
  439. if (!noptargs) {
  440. goto skip_optional_pos;
  441. }
  442. initvalue = fastargs[0];
  443. skip_optional_pos:
  444. return_value = _io_BytesIO___init___impl((bytesio *)self, initvalue);
  445. exit:
  446. return return_value;
  447. }
  448. /*[clinic end generated code: output=1c540b54a10919d7 input=a9049054013a1b77]*/