Skip to content

Commit 87eff2a

Browse files
committed
BUG#21498719: Fix conversion of Python bytearray (c-ext)
This patch fixes the error when passing a Python bytearray as a parameter, using the C extension implementation. This is an additional fix to the original patch which only fixed the pure Python implementation.
1 parent c4713e6 commit 87eff2a

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ v8.0.29
1919
- BUG#33729842: Character set 'utf8mb3' support
2020
- BUG#23338623: Add support for Decimal parsing in protocol.py
2121
- BUG#21528553: Fix API inconsistency when using consume_results=True
22+
- BUG#21498719: Fix conversion of Python bytearray (c-ext)
2223
- BUG#20065830: NaN is not supported
2324

2425
v8.0.28

src/mysql_capi.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,11 @@ MySQL_escape_string(MySQL *self, PyObject *value)
16091609
from_size= PyBytes_Size(value);
16101610
from_str= PyBytes_AsString(value);
16111611
}
1612+
else if (PyByteArray_Check(value))
1613+
{
1614+
from_size= PyByteArray_Size(value);
1615+
from_str= PyByteArray_AsString(value);
1616+
}
16121617
else
16131618
{
16141619
PyErr_SetString(PyExc_TypeError, "Argument must be str or bytes");
@@ -2030,7 +2035,8 @@ MySQL_convert_to_mysql(MySQL *self, PyObject *args)
20302035
// All values that need to be quoted
20312036
if (PyUnicode_Check(value)
20322037
|| PyUnicode_Check(value)
2033-
|| PyBytes_Check(value))
2038+
|| PyBytes_Check(value)
2039+
|| PyByteArray_Check(value))
20342040
{
20352041
new_value= MySQL_escape_string(self, value);
20362042
}
@@ -3423,6 +3429,11 @@ MySQLPrepStmt_execute(MySQLPrepStmt *self, PyObject *args)
34233429
pbind->str_value= value;
34243430
mbind->buffer_type= MYSQL_TYPE_STRING;
34253431
}
3432+
else if (PyByteArray_Check(value))
3433+
{
3434+
pbind->str_value= PyBytes_FromObject(value);
3435+
mbind->buffer_type= MYSQL_TYPE_STRING;
3436+
}
34263437
/* DATETIME */
34273438
else if (PyDateTime_Check(value))
34283439
{

tests/cext/test_cext_cursor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
3+
# Copyright (c) 2014, 2022, Oracle and/or its affiliates.
44
#
55
# This program is free software; you can redistribute it and/or modify
66
# it under the terms of the GNU General Public License, version 2.0, as
@@ -692,7 +692,7 @@ class CMySQLCursorPreparedTests(tests.CMySQLCursorTests):
692692
"abc",
693693
u"MySQL 🐬",
694694
"x-large",
695-
"random blob data"
695+
bytearray(b"random blob data")
696696
)
697697

698698
exp = (

0 commit comments

Comments
 (0)