@@ -482,32 +482,35 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
482482 }
483483}
484484
485- void _pysqlite_set_result (sqlite3_context * context , PyObject * py_val )
485+ static int
486+ _pysqlite_set_result (sqlite3_context * context , PyObject * py_val )
486487{
487- const char * buffer ;
488- Py_ssize_t buflen ;
489-
490- if ((!py_val ) || PyErr_Occurred ()) {
491- sqlite3_result_null (context );
492- } else if (py_val == Py_None ) {
488+ if (py_val == Py_None ) {
493489 sqlite3_result_null (context );
494490 } else if (PyLong_Check (py_val )) {
495- sqlite3_result_int64 (context , PyLong_AsLongLong (py_val ));
491+ sqlite_int64 value = _pysqlite_long_as_int64 (py_val );
492+ if (value == -1 && PyErr_Occurred ())
493+ return -1 ;
494+ sqlite3_result_int64 (context , value );
496495 } else if (PyFloat_Check (py_val )) {
497496 sqlite3_result_double (context , PyFloat_AsDouble (py_val ));
498497 } else if (PyUnicode_Check (py_val )) {
499- char * str = _PyUnicode_AsString (py_val );
500- if (str != NULL )
501- sqlite3_result_text (context , str , -1 , SQLITE_TRANSIENT );
498+ const char * str = _PyUnicode_AsString (py_val );
499+ if (str == NULL )
500+ return -1 ;
501+ sqlite3_result_text (context , str , -1 , SQLITE_TRANSIENT );
502502 } else if (PyObject_CheckBuffer (py_val )) {
503+ const char * buffer ;
504+ Py_ssize_t buflen ;
503505 if (PyObject_AsCharBuffer (py_val , & buffer , & buflen ) != 0 ) {
504506 PyErr_SetString (PyExc_ValueError , "could not convert BLOB to buffer" );
505- } else {
506- sqlite3_result_blob (context , buffer , buflen , SQLITE_TRANSIENT );
507+ return -1 ;
507508 }
509+ sqlite3_result_blob (context , buffer , buflen , SQLITE_TRANSIENT );
508510 } else {
509- /* TODO: raise error */
511+ return -1 ;
510512 }
513+ return 0 ;
511514}
512515
513516PyObject * _pysqlite_build_py_params (sqlite3_context * context , int argc , sqlite3_value * * argv )
@@ -528,7 +531,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
528531 cur_value = argv [i ];
529532 switch (sqlite3_value_type (argv [i ])) {
530533 case SQLITE_INTEGER :
531- cur_py_value = PyLong_FromLongLong (sqlite3_value_int64 (cur_value ));
534+ cur_py_value = _pysqlite_long_from_int64 (sqlite3_value_int64 (cur_value ));
532535 break ;
533536 case SQLITE_FLOAT :
534537 cur_py_value = PyFloat_FromDouble (sqlite3_value_double (cur_value ));
@@ -571,6 +574,7 @@ void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value**
571574 PyObject * args ;
572575 PyObject * py_func ;
573576 PyObject * py_retval = NULL ;
577+ int ok ;
574578
575579#ifdef WITH_THREAD
576580 PyGILState_STATE threadstate ;
@@ -586,10 +590,12 @@ void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value**
586590 Py_DECREF (args );
587591 }
588592
593+ ok = 0 ;
589594 if (py_retval ) {
590- _pysqlite_set_result (context , py_retval );
595+ ok = _pysqlite_set_result (context , py_retval ) == 0 ;
591596 Py_DECREF (py_retval );
592- } else {
597+ }
598+ if (!ok ) {
593599 if (_enable_callback_tracebacks ) {
594600 PyErr_Print ();
595601 } else {
@@ -669,9 +675,10 @@ static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_
669675
670676void _pysqlite_final_callback (sqlite3_context * context )
671677{
672- PyObject * function_result = NULL ;
678+ PyObject * function_result ;
673679 PyObject * * aggregate_instance ;
674680 _Py_IDENTIFIER (finalize );
681+ int ok ;
675682
676683#ifdef WITH_THREAD
677684 PyGILState_STATE threadstate ;
@@ -688,21 +695,23 @@ void _pysqlite_final_callback(sqlite3_context* context)
688695 }
689696
690697 function_result = _PyObject_CallMethodId (* aggregate_instance , & PyId_finalize , "" );
691- if (!function_result ) {
698+ Py_DECREF (* aggregate_instance );
699+
700+ ok = 0 ;
701+ if (function_result ) {
702+ ok = _pysqlite_set_result (context , function_result ) == 0 ;
703+ Py_DECREF (function_result );
704+ }
705+ if (!ok ) {
692706 if (_enable_callback_tracebacks ) {
693707 PyErr_Print ();
694708 } else {
695709 PyErr_Clear ();
696710 }
697711 _sqlite3_result_error (context , "user-defined aggregate's 'finalize' method raised error" , -1 );
698- } else {
699- _pysqlite_set_result (context , function_result );
700712 }
701713
702714error :
703- Py_XDECREF (* aggregate_instance );
704- Py_XDECREF (function_result );
705-
706715#ifdef WITH_THREAD
707716 PyGILState_Release (threadstate );
708717#endif
@@ -859,7 +868,9 @@ static int _authorizer_callback(void* user_arg, int action, const char* arg1, co
859868 rc = SQLITE_DENY ;
860869 } else {
861870 if (PyLong_Check (ret )) {
862- rc = (int )PyLong_AsLong (ret );
871+ rc = _PyLong_AsInt (ret );
872+ if (rc == -1 && PyErr_Occurred ())
873+ rc = SQLITE_DENY ;
863874 } else {
864875 rc = SQLITE_DENY ;
865876 }
@@ -1327,6 +1338,7 @@ pysqlite_collation_callback(
13271338 PyGILState_STATE gilstate ;
13281339#endif
13291340 PyObject * retval = NULL ;
1341+ long longval ;
13301342 int result = 0 ;
13311343#ifdef WITH_THREAD
13321344 gilstate = PyGILState_Ensure ();
@@ -1350,10 +1362,17 @@ pysqlite_collation_callback(
13501362 goto finally ;
13511363 }
13521364
1353- result = PyLong_AsLong (retval );
1354- if (PyErr_Occurred ()) {
1365+ longval = PyLong_AsLongAndOverflow (retval , & result );
1366+ if (longval == -1 && PyErr_Occurred ()) {
1367+ PyErr_Clear ();
13551368 result = 0 ;
13561369 }
1370+ else if (!result ) {
1371+ if (longval > 0 )
1372+ result = 1 ;
1373+ else if (longval < 0 )
1374+ result = -1 ;
1375+ }
13571376
13581377finally :
13591378 Py_XDECREF (string1 );
0 commit comments