| 1 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 2 |
# define Py_BUILD_CORE_MODULE 1 |
| 3 |
#endif |
| 4 |
|
| 5 |
#include "blob.h" |
| 6 |
#include "util.h" |
| 7 |
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
| 8 |
|
| 9 |
#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self))) |
| 10 |
#include "clinic/blob.c.h" |
| 11 |
#undef clinic_state |
| 12 |
|
| 13 |
#define _pysqlite_Blob_CAST(op) ((pysqlite_Blob *)(op)) |
| 14 |
|
| 15 |
/*[clinic input] |
| 16 |
module _sqlite3 |
| 17 |
class _sqlite3.Blob "pysqlite_Blob *" "clinic_state()->BlobType" |
| 18 |
[clinic start generated code]*/ |
| 19 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=908d3e16a45f8da7]*/ |
| 20 |
|
| 21 |
static void |
| 22 |
close_blob(pysqlite_Blob *self) |
| 23 |
{ |
| 24 |
if (self->blob) { |
| 25 |
sqlite3_blob *blob = self->blob; |
| 26 |
self->blob = NULL; |
| 27 |
|
| 28 |
Py_BEGIN_ALLOW_THREADS |
| 29 |
sqlite3_blob_close(blob); |
| 30 |
Py_END_ALLOW_THREADS |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
static int |
| 35 |
blob_traverse(PyObject *op, visitproc visit, void *arg) |
| 36 |
{ |
| 37 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 38 |
Py_VISIT(Py_TYPE(self)); |
| 39 |
Py_VISIT(self->connection); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static int |
| 44 |
blob_clear(PyObject *op) |
| 45 |
{ |
| 46 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 47 |
Py_CLEAR(self->connection); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
static void |
| 52 |
blob_dealloc(PyObject *op) |
| 53 |
{ |
| 54 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 55 |
PyTypeObject *tp = Py_TYPE(self); |
| 56 |
PyObject_GC_UnTrack(self); |
| 57 |
|
| 58 |
close_blob(self); |
| 59 |
|
| 60 |
FT_CLEAR_WEAKREFS(op, self->in_weakreflist); |
| 61 |
(void)tp->tp_clear(op); |
| 62 |
tp->tp_free(self); |
| 63 |
Py_DECREF(tp); |
| 64 |
} |
| 65 |
|
| 66 |
// Return 1 if the blob object is usable, 0 if not. |
| 67 |
static int |
| 68 |
check_blob(pysqlite_Blob *self) |
| 69 |
{ |
| 70 |
if (!pysqlite_check_connection(self->connection) || |
| 71 |
!pysqlite_check_thread(self->connection)) { |
| 72 |
return 0; |
| 73 |
} |
| 74 |
if (self->blob == NULL) { |
| 75 |
pysqlite_state *state = self->connection->state; |
| 76 |
PyErr_SetString(state->ProgrammingError, |
| 77 |
"Cannot operate on a closed blob."); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/*[clinic input] |
| 85 |
_sqlite3.Blob.close as blob_close |
| 86 |
|
| 87 |
Close the blob. |
| 88 |
[clinic start generated code]*/ |
| 89 |
|
| 90 |
static PyObject * |
| 91 |
blob_close_impl(pysqlite_Blob *self) |
| 92 |
/*[clinic end generated code: output=848accc20a138d1b input=7bc178a402a40bd8]*/ |
| 93 |
{ |
| 94 |
if (!pysqlite_check_connection(self->connection) || |
| 95 |
!pysqlite_check_thread(self->connection)) |
| 96 |
{ |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
close_blob(self); |
| 100 |
Py_RETURN_NONE; |
| 101 |
}; |
| 102 |
|
| 103 |
void |
| 104 |
pysqlite_close_all_blobs(pysqlite_Connection *self) |
| 105 |
{ |
| 106 |
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(self->blobs); i++) { |
| 107 |
PyObject *weakref = PyList_GET_ITEM(self->blobs, i); |
| 108 |
PyObject *blob; |
| 109 |
if (!PyWeakref_GetRef(weakref, &blob)) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
close_blob((pysqlite_Blob *)blob); |
| 113 |
Py_DECREF(blob); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
blob_seterror(pysqlite_Blob *self, int rc) |
| 119 |
{ |
| 120 |
assert(self->connection != NULL); |
| 121 |
set_error_from_db(self->connection->state, self->connection->db); |
| 122 |
} |
| 123 |
|
| 124 |
static PyObject * |
| 125 |
read_single(pysqlite_Blob *self, Py_ssize_t offset) |
| 126 |
{ |
| 127 |
unsigned char buf = 0; |
| 128 |
int rc; |
| 129 |
Py_BEGIN_ALLOW_THREADS |
| 130 |
rc = sqlite3_blob_read(self->blob, (void *)&buf, 1, (int)offset); |
| 131 |
Py_END_ALLOW_THREADS |
| 132 |
|
| 133 |
if (rc != SQLITE_OK) { |
| 134 |
blob_seterror(self, rc); |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
return PyLong_FromUnsignedLong((unsigned long)buf); |
| 138 |
} |
| 139 |
|
| 140 |
static PyObject * |
| 141 |
read_multiple(pysqlite_Blob *self, Py_ssize_t length, Py_ssize_t offset) |
| 142 |
{ |
| 143 |
assert(length <= sqlite3_blob_bytes(self->blob)); |
| 144 |
assert(offset < sqlite3_blob_bytes(self->blob)); |
| 145 |
|
| 146 |
PyObject *buffer = PyBytes_FromStringAndSize(NULL, length); |
| 147 |
if (buffer == NULL) { |
| 148 |
return NULL; |
| 149 |
} |
| 150 |
|
| 151 |
char *raw_buffer = PyBytes_AS_STRING(buffer); |
| 152 |
int rc; |
| 153 |
Py_BEGIN_ALLOW_THREADS |
| 154 |
rc = sqlite3_blob_read(self->blob, raw_buffer, (int)length, (int)offset); |
| 155 |
Py_END_ALLOW_THREADS |
| 156 |
|
| 157 |
if (rc != SQLITE_OK) { |
| 158 |
Py_DECREF(buffer); |
| 159 |
blob_seterror(self, rc); |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
return buffer; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
/*[clinic input] |
| 167 |
_sqlite3.Blob.read as blob_read |
| 168 |
|
| 169 |
length: int = -1 |
| 170 |
Read length in bytes. |
| 171 |
/ |
| 172 |
|
| 173 |
Read data at the current offset position. |
| 174 |
|
| 175 |
If the end of the blob is reached, the data up to end of file will |
| 176 |
be returned. When length is not specified, or is negative, |
| 177 |
Blob.read() will read until the end of the blob. |
| 178 |
[clinic start generated code]*/ |
| 179 |
|
| 180 |
static PyObject * |
| 181 |
blob_read_impl(pysqlite_Blob *self, int length) |
| 182 |
/*[clinic end generated code: output=1fc99b2541360dde input=6b745ad37720e556]*/ |
| 183 |
{ |
| 184 |
if (!check_blob(self)) { |
| 185 |
return NULL; |
| 186 |
} |
| 187 |
|
| 188 |
/* Make sure we never read past "EOB". Also read the rest of the blob if a |
| 189 |
* negative length is specified. */ |
| 190 |
int blob_len = sqlite3_blob_bytes(self->blob); |
| 191 |
int max_read_len = blob_len - self->offset; |
| 192 |
if (length < 0 || length > max_read_len) { |
| 193 |
length = max_read_len; |
| 194 |
} |
| 195 |
|
| 196 |
assert(length >= 0); |
| 197 |
if (length == 0) { |
| 198 |
return PyBytes_FromStringAndSize(NULL, 0); |
| 199 |
} |
| 200 |
|
| 201 |
PyObject *buffer = read_multiple(self, length, self->offset); |
| 202 |
if (buffer == NULL) { |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
self->offset += length; |
| 206 |
return buffer; |
| 207 |
}; |
| 208 |
|
| 209 |
static int |
| 210 |
inner_write(pysqlite_Blob *self, const void *buf, Py_ssize_t len, |
| 211 |
Py_ssize_t offset) |
| 212 |
{ |
| 213 |
Py_ssize_t blob_len = sqlite3_blob_bytes(self->blob); |
| 214 |
Py_ssize_t remaining_len = blob_len - offset; |
| 215 |
if (len > remaining_len) { |
| 216 |
PyErr_SetString(PyExc_ValueError, "data longer than blob length"); |
| 217 |
return -1; |
| 218 |
} |
| 219 |
|
| 220 |
assert(offset <= blob_len); |
| 221 |
int rc; |
| 222 |
Py_BEGIN_ALLOW_THREADS |
| 223 |
rc = sqlite3_blob_write(self->blob, buf, (int)len, (int)offset); |
| 224 |
Py_END_ALLOW_THREADS |
| 225 |
|
| 226 |
if (rc != SQLITE_OK) { |
| 227 |
blob_seterror(self, rc); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/*[clinic input] |
| 235 |
_sqlite3.Blob.write as blob_write |
| 236 |
|
| 237 |
data: Py_buffer |
| 238 |
/ |
| 239 |
|
| 240 |
Write data at the current offset. |
| 241 |
|
| 242 |
This function cannot change the blob length. Writing beyond the end |
| 243 |
of the blob will result in an exception being raised. |
| 244 |
[clinic start generated code]*/ |
| 245 |
|
| 246 |
static PyObject * |
| 247 |
blob_write_impl(pysqlite_Blob *self, Py_buffer *data) |
| 248 |
/*[clinic end generated code: output=b34cf22601b570b2 input=0d372cb0240a5d49]*/ |
| 249 |
{ |
| 250 |
if (!check_blob(self)) { |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
int rc = inner_write(self, data->buf, data->len, self->offset); |
| 255 |
if (rc < 0) { |
| 256 |
return NULL; |
| 257 |
} |
| 258 |
self->offset += (int)data->len; |
| 259 |
Py_RETURN_NONE; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
/*[clinic input] |
| 264 |
_sqlite3.Blob.seek as blob_seek |
| 265 |
|
| 266 |
offset: int |
| 267 |
origin: int = 0 |
| 268 |
/ |
| 269 |
|
| 270 |
Set the current access position to offset. |
| 271 |
|
| 272 |
The origin argument defaults to os.SEEK_SET (absolute blob |
| 273 |
positioning). Other values for origin are os.SEEK_CUR (seek |
| 274 |
relative to the current position) and os.SEEK_END (seek relative to |
| 275 |
the blob's end). |
| 276 |
[clinic start generated code]*/ |
| 277 |
|
| 278 |
static PyObject * |
| 279 |
blob_seek_impl(pysqlite_Blob *self, int offset, int origin) |
| 280 |
/*[clinic end generated code: output=854c5a0e208547a5 input=84aea1b6b48607dd]*/ |
| 281 |
{ |
| 282 |
if (!check_blob(self)) { |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
int blob_len = sqlite3_blob_bytes(self->blob); |
| 287 |
switch (origin) { |
| 288 |
case SEEK_SET: |
| 289 |
break; |
| 290 |
case SEEK_CUR: |
| 291 |
if (offset > INT_MAX - self->offset) { |
| 292 |
goto overflow; |
| 293 |
} |
| 294 |
offset += self->offset; |
| 295 |
break; |
| 296 |
case SEEK_END: |
| 297 |
if (offset > INT_MAX - blob_len) { |
| 298 |
goto overflow; |
| 299 |
} |
| 300 |
offset += blob_len; |
| 301 |
break; |
| 302 |
default: |
| 303 |
PyErr_SetString(PyExc_ValueError, |
| 304 |
"'origin' should be os.SEEK_SET, os.SEEK_CUR, or " |
| 305 |
"os.SEEK_END"); |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
if (offset < 0 || offset > blob_len) { |
| 310 |
PyErr_SetString(PyExc_ValueError, "offset out of blob range"); |
| 311 |
return NULL; |
| 312 |
} |
| 313 |
|
| 314 |
self->offset = offset; |
| 315 |
Py_RETURN_NONE; |
| 316 |
|
| 317 |
overflow: |
| 318 |
PyErr_SetString(PyExc_OverflowError, "seek offset results in overflow"); |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
/*[clinic input] |
| 324 |
_sqlite3.Blob.tell as blob_tell |
| 325 |
|
| 326 |
Return the current access position for the blob. |
| 327 |
[clinic start generated code]*/ |
| 328 |
|
| 329 |
static PyObject * |
| 330 |
blob_tell_impl(pysqlite_Blob *self) |
| 331 |
/*[clinic end generated code: output=3d3ba484a90b3a99 input=7e34057aa303612c]*/ |
| 332 |
{ |
| 333 |
if (!check_blob(self)) { |
| 334 |
return NULL; |
| 335 |
} |
| 336 |
return PyLong_FromLong(self->offset); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
/*[clinic input] |
| 341 |
_sqlite3.Blob.__enter__ as blob_enter |
| 342 |
|
| 343 |
Blob context manager enter. |
| 344 |
[clinic start generated code]*/ |
| 345 |
|
| 346 |
static PyObject * |
| 347 |
blob_enter_impl(pysqlite_Blob *self) |
| 348 |
/*[clinic end generated code: output=4fd32484b071a6cd input=fe4842c3c582d5a7]*/ |
| 349 |
{ |
| 350 |
if (!check_blob(self)) { |
| 351 |
return NULL; |
| 352 |
} |
| 353 |
return Py_NewRef(self); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/*[clinic input] |
| 358 |
_sqlite3.Blob.__exit__ as blob_exit |
| 359 |
|
| 360 |
type: object |
| 361 |
val: object |
| 362 |
tb: object |
| 363 |
/ |
| 364 |
|
| 365 |
Blob context manager exit. |
| 366 |
[clinic start generated code]*/ |
| 367 |
|
| 368 |
static PyObject * |
| 369 |
blob_exit_impl(pysqlite_Blob *self, PyObject *type, PyObject *val, |
| 370 |
PyObject *tb) |
| 371 |
/*[clinic end generated code: output=fc86ceeb2b68c7b2 input=575d9ecea205f35f]*/ |
| 372 |
{ |
| 373 |
if (!check_blob(self)) { |
| 374 |
return NULL; |
| 375 |
} |
| 376 |
close_blob(self); |
| 377 |
Py_RETURN_FALSE; |
| 378 |
} |
| 379 |
|
| 380 |
static Py_ssize_t |
| 381 |
blob_length(PyObject *op) |
| 382 |
{ |
| 383 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 384 |
if (!check_blob(self)) { |
| 385 |
return -1; |
| 386 |
} |
| 387 |
return sqlite3_blob_bytes(self->blob); |
| 388 |
}; |
| 389 |
|
| 390 |
static Py_ssize_t |
| 391 |
get_subscript_index(pysqlite_Blob *self, PyObject *item) |
| 392 |
{ |
| 393 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 394 |
if (i == -1 && PyErr_Occurred()) { |
| 395 |
return -1; |
| 396 |
} |
| 397 |
int blob_len = sqlite3_blob_bytes(self->blob); |
| 398 |
if (i < 0) { |
| 399 |
i += blob_len; |
| 400 |
} |
| 401 |
if (i < 0 || i >= blob_len) { |
| 402 |
PyErr_SetString(PyExc_IndexError, "Blob index out of range"); |
| 403 |
return -1; |
| 404 |
} |
| 405 |
return i; |
| 406 |
} |
| 407 |
|
| 408 |
static PyObject * |
| 409 |
subscript_index(pysqlite_Blob *self, PyObject *item) |
| 410 |
{ |
| 411 |
Py_ssize_t i = get_subscript_index(self, item); |
| 412 |
if (i < 0) { |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
return read_single(self, i); |
| 416 |
} |
| 417 |
|
| 418 |
static int |
| 419 |
get_slice_info(pysqlite_Blob *self, PyObject *item, Py_ssize_t *start, |
| 420 |
Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelen) |
| 421 |
{ |
| 422 |
if (PySlice_Unpack(item, start, stop, step) < 0) { |
| 423 |
return -1; |
| 424 |
} |
| 425 |
int len = sqlite3_blob_bytes(self->blob); |
| 426 |
*slicelen = PySlice_AdjustIndices(len, start, stop, *step); |
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
static PyObject * |
| 431 |
subscript_slice(pysqlite_Blob *self, PyObject *item) |
| 432 |
{ |
| 433 |
Py_ssize_t start, stop, step, len; |
| 434 |
if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) { |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
if (len == 0) { |
| 439 |
return PyBytes_FromStringAndSize(NULL, 0); |
| 440 |
} |
| 441 |
|
| 442 |
if (step == 1) { |
| 443 |
return read_multiple(self, len, start); |
| 444 |
} |
| 445 |
PyObject *blob = read_multiple(self, stop - start, start); |
| 446 |
if (blob == NULL) { |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
PyObject *result = PyBytes_FromStringAndSize(NULL, len); |
| 450 |
if (result != NULL) { |
| 451 |
char *blob_buf = PyBytes_AS_STRING(blob); |
| 452 |
char *res_buf = PyBytes_AS_STRING(result); |
| 453 |
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) { |
| 454 |
res_buf[i] = blob_buf[j]; |
| 455 |
} |
| 456 |
Py_DECREF(blob); |
| 457 |
} |
| 458 |
return result; |
| 459 |
} |
| 460 |
|
| 461 |
static PyObject * |
| 462 |
blob_subscript(PyObject *op, PyObject *item) |
| 463 |
{ |
| 464 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 465 |
if (!check_blob(self)) { |
| 466 |
return NULL; |
| 467 |
} |
| 468 |
|
| 469 |
if (PyIndex_Check(item)) { |
| 470 |
return subscript_index(self, item); |
| 471 |
} |
| 472 |
if (PySlice_Check(item)) { |
| 473 |
return subscript_slice(self, item); |
| 474 |
} |
| 475 |
|
| 476 |
PyErr_SetString(PyExc_TypeError, "Blob indices must be integers"); |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
static int |
| 481 |
ass_subscript_index(pysqlite_Blob *self, PyObject *item, PyObject *value) |
| 482 |
{ |
| 483 |
if (value == NULL) { |
| 484 |
PyErr_SetString(PyExc_TypeError, |
| 485 |
"Blob doesn't support item deletion"); |
| 486 |
return -1; |
| 487 |
} |
| 488 |
if (!PyLong_Check(value)) { |
| 489 |
PyErr_Format(PyExc_TypeError, |
| 490 |
"'%s' object cannot be interpreted as an integer", |
| 491 |
Py_TYPE(value)->tp_name); |
| 492 |
return -1; |
| 493 |
} |
| 494 |
Py_ssize_t i = get_subscript_index(self, item); |
| 495 |
if (i < 0) { |
| 496 |
return -1; |
| 497 |
} |
| 498 |
|
| 499 |
long val = PyLong_AsLong(value); |
| 500 |
if (val == -1 && PyErr_Occurred()) { |
| 501 |
PyErr_Clear(); |
| 502 |
val = -1; |
| 503 |
} |
| 504 |
if (val < 0 || val > 255) { |
| 505 |
PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 506 |
return -1; |
| 507 |
} |
| 508 |
// Downcast to avoid endianness problems. |
| 509 |
unsigned char byte = (unsigned char)val; |
| 510 |
return inner_write(self, (const void *)&byte, 1, i); |
| 511 |
} |
| 512 |
|
| 513 |
static int |
| 514 |
ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value) |
| 515 |
{ |
| 516 |
if (value == NULL) { |
| 517 |
PyErr_SetString(PyExc_TypeError, |
| 518 |
"Blob doesn't support slice deletion"); |
| 519 |
return -1; |
| 520 |
} |
| 521 |
|
| 522 |
Py_ssize_t start, stop, step, len; |
| 523 |
if (get_slice_info(self, item, &start, &stop, &step, &len) < 0) { |
| 524 |
return -1; |
| 525 |
} |
| 526 |
|
| 527 |
Py_buffer vbuf; |
| 528 |
if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) { |
| 529 |
return -1; |
| 530 |
} |
| 531 |
|
| 532 |
if (vbuf.len != len) { |
| 533 |
PyErr_SetString(PyExc_IndexError, |
| 534 |
"Blob slice assignment is wrong size"); |
| 535 |
PyBuffer_Release(&vbuf); |
| 536 |
return -1; |
| 537 |
} |
| 538 |
|
| 539 |
if (len == 0) { |
| 540 |
PyBuffer_Release(&vbuf); |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
int rc = -1; |
| 545 |
if (step == 1) { |
| 546 |
rc = inner_write(self, vbuf.buf, len, start); |
| 547 |
} |
| 548 |
else { |
| 549 |
PyObject *blob_bytes = read_multiple(self, stop - start, start); |
| 550 |
if (blob_bytes != NULL) { |
| 551 |
char *blob_buf = PyBytes_AS_STRING(blob_bytes); |
| 552 |
for (Py_ssize_t i = 0, j = 0; i < len; i++, j += step) { |
| 553 |
blob_buf[j] = ((char *)vbuf.buf)[i]; |
| 554 |
} |
| 555 |
rc = inner_write(self, blob_buf, stop - start, start); |
| 556 |
Py_DECREF(blob_bytes); |
| 557 |
} |
| 558 |
} |
| 559 |
PyBuffer_Release(&vbuf); |
| 560 |
return rc; |
| 561 |
} |
| 562 |
|
| 563 |
static int |
| 564 |
blob_ass_subscript(PyObject *op, PyObject *item, PyObject *value) |
| 565 |
{ |
| 566 |
pysqlite_Blob *self = _pysqlite_Blob_CAST(op); |
| 567 |
if (!check_blob(self)) { |
| 568 |
return -1; |
| 569 |
} |
| 570 |
|
| 571 |
if (PyIndex_Check(item)) { |
| 572 |
return ass_subscript_index(self, item, value); |
| 573 |
} |
| 574 |
if (PySlice_Check(item)) { |
| 575 |
return ass_subscript_slice(self, item, value); |
| 576 |
} |
| 577 |
|
| 578 |
PyErr_SetString(PyExc_TypeError, "Blob indices must be integers"); |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
static PyMethodDef blob_methods[] = { |
| 584 |
BLOB_CLOSE_METHODDEF |
| 585 |
BLOB_ENTER_METHODDEF |
| 586 |
BLOB_EXIT_METHODDEF |
| 587 |
BLOB_READ_METHODDEF |
| 588 |
BLOB_SEEK_METHODDEF |
| 589 |
BLOB_TELL_METHODDEF |
| 590 |
BLOB_WRITE_METHODDEF |
| 591 |
{NULL, NULL} |
| 592 |
}; |
| 593 |
|
| 594 |
static struct PyMemberDef blob_members[] = { |
| 595 |
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(pysqlite_Blob, in_weakreflist), Py_READONLY}, |
| 596 |
{NULL}, |
| 597 |
}; |
| 598 |
|
| 599 |
static PyType_Slot blob_slots[] = { |
| 600 |
{Py_tp_dealloc, blob_dealloc}, |
| 601 |
{Py_tp_traverse, blob_traverse}, |
| 602 |
{Py_tp_clear, blob_clear}, |
| 603 |
{Py_tp_methods, blob_methods}, |
| 604 |
{Py_tp_members, blob_members}, |
| 605 |
|
| 606 |
// Mapping protocol |
| 607 |
{Py_mp_length, blob_length}, |
| 608 |
{Py_mp_subscript, blob_subscript}, |
| 609 |
{Py_mp_ass_subscript, blob_ass_subscript}, |
| 610 |
{0, NULL}, |
| 611 |
}; |
| 612 |
|
| 613 |
static PyType_Spec blob_spec = { |
| 614 |
.name = MODULE_NAME ".Blob", |
| 615 |
.basicsize = sizeof(pysqlite_Blob), |
| 616 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 617 |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), |
| 618 |
.slots = blob_slots, |
| 619 |
}; |
| 620 |
|
| 621 |
int |
| 622 |
pysqlite_blob_setup_types(PyObject *mod) |
| 623 |
{ |
| 624 |
PyObject *type = PyType_FromModuleAndSpec(mod, &blob_spec, NULL); |
| 625 |
if (type == NULL) { |
| 626 |
return -1; |
| 627 |
} |
| 628 |
pysqlite_state *state = pysqlite_get_state(mod); |
| 629 |
state->BlobType = (PyTypeObject *)type; |
| 630 |
return 0; |
| 631 |
} |