Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pymysql/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ def convert_date(obj):
return obj


def convert_decimal(obj):
"""Returns a DECIMAL column value as a Decimal object:

>>> convert_decimal('9.99')
Decimal('9.99')

Values are bytes when the connection uses ``use_unicode=False``:

>>> convert_decimal(b'-0.50')
Decimal('-0.50')
"""
if isinstance(obj, (bytes, bytearray)):
obj = obj.decode("ascii")
return Decimal(obj)


def through(x):
return x

Expand Down Expand Up @@ -362,8 +378,8 @@ def through(x):
FIELD_TYPE.STRING: through,
FIELD_TYPE.VAR_STRING: through,
FIELD_TYPE.VARCHAR: through,
FIELD_TYPE.DECIMAL: Decimal,
FIELD_TYPE.NEWDECIMAL: Decimal,
FIELD_TYPE.DECIMAL: convert_decimal,
FIELD_TYPE.NEWDECIMAL: convert_decimal,
}


Expand Down
8 changes: 8 additions & 0 deletions pymysql/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
import time
from decimal import Decimal

import pytest

Expand Down Expand Up @@ -201,6 +202,13 @@ def test_datetime_microseconds(self):
finally:
c.execute("drop table test_datetime")

def test_decimal_without_unicode(self):
"""test DECIMAL conversion with use_unicode=False"""
conn = self.connect(use_unicode=False)
c = conn.cursor()
c.execute("select cast(9.99 as decimal(10, 2)), sum(i) from (select 2 as i) t")
self.assertEqual((Decimal("9.99"), Decimal(2)), c.fetchone())


class TestCursor(base.PyMySQLTestCase):
# this test case does not work quite right yet, however,
Expand Down