Skip to content

Commit 18b0bcb

Browse files
authored
use better format for float (#806)
1 parent f8c31d4 commit 18b0bcb

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

pymysql/converters.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ def escape_int(value, mapping=None):
5454
return str(value)
5555

5656
def escape_float(value, mapping=None):
57-
return ('%.15g' % value)
57+
s = repr(value)
58+
if s in ('inf', 'nan'):
59+
raise ProgrammingError("%s can not be used with MySQL" % s)
60+
if 'e' not in s:
61+
s += 'e0'
62+
return s
5863

5964
_escape_table = [unichr(x) for x in range(128)]
6065
_escape_table[0] = u'\\0'

pymysql/tests/thirdparty/test_MySQLdb/test_MySQLdb_capabilities.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_literal_int(self):
9090
self.assertTrue("2" == self.connection.literal(2))
9191

9292
def test_literal_float(self):
93-
self.assertTrue("3.1415" == self.connection.literal(3.1415))
93+
self.assertEqual("3.1415e0", self.connection.literal(3.1415))
9494

9595
def test_literal_string(self):
9696
self.assertTrue("'foo'" == self.connection.literal("foo"))

0 commit comments

Comments
 (0)