Skip to content

Commit 4db801f

Browse files
committed
BUG31528783: Fix number with ZEROFILL not handled by C extension
An exception is raised when a SELECT statement fetch an INT ZEROFILL data type that will be left 0-padded. This patch fixes this issue by checking if the ZEROFILL_FLAG flag is set and passing the base 10 in the PyLong_FromString call used for the conversion. A test was added for regression.
1 parent bab32dc commit 4db801f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ v8.0.26
1919
- BUG#32740486: Fix typo in docstring
2020
- BUG#32623479: The X DevAPI returns str for binary types values
2121
- BUG#32585611: Fix broken links in X DevAPI reference documentation search
22+
- BUG#31528783: Fix number with ZEROFILL not handled by C extension
2223

2324
v8.0.25
2425
=======

src/mysql_capi.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,13 @@ MySQL_fetch_row(MySQL *self)
26692669
field_type == MYSQL_TYPE_LONGLONG ||
26702670
field_type == MYSQL_TYPE_INT24 ||
26712671
field_type == MYSQL_TYPE_YEAR) {
2672-
PyTuple_SET_ITEM(result_row, i, PyLong_FromString(row[i], NULL, 0));
2672+
if (field_flags && ZEROFILL_FLAG) {
2673+
PyTuple_SET_ITEM(result_row, i, PyLong_FromString(row[i], NULL, 10));
2674+
}
2675+
else
2676+
{
2677+
PyTuple_SET_ITEM(result_row, i, PyLong_FromString(row[i], NULL, 0));
2678+
}
26732679
}
26742680
else if (field_type == MYSQL_TYPE_DATETIME ||
26752681
field_type == MYSQL_TYPE_TIMESTAMP)

tests/test_bugs.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6265,3 +6265,37 @@ def test_prepared_statement_parameters(self):
62656265
rows = cur.fetchall()
62666266
self.assertEqual(2, len(rows))
62676267
self.cnx.cmd_query(f"DROP TABLE IF EXISTS {self.table_name}")
6268+
6269+
6270+
class BugOra31528783(tests.MySQLConnectorTests):
6271+
"""BUG#31528783: ZEROFILL NOT HANDLED BY THE PYTHON CONNECTOR."""
6272+
6273+
table_name = "BugOra31528783"
6274+
6275+
@foreach_cnx()
6276+
def test_number_zerofill(self):
6277+
self.cnx.cmd_query(f"DROP TABLE IF EXISTS {self.table_name}")
6278+
self.cnx.cmd_query(
6279+
f"""
6280+
CREATE TABLE {self.table_name} (
6281+
value INT(4) UNSIGNED ZEROFILL NOT NULL,
6282+
PRIMARY KEY(value)
6283+
)
6284+
"""
6285+
)
6286+
with self.cnx.cursor() as cur:
6287+
values = [1, 10, 100, 1000]
6288+
# Insert data
6289+
for value in values:
6290+
cur.execute(
6291+
f"INSERT INTO {self.table_name} (value) VALUES ({value})"
6292+
)
6293+
6294+
# Test values
6295+
for value in values:
6296+
cur.execute(
6297+
f"SELECT value FROM {self.table_name} WHERE value={value}"
6298+
)
6299+
res = cur.fetchone()
6300+
self.assertEqual(res[0], value)
6301+
self.cnx.cmd_query(f"DROP TABLE IF EXISTS {self.table_name}")

0 commit comments

Comments
 (0)