Skip to content

Commit c532b8d

Browse files
authored
Reject non-finite decimal.Decimal query parameters (NaN, sNaN, ±Infinity) (#1237)
PyMySQL already rejects non-finite `float` parameters before sending SQL, but equivalent `decimal.Decimal` values were still serialized and sent. This change aligns Decimal behavior with existing float safeguards for unsupported non-finite numeric parameters.
1 parent 113e252 commit c532b8d

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

pymysql/converters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ def escape_struct_time(obj, mapping=None):
135135

136136

137137
def Decimal2Literal(o, d):
138+
if not o.is_finite():
139+
raise ProgrammingError("%s can not be used with MySQL" % str(o).lower())
138140
return format(o, "f")
139141

140142

pymysql/tests/test_converters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import datetime
2+
from decimal import Decimal
23
from unittest import TestCase
34
from pymysql import converters
5+
from pymysql.err import ProgrammingError
46

57

68
__all__ = ["TestConverter"]
@@ -52,3 +54,16 @@ def test_convert_time_with_fsp(self):
5254
expected = datetime.time(23, 6, 20, 511581)
5355
time_obj = converters.convert_time("23:06:20.511581")
5456
self.assertEqual(time_obj, expected)
57+
58+
def test_decimal_special_values(self):
59+
values = (
60+
Decimal("NaN"),
61+
Decimal("sNaN"),
62+
Decimal("Infinity"),
63+
Decimal("-Infinity"),
64+
)
65+
for value in values:
66+
with self.assertRaisesRegex(
67+
ProgrammingError, f"{str(value).lower()} can not be used with MySQL"
68+
):
69+
converters.Decimal2Literal(value, None)

0 commit comments

Comments
 (0)