Skip to content

Commit 6ba6cd7

Browse files
committed
BUG#35349093: Compression doesn't work with C extension API
Compression is not enabled when using the C API implementation directly with the connection option `compress=True`. This patch fixes this issue by adding CLIENT_COMPRESS to client flags. Thank you for your contribution. Change-Id: If1fdc1b9316e9bf55416d19b594c8671d3d4d082
1 parent d679d87 commit 6ba6cd7

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ v8.1.0
1313

1414
- WL#15630: Remove Python 3.7 support
1515
- WL#15591: Improve the network module
16+
- BUG#35349093: Compression doesn't work with C extension API
1617
- BUG#35338384: PIP installs incompatible Connector/Python packages
1718
- BUG#35318413: Fix charset mapping for MySQL 8.1.0
1819
- BUG#35212199: Check for identifier quotes in the database name

lib/mysql/connector/connection_cext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def _open_connection(self) -> None:
243243
"port": self._port,
244244
"client_flags": self._client_flags,
245245
"unix_socket": self._unix_socket,
246-
"compress": self.isset_client_flag(ClientFlag.COMPRESS),
246+
"compress": self._compress,
247247
"ssl_disabled": True,
248248
"conn_attrs": self._conn_attrs,
249249
"local_infile": self._allow_local_infile,

src/mysql_capi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,10 @@ MySQL_connect(MySQL *self, PyObject *args, PyObject *kwds)
12061206
client_flags = client_flags & ~CLIENT_LOCAL_FILES;
12071207
}
12081208

1209+
if (compress != NULL && (PyBool_Check(compress) && compress == Py_True)) {
1210+
client_flags |= CLIENT_COMPRESS;
1211+
}
1212+
12091213
#ifdef MS_WINDOWS
12101214
if (NULL == host) {
12111215
// if host is NULL, we try with named pipe

tests/test_bugs.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@
8383
from . import check_tls_versions_support
8484

8585
try:
86+
from _mysql_connector import MySQL
87+
8688
from mysql.connector.connection_cext import CMySQLConnection, MySQLInterfaceError
8789
except ImportError:
8890
# Test without C Extension
8991
CMySQLConnection = None
9092
MySQLInterfaceError = None
93+
MySQL = None
9194

9295
ERR_NO_CEXT = "C Extension not available"
9396

@@ -7523,3 +7526,64 @@ def test_connect_database_name(self):
75237526
config["database"] = self.database
75247527
with self.cnx.__class__(**config) as cnx:
75257528
self.assertTrue(cnx.is_connected())
7529+
7530+
7531+
class BugOra35349093(tests.MySQLConnectorTests):
7532+
"""BUG#35349093: Compression doesn't work with C extension API
7533+
7534+
Compression is not enabled when using the C API implementation directly
7535+
with the connection option `compress=True`.
7536+
7537+
This patch fixes this issue by adding CLIENT_COMPRESS to client flags.
7538+
7539+
Thank you for your contribution.
7540+
"""
7541+
7542+
def _get_compression_status(self):
7543+
with self.cnx.cursor() as cur:
7544+
cur.execute("SHOW SESSION STATUS LIKE 'Compression'")
7545+
return cur.fetchone()
7546+
7547+
@staticmethod
7548+
def _test_compression_status_cext(compress):
7549+
config = tests.get_mysql_config()
7550+
del config["connection_timeout"]
7551+
config["compress"] = compress
7552+
cnx = MySQL()
7553+
cnx.connect(**config)
7554+
cnx.query("SHOW SESSION STATUS LIKE 'Compression'")
7555+
res = cnx.fetch_row()
7556+
cnx.close()
7557+
return res
7558+
7559+
@foreach_cnx()
7560+
def test_set_compress_default(self):
7561+
"""Test default `compress` option."""
7562+
res = self._get_compression_status()
7563+
self.assertEqual(res, ("Compression", "OFF"))
7564+
7565+
@cnx_config(compress=False)
7566+
@foreach_cnx()
7567+
def test_set_compress_false(self):
7568+
"""Test setting `compress=False`."""
7569+
res = self._get_compression_status()
7570+
self.assertEqual(res, ("Compression", "OFF"))
7571+
7572+
@cnx_config(compress=True)
7573+
@foreach_cnx()
7574+
def test_set_compress_true(self):
7575+
"""Test setting `compress=True`."""
7576+
res = self._get_compression_status()
7577+
self.assertEqual(res, ("Compression", "ON"))
7578+
7579+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
7580+
def test_set_compress_cext_true(self):
7581+
"""Test setting `compress=True` in the C extension directly."""
7582+
res = self._test_compression_status_cext(True)
7583+
self.assertEqual(res, ("Compression", "ON"))
7584+
7585+
@unittest.skipIf(not CMySQLConnection, ERR_NO_CEXT)
7586+
def test_set_compress_cext_false(self):
7587+
"""Test setting `compress=False` in the C extension directly."""
7588+
res = self._test_compression_status_cext(False)
7589+
self.assertEqual(res, ("Compression", "OFF"))

0 commit comments

Comments
 (0)