@@ -223,6 +223,7 @@ pysqlite_do_all_statements(pysqlite_Connection *self)
223223do { \
224224 if (ctx) { \
225225 Py_VISIT(ctx->callable); \
226+ Py_VISIT(ctx->connection); \
226227 } \
227228} while (0)
228229
@@ -247,6 +248,7 @@ clear_callback_context(callback_context *ctx)
247248{
248249 if (ctx != NULL ) {
249250 Py_CLEAR (ctx -> callable );
251+ Py_CLEAR (ctx -> connection );
250252 }
251253}
252254
@@ -816,12 +818,13 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
816818}
817819
818820static callback_context *
819- create_callback_context (pysqlite_state * state , PyObject * callable )
821+ create_callback_context (pysqlite_Connection * con , PyObject * callable )
820822{
821823 callback_context * ctx = PyMem_Malloc (sizeof (callback_context ));
822824 if (ctx != NULL ) {
823825 ctx -> callable = Py_NewRef (callable );
824- ctx -> state = state ;
826+ ctx -> state = con -> state ;
827+ ctx -> connection = Py_NewRef (con );
825828 }
826829 return ctx ;
827830}
@@ -831,6 +834,7 @@ free_callback_context(callback_context *ctx)
831834{
832835 assert (ctx != NULL );
833836 Py_XDECREF (ctx -> callable );
837+ Py_XDECREF (ctx -> connection );
834838 PyMem_Free (ctx );
835839}
836840
@@ -896,7 +900,7 @@ pysqlite_connection_create_function_impl(pysqlite_Connection *self,
896900 flags |= SQLITE_DETERMINISTIC ;
897901#endif
898902 }
899- callback_context * ctx = create_callback_context (self -> state , func );
903+ callback_context * ctx = create_callback_context (self , func );
900904 if (ctx == NULL ) {
901905 return NULL ;
902906 }
@@ -936,8 +940,7 @@ pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
936940 return NULL ;
937941 }
938942
939- callback_context * ctx = create_callback_context (self -> state ,
940- aggregate_class );
943+ callback_context * ctx = create_callback_context (self , aggregate_class );
941944 if (ctx == NULL ) {
942945 return NULL ;
943946 }
@@ -1088,7 +1091,7 @@ pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
10881091 set_callback_context (& self -> authorizer_ctx , NULL );
10891092 }
10901093 else {
1091- callback_context * ctx = create_callback_context (self -> state , callable );
1094+ callback_context * ctx = create_callback_context (self , callable );
10921095 if (ctx == NULL ) {
10931096 return NULL ;
10941097 }
@@ -1128,7 +1131,7 @@ pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
11281131 set_callback_context (& self -> progress_ctx , NULL );
11291132 }
11301133 else {
1131- callback_context * ctx = create_callback_context (self -> state , callable );
1134+ callback_context * ctx = create_callback_context (self , callable );
11321135 if (ctx == NULL ) {
11331136 return NULL ;
11341137 }
@@ -1173,7 +1176,7 @@ pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
11731176 set_callback_context (& self -> trace_ctx , NULL );
11741177 }
11751178 else {
1176- callback_context * ctx = create_callback_context (self -> state , callable );
1179+ callback_context * ctx = create_callback_context (self , callable );
11771180 if (ctx == NULL ) {
11781181 return NULL ;
11791182 }
@@ -1766,7 +1769,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
17661769 PyErr_SetString (PyExc_TypeError , "parameter must be callable" );
17671770 return NULL ;
17681771 }
1769- ctx = create_callback_context (self -> state , callable );
1772+ ctx = create_callback_context (self , callable );
17701773 if (ctx == NULL ) {
17711774 return NULL ;
17721775 }
0 commit comments