@@ -430,9 +430,14 @@ static int check_cursor(pysqlite_Cursor* cur)
430430 if (cur -> closed ) {
431431 PyErr_SetString (pysqlite_ProgrammingError , "Cannot operate on a closed cursor." );
432432 return 0 ;
433- } else {
434- return pysqlite_check_thread (cur -> connection ) && pysqlite_check_connection (cur -> connection );
435433 }
434+
435+ if (cur -> locked ) {
436+ PyErr_SetString (pysqlite_ProgrammingError , "Recursive use of cursors not allowed." );
437+ return 0 ;
438+ }
439+
440+ return pysqlite_check_thread (cur -> connection ) && pysqlite_check_connection (cur -> connection );
436441}
437442
438443PyObject * _pysqlite_query_execute (pysqlite_Cursor * self , int multiple , PyObject * args )
@@ -455,9 +460,10 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
455460 int allow_8bit_chars ;
456461
457462 if (!check_cursor (self )) {
458- return NULL ;
463+ goto error ;
459464 }
460465
466+ self -> locked = 1 ;
461467 self -> reset = 0 ;
462468
463469 /* Make shooting yourself in the foot with not utf-8 decodable 8-bit-strings harder */
@@ -470,12 +476,12 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
470476 if (multiple ) {
471477 /* executemany() */
472478 if (!PyArg_ParseTuple (args , "OO" , & operation , & second_argument )) {
473- return NULL ;
479+ goto error ;
474480 }
475481
476482 if (!PyUnicode_Check (operation )) {
477483 PyErr_SetString (PyExc_ValueError , "operation parameter must be str" );
478- return NULL ;
484+ goto error ;
479485 }
480486
481487 if (PyIter_Check (second_argument )) {
@@ -486,23 +492,23 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
486492 /* sequence */
487493 parameters_iter = PyObject_GetIter (second_argument );
488494 if (!parameters_iter ) {
489- return NULL ;
495+ goto error ;
490496 }
491497 }
492498 } else {
493499 /* execute() */
494500 if (!PyArg_ParseTuple (args , "O|O" , & operation , & second_argument )) {
495- return NULL ;
501+ goto error ;
496502 }
497503
498504 if (!PyUnicode_Check (operation )) {
499505 PyErr_SetString (PyExc_ValueError , "operation parameter must be str" );
500- return NULL ;
506+ goto error ;
501507 }
502508
503509 parameters_list = PyList_New (0 );
504510 if (!parameters_list ) {
505- return NULL ;
511+ goto error ;
506512 }
507513
508514 if (second_argument == NULL ) {
@@ -742,14 +748,17 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
742748 * ROLLBACK could have happened */
743749 #ifdef SQLITE_VERSION_NUMBER
744750 #if SQLITE_VERSION_NUMBER >= 3002002
745- self -> connection -> inTransaction = !sqlite3_get_autocommit (self -> connection -> db );
751+ if (self -> connection && self -> connection -> db )
752+ self -> connection -> inTransaction = !sqlite3_get_autocommit (self -> connection -> db );
746753 #endif
747754 #endif
748755
749756 Py_XDECREF (parameters );
750757 Py_XDECREF (parameters_iter );
751758 Py_XDECREF (parameters_list );
752759
760+ self -> locked = 0 ;
761+
753762 if (PyErr_Occurred ()) {
754763 self -> rowcount = -1L ;
755764 return NULL ;
0 commit comments