Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal escape is not right in python 2.x #222

Closed
limodou opened this issue Mar 17, 2014 · 0 comments
Closed

Decimal escape is not right in python 2.x #222

limodou opened this issue Mar 17, 2014 · 0 comments

Comments

@limodou
Copy link

limodou commented Mar 17, 2014

Today I found the similary issue #217 but this time is Decimal datatype, because after #221 all unicode will be converted to string, but Decimal will be converted to unicode, for example:

>>> from pymysql.converters import escape_item
>>> from decimal import Decimal
>>> escape_item(Decimal('0.4'), 'utf8')
u'0.4'

So this will cause encode error with other non-ascii strings.

And I tried to fix it, the code is:

diff --git a/pymysql/converters.py b/pymysql/converters.py
index 4f22762..daa00f7 100644
--- a/pymysql/converters.py
+++ b/pymysql/converters.py
@@ -58,6 +58,9 @@ def escape_int(value):
 def escape_float(value):
     return ('%.15g' % value)

+def escape_decimal(value):
+    return str(value)
+
 def escape_string(value):
     return ("%s" % (ESCAPE_REGEX.sub(
             lambda match: ESCAPE_MAP.get(match.group(0)), value),))
@@ -292,7 +295,7 @@ encoders = {
         datetime.timedelta : escape_timedelta,
         datetime.time : escape_time,
         time.struct_time : escape_struct_time,
-        Decimal: text_type,
+        Decimal: escape_decimal,
         }
methane added a commit that referenced this issue Mar 22, 2014
czerwingithub pushed a commit to scalyr/PyMySQL that referenced this issue Sep 20, 2014
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant