Skip to content

Commit 8c8f499

Browse files
authored
Restore isinstance checks for string and byte literals (#814)
mysqlclient 2.3.1 changed `Connection.literal()` to dispatch on exact built-in types. As reported in #813 (comment), this makes a `class Foo(str, enum.Enum)` member serialize as `'Foo.BAR'` instead of its string value `'BAR'`. Restore `isinstance()` checks only for `str`, `bytes`, and `bytearray`, so their subclasses receive the corresponding string/byte handling.
1 parent 79db3b4 commit 8c8f499

2 files changed

Lines changed: 68 additions & 8 deletions

File tree

‎src/MySQLdb/connections.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,13 @@ def literal(self, o):
324324
applications.
325325
"""
326326
t = type(o)
327-
if t is str:
327+
if isinstance(o, str):
328328
s = self.string_literal(o.encode(self.encoding))
329-
elif t in (bytes, bytearray):
329+
elif isinstance(o, (bytes, bytearray)):
330330
s = self._bytes_literal(o)
331-
elif t in (tuple, list):
331+
elif t is tuple or t is list:
332332
s = self._tuple_literal(o)
333-
elif t in (set, frozenset):
333+
elif t is set or t is frozenset:
334334
s = self._set_literal(o)
335335
else:
336336
s = self.escape(o, self.encoders)

‎tests/test_connection.py‎

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import enum
12
import threading
23
import time
34

45
import pytest
56
from configdb import connection_factory, connection_kwargs
67

7-
from MySQLdb.connections import Connection
88
from MySQLdb._exceptions import ProgrammingError
9+
from MySQLdb.connections import Connection
910
from MySQLdb.constants import CLIENT
1011

1112

@@ -18,6 +19,67 @@ def conn():
1819
connection.close()
1920

2021

22+
@pytest.mark.parametrize("sql_mode", ["", "NO_BACKSLASH_ESCAPES"])
23+
def test_literal_str_enum(conn, sql_mode):
24+
class Foo(str, enum.Enum):
25+
BAR = "BAR"
26+
BAZ = "BAZ"
27+
28+
conn.set_sql_mode(sql_mode)
29+
assert conn.literal(Foo.BAR) == b"'BAR'"
30+
assert conn.literal((Foo.BAR, Foo.BAZ)) == b"('BAR','BAZ')"
31+
cursor = conn.cursor()
32+
cursor.execute("SELECT %s", (Foo.BAR,))
33+
assert cursor.fetchone() == ("BAR",)
34+
35+
36+
@pytest.mark.parametrize("value", ["BAR", b"BAR"])
37+
@pytest.mark.parametrize("binary_prefix", [False, True])
38+
def test_literal_string_subclass(value, binary_prefix):
39+
class Subclass(type(value)):
40+
pass
41+
42+
with connection_factory(binary_prefix=binary_prefix) as conn:
43+
assert conn.literal(Subclass(value)) == conn.literal(value)
44+
45+
46+
@pytest.mark.parametrize("binary_prefix", [False, True])
47+
def test_literal_bytearray_subclass(binary_prefix):
48+
class Subclass(bytearray):
49+
pass
50+
51+
value = Subclass(b"BAR")
52+
with connection_factory(binary_prefix=binary_prefix) as conn:
53+
cursor = conn.cursor()
54+
cursor.execute("SELECT %s", (value,))
55+
expected = str(value).encode(conn.encoding) if binary_prefix else str(value)
56+
assert cursor.fetchone() == (expected,)
57+
58+
59+
@pytest.mark.parametrize("container_type", [tuple, list, set, frozenset])
60+
def test_literal_container_subclass_converter(conn, container_type):
61+
class Subclass(container_type):
62+
pass
63+
64+
conn.encoders[Subclass] = lambda value, mapping: b"'custom'"
65+
cursor = conn.cursor()
66+
cursor.execute("SELECT %s", (Subclass(["BAR"]),))
67+
assert cursor.fetchone() == ("custom",)
68+
69+
70+
@pytest.mark.parametrize("set_type", [set, frozenset])
71+
@pytest.mark.parametrize("sql_mode", ["", "NO_BACKSLASH_ESCAPES"])
72+
@pytest.mark.parametrize("charset", ["utf8mb4", "latin1"])
73+
def test_literal_set_escaping(set_type, sql_mode, charset):
74+
value = "é' OR 1=1 -- \\"
75+
with connection_factory(charset=charset, sql_mode=sql_mode) as conn:
76+
cursor = conn.cursor()
77+
cursor.execute("SELECT %s", (set_type([value]),))
78+
assert cursor.fetchone() == (value,)
79+
with pytest.raises(TypeError, match="set elements must be strings"):
80+
conn.literal(set_type([1]))
81+
82+
2183
def test_multi_statements_default_true(conn):
2284
cursor = conn.cursor()
2385

@@ -63,9 +125,7 @@ class MultiConnection(Connection):
63125
with MultiConnection(**connection_kwargs({})) as conn:
64126
assert conn.executemany_fallback == "multi"
65127

66-
with MultiConnection(
67-
**connection_kwargs({"executemany_fallback": "loop"})
68-
) as conn:
128+
with MultiConnection(**connection_kwargs({"executemany_fallback": "loop"})) as conn:
69129
assert conn.executemany_fallback == "loop"
70130

71131

0 commit comments

Comments
 (0)