@@ -626,14 +626,12 @@ set_sqlite_error(sqlite3_context *context, const char *msg)
626
626
static void
627
627
_pysqlite_func_callback (sqlite3_context * context , int argc , sqlite3_value * * argv )
628
628
{
629
+ PyGILState_STATE threadstate = PyGILState_Ensure ();
630
+
629
631
PyObject * args ;
630
632
PyObject * py_retval = NULL ;
631
633
int ok ;
632
634
633
- PyGILState_STATE threadstate ;
634
-
635
- threadstate = PyGILState_Ensure ();
636
-
637
635
args = _pysqlite_build_py_params (context , argc , argv );
638
636
if (args ) {
639
637
callback_context * ctx = (callback_context * )sqlite3_user_data (context );
@@ -656,15 +654,13 @@ _pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv
656
654
657
655
static void _pysqlite_step_callback (sqlite3_context * context , int argc , sqlite3_value * * params )
658
656
{
657
+ PyGILState_STATE threadstate = PyGILState_Ensure ();
658
+
659
659
PyObject * args ;
660
660
PyObject * function_result = NULL ;
661
661
PyObject * * aggregate_instance ;
662
662
PyObject * stepmethod = NULL ;
663
663
664
- PyGILState_STATE threadstate ;
665
-
666
- threadstate = PyGILState_Ensure ();
667
-
668
664
aggregate_instance = (PyObject * * )sqlite3_aggregate_context (context , sizeof (PyObject * ));
669
665
670
666
if (* aggregate_instance == NULL ) {
@@ -706,16 +702,14 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
706
702
static void
707
703
_pysqlite_final_callback (sqlite3_context * context )
708
704
{
705
+ PyGILState_STATE threadstate = PyGILState_Ensure ();
706
+
709
707
PyObject * function_result ;
710
708
PyObject * * aggregate_instance ;
711
709
_Py_IDENTIFIER (finalize );
712
710
int ok ;
713
711
PyObject * exception , * value , * tb ;
714
712
715
- PyGILState_STATE threadstate ;
716
-
717
- threadstate = PyGILState_Ensure ();
718
-
719
713
aggregate_instance = (PyObject * * )sqlite3_aggregate_context (context , 0 );
720
714
if (aggregate_instance == NULL ) {
721
715
/* No rows matched the query; the step handler was never called. */
@@ -787,34 +781,34 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
787
781
static callback_context *
788
782
create_callback_context (pysqlite_state * state , PyObject * callable )
789
783
{
790
- PyGILState_STATE gstate = PyGILState_Ensure ();
791
784
callback_context * ctx = PyMem_Malloc (sizeof (callback_context ));
792
785
if (ctx != NULL ) {
793
786
ctx -> callable = Py_NewRef (callable );
794
787
ctx -> state = state ;
795
788
}
796
- PyGILState_Release (gstate );
797
789
return ctx ;
798
790
}
799
791
800
792
static void
801
793
free_callback_context (callback_context * ctx )
794
+ {
795
+ assert (ctx != NULL );
796
+ Py_DECREF (ctx -> callable );
797
+ PyMem_Free (ctx );
798
+ }
799
+
800
+ static void
801
+ _destructor (void * ctx )
802
802
{
803
803
if (ctx != NULL ) {
804
804
// This function may be called without the GIL held, so we need to
805
805
// ensure that we destroy 'ctx' with the GIL held.
806
806
PyGILState_STATE gstate = PyGILState_Ensure ();
807
- Py_DECREF (ctx -> callable );
808
- PyMem_Free (ctx );
807
+ free_callback_context ((callback_context * )ctx );
809
808
PyGILState_Release (gstate );
810
809
}
811
810
}
812
811
813
- static void _destructor (void * args )
814
- {
815
- free_callback_context ((callback_context * )args );
816
- }
817
-
818
812
/*[clinic input]
819
813
_sqlite3.Connection.create_function as pysqlite_connection_create_function
820
814
@@ -914,11 +908,10 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
914
908
915
909
static int _authorizer_callback (void * user_arg , int action , const char * arg1 , const char * arg2 , const char * dbname , const char * access_attempt_source )
916
910
{
911
+ PyGILState_STATE gilstate = PyGILState_Ensure ();
912
+
917
913
PyObject * ret ;
918
914
int rc ;
919
- PyGILState_STATE gilstate ;
920
-
921
- gilstate = PyGILState_Ensure ();
922
915
923
916
ret = PyObject_CallFunction ((PyObject * )user_arg , "issss" , action , arg1 , arg2 , dbname , access_attempt_source );
924
917
@@ -959,11 +952,10 @@ static int _authorizer_callback(void* user_arg, int action, const char* arg1, co
959
952
960
953
static int _progress_handler (void * user_arg )
961
954
{
955
+ PyGILState_STATE gilstate = PyGILState_Ensure ();
956
+
962
957
int rc ;
963
958
PyObject * ret ;
964
- PyGILState_STATE gilstate ;
965
-
966
- gilstate = PyGILState_Ensure ();
967
959
ret = _PyObject_CallNoArg ((PyObject * )user_arg );
968
960
969
961
if (!ret ) {
@@ -1000,18 +992,16 @@ static int _trace_callback(unsigned int type, void* user_arg, void* prepared_sta
1000
992
static void _trace_callback (void * user_arg , const char * statement_string )
1001
993
#endif
1002
994
{
1003
- PyObject * py_statement = NULL ;
1004
- PyObject * ret = NULL ;
1005
-
1006
- PyGILState_STATE gilstate ;
1007
-
1008
995
#ifdef HAVE_TRACE_V2
1009
996
if (type != SQLITE_TRACE_STMT ) {
1010
997
return 0 ;
1011
998
}
1012
999
#endif
1013
1000
1014
- gilstate = PyGILState_Ensure ();
1001
+ PyGILState_STATE gilstate = PyGILState_Ensure ();
1002
+
1003
+ PyObject * py_statement = NULL ;
1004
+ PyObject * ret = NULL ;
1015
1005
py_statement = PyUnicode_DecodeUTF8 (statement_string ,
1016
1006
strlen (statement_string ), "replace" );
1017
1007
if (py_statement ) {
@@ -1461,14 +1451,16 @@ pysqlite_collation_callback(
1461
1451
int text1_length , const void * text1_data ,
1462
1452
int text2_length , const void * text2_data )
1463
1453
{
1454
+ PyGILState_STATE gilstate = PyGILState_Ensure ();
1455
+
1464
1456
PyObject * string1 = 0 ;
1465
1457
PyObject * string2 = 0 ;
1466
- PyGILState_STATE gilstate ;
1467
1458
PyObject * retval = NULL ;
1468
1459
long longval ;
1469
1460
int result = 0 ;
1470
- gilstate = PyGILState_Ensure ();
1471
1461
1462
+ /* This callback may be executed multiple times per sqlite3_step(). Bail if
1463
+ * the previous call failed */
1472
1464
if (PyErr_Occurred ()) {
1473
1465
goto finally ;
1474
1466
}
0 commit comments