Skip to content

Commit 0d0fff7

Browse files
authored
Do not use MYSQL_OPT_RECONNECT as possible. (#664)
MySQL 8.0.33+ shows deprecation warning to stderr. So we avoid using it as possible. In the future, we will deprecate `reconnect` option of the `Connection.ping()`.
1 parent 91c0428 commit 0d0fff7

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/MySQLdb/_mysql.c

+25-15
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
2525
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2626
PERFORMANCE OF THIS SOFTWARE.
2727
*/
28-
28+
#include <stdbool.h>
2929
#include "mysql.h"
3030
#include "mysqld_error.h"
3131

3232
#if MYSQL_VERSION_ID >= 80000
3333
// https://github.com/mysql/mysql-server/commit/eb821c023cedc029ca0b06456dfae365106bee84
34-
#define my_bool _Bool
34+
// my_bool was typedef of char before MySQL 8.0.0.
35+
#define my_bool bool
3536
#endif
3637

3738
#if ((MYSQL_VERSION_ID >= 50555 && MYSQL_VERSION_ID <= 50599) || \
@@ -71,7 +72,8 @@ static PyObject *_mysql_NotSupportedError;
7172
typedef struct {
7273
PyObject_HEAD
7374
MYSQL connection;
74-
int open;
75+
bool open;
76+
bool reconnect;
7577
PyObject *converter;
7678
} _mysql_ConnectionObject;
7779

@@ -443,7 +445,8 @@ _mysql_ConnectionObject_Initialize(
443445
*auth_plugin=NULL;
444446

445447
self->converter = NULL;
446-
self->open = 0;
448+
self->open = false;
449+
self->reconnect = false;
447450

448451
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
449452
"|ssssisOiiisssiOsiiiss:connect",
@@ -487,7 +490,7 @@ _mysql_ConnectionObject_Initialize(
487490
PyErr_SetNone(PyExc_MemoryError);
488491
return -1;
489492
}
490-
self->open = 1;
493+
self->open = true;
491494

492495
if (connect_timeout) {
493496
unsigned int timeout = connect_timeout;
@@ -686,7 +689,7 @@ _mysql_ConnectionObject_close(
686689
Py_BEGIN_ALLOW_THREADS
687690
mysql_close(&(self->connection));
688691
Py_END_ALLOW_THREADS
689-
self->open = 0;
692+
self->open = false;
690693
_mysql_ConnectionObject_clear(self);
691694
Py_RETURN_NONE;
692695
}
@@ -1852,18 +1855,18 @@ _mysql_ResultObject_num_rows(
18521855
}
18531856

18541857
static char _mysql_ConnectionObject_ping__doc__[] =
1855-
"Checks whether or not the connection to the server is\n\
1856-
working. If it has gone down, an automatic reconnection is\n\
1857-
attempted.\n\
1858+
"Checks whether or not the connection to the server is working.\n\
18581859
\n\
18591860
This function can be used by clients that remain idle for a\n\
18601861
long while, to check whether or not the server has closed the\n\
1861-
connection and reconnect if necessary.\n\
1862+
connection.\n\
18621863
\n\
18631864
New in 1.2.2: Accepts an optional reconnect parameter. If True,\n\
18641865
then the client will attempt reconnection. Note that this setting\n\
18651866
is persistent. By default, this is on in MySQL<5.0.3, and off\n\
18661867
thereafter.\n\
1868+
MySQL 8.0.33 deprecated the MYSQL_OPT_RECONNECT option so reconnect\n\
1869+
parameter is also deprecated in mysqlclient 2.2.1.\n\
18671870
\n\
18681871
Non-standard. You should assume that ping() performs an\n\
18691872
implicit rollback; use only when starting a new transaction.\n\
@@ -1875,17 +1878,24 @@ _mysql_ConnectionObject_ping(
18751878
_mysql_ConnectionObject *self,
18761879
PyObject *args)
18771880
{
1878-
int r, reconnect = -1;
1879-
if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL;
1881+
int reconnect = 0;
1882+
if (!PyArg_ParseTuple(args, "|p", &reconnect)) return NULL;
18801883
check_connection(self);
1881-
if (reconnect != -1) {
1884+
if (reconnect != (self->reconnect == true)) {
1885+
// libmysqlclient show warning to stderr when MYSQL_OPT_RECONNECT is used.
1886+
// so we avoid using it as possible for now.
1887+
// TODO: Warn when reconnect is true.
1888+
// MySQL 8.0.33 show warning to stderr already.
1889+
// We will emit Pytohn warning in future.
18821890
my_bool recon = (my_bool)reconnect;
18831891
mysql_options(&self->connection, MYSQL_OPT_RECONNECT, &recon);
1892+
self->reconnect = (bool)reconnect;
18841893
}
1894+
int r;
18851895
Py_BEGIN_ALLOW_THREADS
18861896
r = mysql_ping(&(self->connection));
18871897
Py_END_ALLOW_THREADS
1888-
if (r) return _mysql_Exception(self);
1898+
if (r) return _mysql_Exception(self);
18891899
Py_RETURN_NONE;
18901900
}
18911901

@@ -2165,7 +2175,7 @@ _mysql_ConnectionObject_dealloc(
21652175
PyObject_GC_UnTrack(self);
21662176
if (self->open) {
21672177
mysql_close(&(self->connection));
2168-
self->open = 0;
2178+
self->open = false;
21692179
}
21702180
Py_CLEAR(self->converter);
21712181
MyFree(self);

src/MySQLdb/connections.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ class object, used to create cursors (keyword only)
192192

193193
super().__init__(*args, **kwargs2)
194194
self.cursorclass = cursorclass
195-
self.encoders = {k: v for k, v in conv.items() if type(k) is not int}
195+
self.encoders = {
196+
k: v for k, v in conv.items() if type(k) is not int # noqa: E721
197+
}
196198

197199
self._server_version = tuple(
198200
[numeric_part(n) for n in self.get_server_info().split(".")[:2]]

0 commit comments

Comments
 (0)