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