blob.c.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(blob_close__doc__,
  9. "close($self, /)\n"
  10. "--\n"
  11. "\n"
  12. "Close the blob.");
  13. #define BLOB_CLOSE_METHODDEF \
  14. {"close", (PyCFunction)blob_close, METH_NOARGS, blob_close__doc__},
  15. static PyObject *
  16. blob_close_impl(pysqlite_Blob *self);
  17. static PyObject *
  18. blob_close(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored))
  19. {
  20. return blob_close_impl(self);
  21. }
  22. PyDoc_STRVAR(blob_read__doc__,
  23. "read($self, length=-1, /)\n"
  24. "--\n"
  25. "\n"
  26. "Read data at the current offset position.\n"
  27. "\n"
  28. " length\n"
  29. " Read length in bytes.\n"
  30. "\n"
  31. "If the end of the blob is reached, the data up to end of file will be returned.\n"
  32. "When length is not specified, or is negative, Blob.read() will read until the\n"
  33. "end of the blob.");
  34. #define BLOB_READ_METHODDEF \
  35. {"read", _PyCFunction_CAST(blob_read), METH_FASTCALL, blob_read__doc__},
  36. static PyObject *
  37. blob_read_impl(pysqlite_Blob *self, int length);
  38. static PyObject *
  39. blob_read(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs)
  40. {
  41. PyObject *return_value = NULL;
  42. int length = -1;
  43. if (!_PyArg_CheckPositional("read", nargs, 0, 1)) {
  44. goto exit;
  45. }
  46. if (nargs < 1) {
  47. goto skip_optional;
  48. }
  49. length = _PyLong_AsInt(args[0]);
  50. if (length == -1 && PyErr_Occurred()) {
  51. goto exit;
  52. }
  53. skip_optional:
  54. return_value = blob_read_impl(self, length);
  55. exit:
  56. return return_value;
  57. }
  58. PyDoc_STRVAR(blob_write__doc__,
  59. "write($self, data, /)\n"
  60. "--\n"
  61. "\n"
  62. "Write data at the current offset.\n"
  63. "\n"
  64. "This function cannot change the blob length. Writing beyond the end of the\n"
  65. "blob will result in an exception being raised.");
  66. #define BLOB_WRITE_METHODDEF \
  67. {"write", (PyCFunction)blob_write, METH_O, blob_write__doc__},
  68. static PyObject *
  69. blob_write_impl(pysqlite_Blob *self, Py_buffer *data);
  70. static PyObject *
  71. blob_write(pysqlite_Blob *self, PyObject *arg)
  72. {
  73. PyObject *return_value = NULL;
  74. Py_buffer data = {NULL, NULL};
  75. if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
  76. goto exit;
  77. }
  78. if (!PyBuffer_IsContiguous(&data, 'C')) {
  79. _PyArg_BadArgument("write", "argument", "contiguous buffer", arg);
  80. goto exit;
  81. }
  82. return_value = blob_write_impl(self, &data);
  83. exit:
  84. /* Cleanup for data */
  85. if (data.obj) {
  86. PyBuffer_Release(&data);
  87. }
  88. return return_value;
  89. }
  90. PyDoc_STRVAR(blob_seek__doc__,
  91. "seek($self, offset, origin=0, /)\n"
  92. "--\n"
  93. "\n"
  94. "Set the current access position to offset.\n"
  95. "\n"
  96. "The origin argument defaults to os.SEEK_SET (absolute blob positioning).\n"
  97. "Other values for origin are os.SEEK_CUR (seek relative to the current position)\n"
  98. "and os.SEEK_END (seek relative to the blob\'s end).");
  99. #define BLOB_SEEK_METHODDEF \
  100. {"seek", _PyCFunction_CAST(blob_seek), METH_FASTCALL, blob_seek__doc__},
  101. static PyObject *
  102. blob_seek_impl(pysqlite_Blob *self, int offset, int origin);
  103. static PyObject *
  104. blob_seek(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs)
  105. {
  106. PyObject *return_value = NULL;
  107. int offset;
  108. int origin = 0;
  109. if (!_PyArg_CheckPositional("seek", nargs, 1, 2)) {
  110. goto exit;
  111. }
  112. offset = _PyLong_AsInt(args[0]);
  113. if (offset == -1 && PyErr_Occurred()) {
  114. goto exit;
  115. }
  116. if (nargs < 2) {
  117. goto skip_optional;
  118. }
  119. origin = _PyLong_AsInt(args[1]);
  120. if (origin == -1 && PyErr_Occurred()) {
  121. goto exit;
  122. }
  123. skip_optional:
  124. return_value = blob_seek_impl(self, offset, origin);
  125. exit:
  126. return return_value;
  127. }
  128. PyDoc_STRVAR(blob_tell__doc__,
  129. "tell($self, /)\n"
  130. "--\n"
  131. "\n"
  132. "Return the current access position for the blob.");
  133. #define BLOB_TELL_METHODDEF \
  134. {"tell", (PyCFunction)blob_tell, METH_NOARGS, blob_tell__doc__},
  135. static PyObject *
  136. blob_tell_impl(pysqlite_Blob *self);
  137. static PyObject *
  138. blob_tell(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored))
  139. {
  140. return blob_tell_impl(self);
  141. }
  142. PyDoc_STRVAR(blob_enter__doc__,
  143. "__enter__($self, /)\n"
  144. "--\n"
  145. "\n"
  146. "Blob context manager enter.");
  147. #define BLOB_ENTER_METHODDEF \
  148. {"__enter__", (PyCFunction)blob_enter, METH_NOARGS, blob_enter__doc__},
  149. static PyObject *
  150. blob_enter_impl(pysqlite_Blob *self);
  151. static PyObject *
  152. blob_enter(pysqlite_Blob *self, PyObject *Py_UNUSED(ignored))
  153. {
  154. return blob_enter_impl(self);
  155. }
  156. PyDoc_STRVAR(blob_exit__doc__,
  157. "__exit__($self, type, val, tb, /)\n"
  158. "--\n"
  159. "\n"
  160. "Blob context manager exit.");
  161. #define BLOB_EXIT_METHODDEF \
  162. {"__exit__", _PyCFunction_CAST(blob_exit), METH_FASTCALL, blob_exit__doc__},
  163. static PyObject *
  164. blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val,
  165. PyObject *tb);
  166. static PyObject *
  167. blob_exit(pysqlite_Blob *self, PyObject *const *args, Py_ssize_t nargs)
  168. {
  169. PyObject *return_value = NULL;
  170. PyObject *type;
  171. PyObject *val;
  172. PyObject *tb;
  173. if (!_PyArg_CheckPositional("__exit__", nargs, 3, 3)) {
  174. goto exit;
  175. }
  176. type = args[0];
  177. val = args[1];
  178. tb = args[2];
  179. return_value = blob_exit_impl(self, type, val, tb);
  180. exit:
  181. return return_value;
  182. }
  183. /*[clinic end generated code: output=ad6a402f70e85977 input=a9049054013a1b77]*/