@@ -11,6 +11,10 @@ int pysqlite_blob_init(pysqlite_Blob *self, pysqlite_Connection* connection,
11
11
self -> blob = blob ;
12
12
self -> in_weakreflist = NULL ;
13
13
14
+ Py_BEGIN_ALLOW_THREADS
15
+ self -> length = sqlite3_blob_bytes (self -> blob );
16
+ Py_END_ALLOW_THREADS
17
+
14
18
if (!pysqlite_check_thread (self -> connection )) {
15
19
return -1 ;
16
20
}
@@ -94,15 +98,11 @@ PyObject* pysqlite_blob_close(pysqlite_Blob *self)
94
98
95
99
static Py_ssize_t pysqlite_blob_length (pysqlite_Blob * self )
96
100
{
97
- int blob_length ;
98
101
if (!pysqlite_check_blob (self )) {
99
102
return -1 ;
100
103
}
101
- Py_BEGIN_ALLOW_THREADS
102
- blob_length = sqlite3_blob_bytes (self -> blob );
103
- Py_END_ALLOW_THREADS
104
104
105
- return blob_length ;
105
+ return self -> length ;
106
106
};
107
107
108
108
static PyObject * inner_read (pysqlite_Blob * self , int read_length , int offset )
@@ -140,7 +140,6 @@ static PyObject* inner_read(pysqlite_Blob *self, int read_length, int offset)
140
140
PyObject * pysqlite_blob_read (pysqlite_Blob * self , PyObject * args )
141
141
{
142
142
int read_length = -1 ;
143
- int blob_length = 0 ;
144
143
PyObject * buffer ;
145
144
146
145
if (!PyArg_ParseTuple (args , "|i" , & read_length )) {
@@ -151,20 +150,14 @@ PyObject* pysqlite_blob_read(pysqlite_Blob *self, PyObject *args)
151
150
return NULL ;
152
151
}
153
152
154
-
155
- /* TODO: make this multithreaded and safe! */
156
- Py_BEGIN_ALLOW_THREADS
157
- blob_length = sqlite3_blob_bytes (self -> blob );
158
- Py_END_ALLOW_THREADS
159
-
160
153
if (read_length < 0 ) {
161
154
/* same as file read. */
162
- read_length = blob_length ;
155
+ read_length = self -> length ;
163
156
}
164
157
165
158
/* making sure we don't read more then blob size */
166
- if (read_length > blob_length - self -> offset ) {
167
- read_length = blob_length - self -> offset ;
159
+ if (read_length > self -> length - self -> offset ) {
160
+ read_length = self -> length - self -> offset ;
168
161
}
169
162
170
163
buffer = inner_read (self , read_length , self -> offset );
@@ -236,7 +229,7 @@ PyObject* pysqlite_blob_write(pysqlite_Blob *self, PyObject *data)
236
229
237
230
PyObject * pysqlite_blob_seek (pysqlite_Blob * self , PyObject * args )
238
231
{
239
- int blob_length , offset , from_what = 0 ;
232
+ int offset , from_what = 0 ;
240
233
241
234
if (!PyArg_ParseTuple (args , "i|i" , & offset , & from_what )) {
242
235
return NULL ;
@@ -247,10 +240,6 @@ PyObject* pysqlite_blob_seek(pysqlite_Blob *self, PyObject *args)
247
240
return NULL ;
248
241
}
249
242
250
- Py_BEGIN_ALLOW_THREADS
251
- blob_length = sqlite3_blob_bytes (self -> blob );
252
- Py_END_ALLOW_THREADS
253
-
254
243
switch (from_what ) {
255
244
case 0 : // relative to blob begin
256
245
break ;
@@ -261,17 +250,17 @@ PyObject* pysqlite_blob_seek(pysqlite_Blob *self, PyObject *args)
261
250
offset = self -> offset + offset ;
262
251
break ;
263
252
case 2 : // relative to blob end
264
- if (offset > INT_MAX - blob_length ) {
253
+ if (offset > INT_MAX - self -> length ) {
265
254
goto overflow ;
266
255
}
267
- offset = blob_length + offset ;
256
+ offset = self -> length + offset ;
268
257
break ;
269
258
default :
270
259
return PyErr_Format (PyExc_ValueError ,
271
260
"from_what should be 0, 1 or 2" );
272
261
}
273
262
274
- if (offset < 0 || offset > blob_length ) {
263
+ if (offset < 0 || offset > self -> length ) {
275
264
return PyErr_Format (PyExc_ValueError , "offset out of blob range" );
276
265
}
277
266
@@ -340,17 +329,11 @@ static PyObject* pysqlite_blob_repeat(pysqlite_Blob *self, PyObject *args)
340
329
341
330
static PyObject * pysqlite_blob_item (pysqlite_Blob * self , Py_ssize_t i )
342
331
{
343
- int blob_length = 0 ;
344
-
345
332
if (!pysqlite_check_blob (self )) {
346
333
return NULL ;
347
334
}
348
335
349
- Py_BEGIN_ALLOW_THREADS
350
- blob_length = sqlite3_blob_bytes (self -> blob );
351
- Py_END_ALLOW_THREADS
352
-
353
- if (i < 0 || i >= blob_length ) {
336
+ if (i < 0 || i >= self -> length ) {
354
337
PyErr_SetString (PyExc_IndexError , "Blob index out of range" );
355
338
return NULL ;
356
339
}
@@ -360,18 +343,13 @@ static PyObject* pysqlite_blob_item(pysqlite_Blob *self, Py_ssize_t i)
360
343
361
344
static int pysqlite_blob_ass_item (pysqlite_Blob * self , Py_ssize_t i , PyObject * v )
362
345
{
363
- int blob_length = 0 ;
364
346
const char * buf ;
365
347
366
348
if (!pysqlite_check_blob (self )) {
367
349
return -1 ;
368
350
}
369
351
370
- Py_BEGIN_ALLOW_THREADS
371
- blob_length = sqlite3_blob_bytes (self -> blob );
372
- Py_END_ALLOW_THREADS
373
-
374
- if (i < 0 || i >= blob_length ) {
352
+ if (i < 0 || i >= self -> length ) {
375
353
PyErr_SetString (PyExc_IndexError , "Blob index out of range" );
376
354
return -1 ;
377
355
}
@@ -393,23 +371,17 @@ static int pysqlite_blob_ass_item(pysqlite_Blob *self, Py_ssize_t i, PyObject *v
393
371
394
372
static PyObject * pysqlite_blob_subscript (pysqlite_Blob * self , PyObject * item )
395
373
{
396
- int blob_length = 0 ;
397
-
398
374
if (!pysqlite_check_blob (self )) {
399
375
return NULL ;
400
376
}
401
377
402
- Py_BEGIN_ALLOW_THREADS
403
- blob_length = sqlite3_blob_bytes (self -> blob );
404
- Py_END_ALLOW_THREADS
405
-
406
378
if (PyIndex_Check (item )) {
407
379
Py_ssize_t i = PyNumber_AsSsize_t (item , PyExc_IndexError );
408
380
if (i == -1 && PyErr_Occurred ())
409
381
return NULL ;
410
382
if (i < 0 )
411
- i += blob_length ;
412
- if (i < 0 || i >= blob_length ) {
383
+ i += self -> length ;
384
+ if (i < 0 || i >= self -> length ) {
413
385
PyErr_SetString (PyExc_IndexError ,
414
386
"Blob index out of range" );
415
387
return NULL ;
@@ -420,7 +392,7 @@ static PyObject * pysqlite_blob_subscript(pysqlite_Blob *self, PyObject *item)
420
392
else if (PySlice_Check (item )) {
421
393
Py_ssize_t start , stop , step , slicelen ;
422
394
423
- if (PySlice_GetIndicesEx (item , blob_length ,
395
+ if (PySlice_GetIndicesEx (item , self -> length ,
424
396
& start , & stop , & step , & slicelen ) < 0 ) {
425
397
return NULL ;
426
398
}
@@ -484,26 +456,21 @@ static PyObject * pysqlite_blob_subscript(pysqlite_Blob *self, PyObject *item)
484
456
485
457
static int pysqlite_blob_ass_subscript (pysqlite_Blob * self , PyObject * item , PyObject * value )
486
458
{
487
- int blob_length = 0 ;
488
459
int rc ;
489
460
490
461
if (!pysqlite_check_blob (self )) {
491
462
return -1 ;
492
463
}
493
464
494
- Py_BEGIN_ALLOW_THREADS
495
- blob_length = sqlite3_blob_bytes (self -> blob );
496
- Py_END_ALLOW_THREADS
497
-
498
465
if (PyIndex_Check (item )) {
499
466
Py_ssize_t i = PyNumber_AsSsize_t (item , PyExc_IndexError );
500
467
const char * buf ;
501
468
502
469
if (i == -1 && PyErr_Occurred ())
503
470
return -1 ;
504
471
if (i < 0 )
505
- i += blob_length ;
506
- if (i < 0 || i >= blob_length ) {
472
+ i += self -> length ;
473
+ if (i < 0 || i >= self -> length ) {
507
474
PyErr_SetString (PyExc_IndexError ,
508
475
"Blob index out of range" );
509
476
return -1 ;
@@ -527,7 +494,7 @@ static int pysqlite_blob_ass_subscript(pysqlite_Blob *self, PyObject *item, PyOb
527
494
Py_buffer vbuf ;
528
495
529
496
if (PySlice_GetIndicesEx (item ,
530
- blob_length , & start , & stop ,
497
+ self -> length , & start , & stop ,
531
498
& step , & slicelen ) < 0 ) {
532
499
return -1 ;
533
500
}
0 commit comments