Skip to content

Commit

Permalink
Add Cursor.cancel
Browse files Browse the repository at this point in the history
Fixes #158
  • Loading branch information
mkleehammer committed Oct 31, 2017
1 parent e218723 commit 5eb9d0d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,33 @@ static PyObject* Cursor_rollback(PyObject* self, PyObject* args)
}


static char cancel_doc[] =
"Cursor.cancel() -> None\n"
"Cancels the processing of the current statement.\n"
"\n"
"Cancels the processing of the current statement.\n"
"\n"
"This calls SQLCancel and is designed to be called from another thread to"
"stop processing of an ongoing query.";

static PyObject* Cursor_cancel(PyObject* self, PyObject* args)
{
UNUSED(args);
Cursor* cur = Cursor_Validate(self, CURSOR_REQUIRE_OPEN | CURSOR_RAISE_ERROR);
if (!cur)
return 0;
SQLRETURN ret;
Py_BEGIN_ALLOW_THREADS
ret = SQLCancel(cur->hstmt);
Py_END_ALLOW_THREADS

if (!SQL_SUCCEEDED(ret))
return RaiseErrorFromHandle(cur->cnxn, "SQLCancel", cur->cnxn->hdbc, cur->hstmt);

Py_RETURN_NONE;
}


static PyObject* Cursor_ignored(PyObject* self, PyObject* args)
{
UNUSED(self, args);
Expand Down Expand Up @@ -2192,9 +2219,10 @@ static PyMethodDef Cursor_methods[] =
{ "skip", (PyCFunction)Cursor_skip, METH_VARARGS, skip_doc },
{ "commit", (PyCFunction)Cursor_commit, METH_NOARGS, commit_doc },
{ "rollback", (PyCFunction)Cursor_rollback, METH_NOARGS, rollback_doc },
{ "__enter__", Cursor_enter, METH_NOARGS, enter_doc },
{ "__exit__", Cursor_exit, METH_VARARGS, exit_doc },
{ 0, 0, 0, 0 }
{"cancel", (PyCFunction)Cursor_cancel, METH_NOARGS, cancel_doc},
{"__enter__", Cursor_enter, METH_NOARGS, enter_doc },
{"__exit__", Cursor_exit, METH_VARARGS, exit_doc },
{0, 0, 0, 0}
};

static char cursor_doc[] =
Expand Down
6 changes: 6 additions & 0 deletions tests3/pgtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ def test_columns(self):
assert row.type_name == 'varchar'
assert row.precision == 3

def test_cancel(self):
# I'm not sure how to reliably cause a hang to cancel, so for now we'll settle with
# making sure SQLCancel is called correctly.
self.cursor.execute("select 1")
self.cursor.cancel()


def main():
from optparse import OptionParser
Expand Down
6 changes: 6 additions & 0 deletions tests3/sqlservertests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,12 @@ def test_columns(self):
assert row.type_name == 'varchar'
assert row.column_size == 3

def test_cancel(self):
# I'm not sure how to reliably cause a hang to cancel, so for now we'll settle with
# making sure SQLCancel is called correctly.
self.cursor.execute("select 1")
self.cursor.cancel()


def main():
from optparse import OptionParser
Expand Down

0 comments on commit 5eb9d0d

Please sign in to comment.