@@ -612,8 +612,10 @@ set_sqlite_error(sqlite3_context *context, const char *msg)
612
612
else {
613
613
sqlite3_result_error (context , msg , -1 );
614
614
}
615
- pysqlite_state * state = pysqlite_get_state (NULL );
616
- if (state -> enable_callback_tracebacks ) {
615
+ callback_context * ctx = (callback_context * )sqlite3_user_data (context );
616
+ assert (ctx != NULL );
617
+ assert (ctx -> state != NULL );
618
+ if (ctx -> state -> enable_callback_tracebacks ) {
617
619
PyErr_Print ();
618
620
}
619
621
else {
@@ -625,19 +627,18 @@ static void
625
627
_pysqlite_func_callback (sqlite3_context * context , int argc , sqlite3_value * * argv )
626
628
{
627
629
PyObject * args ;
628
- PyObject * py_func ;
629
630
PyObject * py_retval = NULL ;
630
631
int ok ;
631
632
632
633
PyGILState_STATE threadstate ;
633
634
634
635
threadstate = PyGILState_Ensure ();
635
636
636
- py_func = (PyObject * )sqlite3_user_data (context );
637
-
638
637
args = _pysqlite_build_py_params (context , argc , argv );
639
638
if (args ) {
640
- py_retval = PyObject_CallObject (py_func , args );
639
+ callback_context * ctx = (callback_context * )sqlite3_user_data (context );
640
+ assert (ctx != NULL );
641
+ py_retval = PyObject_CallObject (ctx -> callable , args );
641
642
Py_DECREF (args );
642
643
}
643
644
@@ -657,20 +658,19 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
657
658
{
658
659
PyObject * args ;
659
660
PyObject * function_result = NULL ;
660
- PyObject * aggregate_class ;
661
661
PyObject * * aggregate_instance ;
662
662
PyObject * stepmethod = NULL ;
663
663
664
664
PyGILState_STATE threadstate ;
665
665
666
666
threadstate = PyGILState_Ensure ();
667
667
668
- aggregate_class = (PyObject * )sqlite3_user_data (context );
669
-
670
668
aggregate_instance = (PyObject * * )sqlite3_aggregate_context (context , sizeof (PyObject * ));
671
669
672
670
if (* aggregate_instance == NULL ) {
673
- * aggregate_instance = _PyObject_CallNoArg (aggregate_class );
671
+ callback_context * ctx = (callback_context * )sqlite3_user_data (context );
672
+ assert (ctx != NULL );
673
+ * aggregate_instance = _PyObject_CallNoArg (ctx -> callable );
674
674
if (!* aggregate_instance ) {
675
675
set_sqlite_error (context ,
676
676
"user-defined aggregate's '__init__' method raised error" );
@@ -784,14 +784,35 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
784
784
Py_SETREF (self -> cursors , new_list );
785
785
}
786
786
787
- static void _destructor (void * args )
787
+ static callback_context *
788
+ create_callback_context (pysqlite_state * state , PyObject * callable )
788
789
{
789
- // This function may be called without the GIL held, so we need to ensure
790
- // that we destroy 'args' with the GIL
791
- PyGILState_STATE gstate ;
<
8000
/td>792
- gstate = PyGILState_Ensure ();
793
- Py_DECREF ((PyObject * )args );
790
+ PyGILState_STATE gstate = PyGILState_Ensure ();
791
+ callback_context * ctx = PyMem_Malloc (sizeof (callback_context ));
792
+ if (ctx != NULL ) {
793
+ ctx -> callable = Py_NewRef (callable );
794
+ ctx -> state = state ;
795
+ }
794
796
PyGILState_Release (gstate );
797
+ return ctx ;
798
+ }
799
+
800
+ static void
801
+ free_callback_context (callback_context * ctx )
802
+ {
803
+ if (ctx != NULL ) {
804
+ // This function may be called without the GIL held, so we need to
805
+ // ensure that we destroy 'ctx' with the GIL held.
806
+ PyGILState_STATE gstate = PyGILState_Ensure ();
807
+ Py_DECREF (ctx -> callable );
808
+ PyMem_Free (ctx );
809
+ PyGILState_Release (gstate );
810
+ }
811
+ }
812
+
813
+ static void _destructor (void * args )
814
+ {
815
+ free_callback_context ((callback_context * )args );
795
816
}
796
817
797
818
/*[clinic input]
@@ -833,11 +854,11 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,
833
854
flags |= SQLITE_DETERMINISTIC ;
834
855
#endif
835
856
}
836
- rc = sqlite3_create_function_v2 (self -> db ,
837
- name ,
838
- narg ,
839
- flags ,
840
-
A3E2
( void * ) Py_NewRef ( func ) ,
857
+ callback_context * ctx = create_callback_context (self -> state , func );
858
+ if ( ctx == NULL ) {
859
+ return NULL ;
860
+ }
861
+ rc = sqlite3_create_function_v2 ( self -> db , name , narg , flags , ctx ,
841
862
_pysqlite_func_callback ,
842
863
NULL ,
843
864
NULL ,
@@ -873,11 +894,12 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
873
894
return NULL ;
874
895
}
875
896
876
- rc = sqlite3_create_function_v2 (self -> db ,
877
- name ,
878
- n_arg ,
879
- SQLITE_UTF8 ,
880
- (void * )Py_NewRef (aggregate_class ),
897
+ callback_context * ctx = create_callback_context (self -> state ,
898
+ aggregate_class );
899
+ if (ctx == NULL ) {
900
+ return NULL ;
901
+ }
902
+ rc = sqlite3_create_function_v2 (self -> db , name , n_arg , SQLITE_UTF8 , ctx ,
881
903
0 ,
882
904
& _pysqlite_step_callback ,
883
905
& _pysqlite_final_callback ,
@@ -1439,7 +1461,6 @@ pysqlite_collation_callback(
1439
1461
int text1_length , const void * text1_data ,
1440
1462
int text2_length , const void * text2_data )
1441
1463
{
1442
- PyObject * callback = (PyObject * )context ;
1443
1464
PyObject * string1 = 0 ;
1444
1465
PyObject * string2 = 0 ;
1445
1466
PyGILState_STATE gilstate ;
@@ -1459,8 +1480,10 @@ pysqlite_collation_callback(
1459
1480
goto finally ; /* failed to allocate strings */
1460
1481
}
1461
1482
1483
+ callback_context * ctx = (callback_context * )context ;
1484
+ assert (ctx != NULL );
1462
1485
PyObject * args [] = { string1 , string2 }; // Borrowed refs.
1463
- retval = PyObject_Vectorcall (callback , args , 2 , NULL );
1486
+ retval = PyObject_Vectorcall (ctx -> callable , args , 2 , NULL );
1464
1487
if (retval == NULL ) {
1465
1488
/* execution failed */
1466
1489
goto finally ;
@@ -1690,6 +1713,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1690
1713
return NULL ;
1691
1714
}
1692
1715
1716
+ callback_context * ctx = NULL ;
1693
1717
int rc ;
1694
1718
int flags = SQLITE_UTF8 ;
1695
1719
if (callable == Py_None ) {
@@ -1701,8 +1725,11 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1701
1725
PyErr_SetString (PyExc_TypeError , "parameter must be callable" );
1702
1726
return NULL ;
1703
1727
}
1704
- rc = sqlite3_create_collation_v2 (self -> db , name , flags ,
1705
- Py_NewRef (callable ),
1728
+ ctx = create_callback_context (self -> state , callable );
1729
+ if (ctx == NULL ) {
1730
+ return NULL ;
1731
+ }
1732
+ rc = sqlite3_create_collation_v2 (self -> db , name , flags , ctx ,
1706
1733
& pysqlite_collation_callback ,
1707
1734
& _destructor );
1708
1735
}
@@ -1713,7 +1740,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1713
1740
* the context before returning.
1714
1741
*/
1715
1742
if (callable != Py_None ) {
1716
- Py_DECREF ( callable );
1743
+ free_callback_context ( ctx );
1717
1744
}
1718
1745
_pysqlite_seterror (self -> state , self -> db );
1719
1746
return NULL ;
0 commit comments