Skip to content

Commit 7b63daa

Browse files
committed
CONPY-188:
When a connection or cursor was closed, an exception will be returned if a method or property of closed object will be called.
1 parent c7b27ed commit 7b63daa

File tree

4 files changed

+39
-60
lines changed

4 files changed

+39
-60
lines changed

include/mariadb_python.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ if ((obj)->thread_state)\
478478
#define MARIADB_CHECK_CONNECTION(connection, ret)\
479479
if (!(connection) || !(connection)->mysql)\
480480
{\
481-
mariadb_throw_exception(NULL, Mariadb_InterfaceError, 0, \
481+
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0, \
482482
"Invalid connection or not connected");\
483483
return (ret);\
484484
}
@@ -498,12 +498,13 @@ if ((obj)->thread_state)\
498498
(a)= NULL;\
499499
}
500500

501-
#define MARIADB_CHECK_STMT(cursor)\
502-
if (!cursor->connection->mysql || cursor->closed)\
501+
#define MARIADB_CHECK_STMT(cursor, retval)\
502+
if (!(cursor)->connection->mysql || (cursor)->closed)\
503503
{\
504504
(cursor)->closed= 1;\
505-
mariadb_throw_exception(cursor->stmt, Mariadb_ProgrammingError, 1,\
505+
mariadb_throw_exception((cursor)->stmt, Mariadb_ProgrammingError, 1,\
506506
"Invalid cursor or not connected");\
507+
return (retval);\
507508
}
508509

509510
#define pooling_keywords "pool_name", "pool_size", "reset_session", "idle_timeout", "acquire_timeout"

mariadb/mariadb_connection.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ static PyObject *MrdbConnection_cursor(MrdbConnection *self,
674674
PyObject *cursor= NULL;
675675
PyObject *conn = NULL;
676676

677+
MARIADB_CHECK_CONNECTION(self, NULL);
678+
677679
conn= Py_BuildValue("(O)", self);
678680
cursor= PyObject_Call((PyObject *)&MrdbCursor_Type, conn, kwargs);
679681
Py_DECREF(conn);
@@ -792,6 +794,8 @@ MrdbConnection_tpc_begin(MrdbConnection *self, PyObject *args)
792794
char stmt[192];
793795
int rc= 0;
794796

797+
MARIADB_CHECK_CONNECTION(self, NULL);
798+
795799
if (!PyArg_ParseTuple(args, "(iss)", &format_id,
796800
&transaction_id,
797801
&branch_qualifier))
@@ -1143,6 +1147,8 @@ static PyObject *MrdbConnection_getreconnect(MrdbConnection *self,
11431147
{
11441148
uint8_t reconnect= 0;
11451149

1150+
MARIADB_CHECK_CONNECTION(self, NULL);
1151+
11461152
if (self->mysql) {
11471153
mysql_get_option(self->mysql, MYSQL_OPT_RECONNECT, &reconnect);
11481154
}
@@ -1162,9 +1168,7 @@ static int MrdbConnection_setreconnect(MrdbConnection *self,
11621168
{
11631169
uint8_t reconnect;
11641170

1165-
if (!self->mysql) {
1166-
return 0;
1167-
}
1171+
MARIADB_CHECK_CONNECTION(self, -1);
11681172

11691173
if (!args || !CHECK_TYPE(args, &PyBool_Type)) {
11701174
PyErr_SetString(PyExc_TypeError, "Argument must be boolean");
@@ -1406,6 +1410,7 @@ MrdbConnection_exit(MrdbConnection *self, PyObject *args __attribute__((unused))
14061410
static PyObject *MrdbConnection_get_server_version(MrdbConnection *self)
14071411
{
14081412
MARIADB_CHECK_CONNECTION(self, NULL);
1413+
14091414
Py_INCREF(self->server_version_info);
14101415
return self->server_version_info;
14111416
}

mariadb/mariadb_cursor.c

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,7 @@ PyObject *MrdbCursor_execute(MrdbCursor *self,
677677
static char *key_words[]= {"", "", "buffered", NULL};
678678
char errmsg[128];
679679

680-
MARIADB_CHECK_STMT(self);
681-
if (PyErr_Occurred())
682-
return NULL;
680+
MARIADB_CHECK_STMT(self, NULL);
683681

684682
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
685683
"s#|Ob", key_words, &statement, &statement_len, &Data, &is_buffered))
@@ -871,8 +869,7 @@ PyObject *MrdbCursor_execute(MrdbCursor *self,
871869
/* {{{ MrdbCursor_fieldcount() */
872870
PyObject *MrdbCursor_fieldcount(MrdbCursor *self)
873871
{
874-
if (PyErr_Occurred())
875-
return NULL;
872+
MARIADB_CHECK_STMT(self, NULL);
876873

877874
return PyLong_FromLong((long)self->field_count);
878875
}
@@ -890,9 +887,7 @@ PyObject *MrdbCursor_description(MrdbCursor *self)
890887
PyObject *obj= NULL;
891888
unsigned int field_count= self->field_count;
892889

893-
if (PyErr_Occurred())
894-
return NULL;
895-
890+
MARIADB_CHECK_STMT(self, NULL);
896891

897892
if (self->fields && field_count)
898893
{
@@ -996,12 +991,7 @@ MrdbCursor_fetchone(MrdbCursor *self)
996991
uint32_t i;
997992
unsigned int field_count= self->field_count;
998993

999-
if (self->cursor_type == CURSOR_TYPE_READ_ONLY)
1000-
MARIADB_CHECK_STMT(self);
1001-
if (PyErr_Occurred())
1002-
{
1003-
return NULL;
1004-
}
994+
MARIADB_CHECK_STMT(self, NULL);
1005995

1006996
if (!field_count)
1007997
{
@@ -1042,11 +1032,7 @@ MrdbCursor_scroll(MrdbCursor *self,
10421032
const char *scroll_modes[]= {"relative", "absolute", NULL};
10431033

10441034

1045-
MARIADB_CHECK_STMT(self);
1046-
if (PyErr_Occurred())
1047-
{
1048-
return NULL;
1049-
}
1035+
MARIADB_CHECK_STMT(self, NULL);
10501036

10511037
if (!self->field_count)
10521038
{
@@ -1133,11 +1119,7 @@ MrdbCursor_fetchmany(MrdbCursor *self,
11331119
static char *kw_list[]= {"size", NULL};
11341120
unsigned int field_count= self->field_count;
11351121

1136-
MARIADB_CHECK_STMT(self);
1137-
if (PyErr_Occurred())
1138-
{
1139-
return NULL;
1140-
}
1122+
MARIADB_CHECK_STMT(self, NULL);
11411123

11421124
if (!field_count)
11431125
{
@@ -1211,11 +1193,8 @@ MrdbCursor_fetchall(MrdbCursor *self)
12111193
{
12121194
PyObject *List;
12131195
unsigned int field_count= self->field_count;
1214-
MARIADB_CHECK_STMT(self);
1215-
if (PyErr_Occurred())
1216-
{
1217-
return NULL;
1218-
}
1196+
1197+
MARIADB_CHECK_STMT(self, NULL);
12191198

12201199
if (!field_count)
12211200
{
@@ -1332,12 +1311,7 @@ MrdbCursor_executemany(MrdbCursor *self,
13321311
uint8_t do_prepare= 1;
13331312
char errmsg[128];
13341313

1335-
MARIADB_CHECK_STMT(self);
1336-
1337-
if (PyErr_Occurred())
1338-
{
1339-
return NULL;
1340-
}
1314+
MARIADB_CHECK_STMT(self, NULL);
13411315

13421316
self->data= NULL;
13431317

@@ -1447,13 +1421,8 @@ static PyObject *
14471421
MrdbCursor_nextset(MrdbCursor *self)
14481422
{
14491423
int rc;
1450-
MARIADB_CHECK_STMT(self);
1424+
MARIADB_CHECK_STMT(self, NULL);
14511425

1452-
if (PyErr_Occurred())
1453-
{
1454-
return NULL;
1455-
}
1456-
/* hmmm */
14571426
if (!self->field_count)
14581427
{
14591428
mariadb_throw_exception(NULL, Mariadb_ProgrammingError, 0,
@@ -1519,14 +1488,15 @@ Mariadb_row_number(MrdbCursor *self)
15191488
static PyObject *
15201489
MrdbCursor_warnings(MrdbCursor *self)
15211490
{
1522-
MARIADB_CHECK_STMT(self);
1491+
MARIADB_CHECK_STMT(self, NULL);
15231492

15241493
return PyLong_FromLong((long)CURSOR_WARNING_COUNT(self));
15251494
}
15261495

15271496
static PyObject *
15281497
MrdbCursor_getbuffered(MrdbCursor *self)
15291498
{
1499+
MARIADB_CHECK_STMT(self, NULL);
15301500
if (self->is_buffered)
15311501
{
15321502
Py_RETURN_TRUE;
@@ -1537,6 +1507,7 @@ MrdbCursor_getbuffered(MrdbCursor *self)
15371507
static int
15381508
MrdbCursor_setbuffered(MrdbCursor *self, PyObject *arg)
15391509
{
1510+
MARIADB_CHECK_STMT(self, -1);
15401511
if (!arg || !CHECK_TYPE(arg, &PyBool_Type))
15411512
{
15421513
PyErr_SetString(PyExc_TypeError, "Argument must be boolean");
@@ -1550,6 +1521,7 @@ MrdbCursor_setbuffered(MrdbCursor *self, PyObject *arg)
15501521
static PyObject *
15511522
MrdbCursor_lastrowid(MrdbCursor *self)
15521523
{
1524+
MARIADB_CHECK_STMT(self, NULL);
15531525
if (!self->lastrow_id)
15541526
{
15551527
Py_INCREF(Py_None);
@@ -1563,7 +1535,7 @@ MrdbCursor_lastrowid(MrdbCursor *self)
15631535
static PyObject *
15641536
MrdbCursor_iter(PyObject *self)
15651537
{
1566-
MARIADB_CHECK_STMT(((MrdbCursor *)self));
1538+
MARIADB_CHECK_STMT((MrdbCursor *)self, NULL);
15671539
Py_INCREF(self);
15681540
return self;
15691541
}
@@ -1573,6 +1545,8 @@ MrdbCursor_iternext(PyObject *self)
15731545
{
15741546
PyObject *res;
15751547

1548+
MARIADB_CHECK_STMT((MrdbCursor *)self, NULL);
1549+
15761550
res= MrdbCursor_fetchone((MrdbCursor *)self);
15771551

15781552
if (res && res == Py_None)
@@ -1594,15 +1568,14 @@ static PyObject
15941568
static PyObject *
15951569
MrdbCursor_sp_outparams(MrdbCursor *self)
15961570
{
1597-
if (!self->closed && self->stmt &&
1598-
self->stmt->mysql)
1571+
uint32_t server_status;
1572+
1573+
MARIADB_CHECK_STMT(self, NULL);
1574+
1575+
mariadb_get_infov(self->stmt->mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
1576+
if (server_status & SERVER_PS_OUT_PARAMS)
15991577
{
1600-
uint32_t server_status;
1601-
mariadb_get_infov(self->stmt->mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
1602-
if (server_status & SERVER_PS_OUT_PARAMS)
1603-
{
1604-
Py_RETURN_TRUE;
1605-
}
1578+
Py_RETURN_TRUE;
16061579
}
16071580
Py_RETURN_FALSE;
16081581
}
@@ -1619,7 +1592,7 @@ MrdbCursor_callproc(MrdbCursor *self, PyObject *args)
16191592
PyObject *new_args= NULL;
16201593
PyObject *rc= NULL;
16211594

1622-
MARIADB_CHECK_STMT(((MrdbCursor *)self));
1595+
MARIADB_CHECK_STMT(self, NULL);
16231596

16241597
if (!PyArg_ParseTuple(args, "s#|O", &sp, &sp_len, &data))
16251598
return NULL;

testing/test/integration/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def test_conpy155(self):
201201
c1.close()
202202
try:
203203
version= c1.get_server_version()
204-
except mariadb.InterfaceError:
204+
except mariadb.ProgrammingError:
205205
pass
206206

207207
def test_conpy175(self):

0 commit comments

Comments
 (0)