@@ -698,6 +698,7 @@ void _pysqlite_final_callback(sqlite3_context* context)
698698 _Py_IDENTIFIER (finalize );
699699 int ok ;
700700 PyObject * exception , * value , * tb ;
701+ int restore ;
701702
702703#ifdef WITH_THREAD
703704 PyGILState_STATE threadstate ;
@@ -715,6 +716,7 @@ void _pysqlite_final_callback(sqlite3_context* context)
715716
716717 /* Keep the exception (if any) of the last call to step() */
717718 PyErr_Fetch (& exception , & value , & tb );
719+ restore = 1 ;
718720
719721 function_result = _PyObject_CallMethodId (* aggregate_instance , & PyId_finalize , "" );
720722
@@ -732,11 +734,18 @@ void _pysqlite_final_callback(sqlite3_context* context)
732734 PyErr_Clear ();
733735 }
734736 _sqlite3_result_error (context , "user-defined aggregate's 'finalize' method raised error" , -1 );
737+ #if SQLITE_VERSION_NUMBER < 3003003
738+ /* with old SQLite versions, _sqlite3_result_error() sets a new Python
739+ exception, so don't restore the previous exception */
740+ restore = 0 ;
741+ #endif
735742 }
736743
737- /* Restore the exception (if any) of the last call to step(),
738- but clear also the current exception if finalize() failed */
739- PyErr_Restore (exception , value , tb );
744+ if (restore ) {
745+ /* Restore the exception (if any) of the last call to step(),
746+ but clear also the current exception if finalize() failed */
747+ PyErr_Restore (exception , value , tb );
748+ }
740749
741750error :
742751#ifdef WITH_THREAD
0 commit comments