Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0aa9a53

Browse filesBrowse files
committed
Merge pull request PyMySQL#39 from PyMySQL/fix/bit-type
Fix UnicodeError when fetching BIT value on Python 3
2 parents 375508b + 04896fc commit 0aa9a53
Copy full SHA for 0aa9a53

File tree

Expand file treeCollapse file tree

2 files changed

+34
-9
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-9
lines changed

‎_mysql.c

Copy file name to clipboardExpand all lines: _mysql.c
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,8 @@ _mysql_field_to_python(
13721372
field_type == FIELD_TYPE_LONG_BLOB ||
13731373
field_type == FIELD_TYPE_BLOB ||
13741374
field_type == FIELD_TYPE_VAR_STRING ||
1375-
field_type == FIELD_TYPE_STRING) {
1375+
field_type == FIELD_TYPE_STRING ||
1376+
field_type == FIELD_TYPE_BIT) {
13761377
binary = 1;
13771378
}
13781379
#endif

‎tests/test_MySQLdb_capabilities.py

Copy file name to clipboardExpand all lines: tests/test_MySQLdb_capabilities.py
+32-8Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
#!/usr/bin/env python
22
import capabilities
3+
from datetime import timedelta
34
import unittest
45
import MySQLdb
56
import warnings
67

8+
79
warnings.filterwarnings('ignore')
810

11+
912
class test_MySQLdb(capabilities.DatabaseTest):
1013

1114
db_module = MySQLdb
1215
connect_args = ()
1316
connect_kwargs = dict(use_unicode=True, sql_mode="ANSI,STRICT_TRANS_TABLES,TRADITIONAL")
1417
create_table_extra = "ENGINE=INNODB CHARACTER SET UTF8"
1518
leak_test = False
16-
19+
1720
def quote_identifier(self, ident):
1821
return "`%s`" % ident
1922

2023
def test_TIME(self):
21-
from datetime import timedelta
2224
def generator(row,col):
2325
return timedelta(0, row*8000)
2426
self.check_data_integrity(
@@ -27,23 +29,23 @@ def generator(row,col):
2729

2830
def test_TINYINT(self):
2931
# Number data
30-
def generator(row,col):
32+
def generator(row, col):
3133
v = (row*row) % 256
3234
if v > 127:
3335
v = v-256
3436
return v
3537
self.check_data_integrity(
3638
('col1 TINYINT',),
3739
generator)
38-
40+
3941
def test_stored_procedures(self):
4042
db = self.connection
4143
c = self.cursor
4244
self.create_table(('pos INT', 'tree CHAR(20)'))
4345
c.executemany("INSERT INTO %s (pos,tree) VALUES (%%s,%%s)" % self.table,
4446
list(enumerate('ash birch cedar larch pine'.split())))
4547
db.commit()
46-
48+
4749
c.execute("""
4850
CREATE PROCEDURE test_sp(IN t VARCHAR(255))
4951
BEGIN
@@ -57,7 +59,7 @@ def test_stored_procedures(self):
5759
self.assertEquals(len(rows), 1)
5860
self.assertEquals(rows[0][0], 3)
5961
c.nextset()
60-
62+
6163
c.execute("DROP PROCEDURE test_sp")
6264
c.execute('drop table %s' % (self.table))
6365

@@ -71,7 +73,29 @@ def generator(row,col):
7173
self.check_data_integrity(
7274
('col1 char(1)','col2 char(1)'),
7375
generator)
74-
76+
77+
def test_BIT(self):
78+
c = self.cursor
79+
try:
80+
c.execute("""create table test_BIT (
81+
b3 BIT(3),
82+
b7 BIT(10),
83+
b64 BIT(64))""")
84+
85+
one64 = '1'*64
86+
c.execute(
87+
"insert into test_BIT (b3, b7, b64)"
88+
" VALUES (b'011', b'1111111111', b'%s')"
89+
% one64)
90+
91+
c.execute("SELECT b3, b7, b64 FROM test_BIT")
92+
row = c.fetchone()
93+
self.assertEqual(row[0], b'\x03')
94+
self.assertEqual(row[1], b'\x03\xff')
95+
self.assertEqual(row[2], b'\xff'*8)
96+
finally:
97+
c.execute("drop table if exists test_BIT")
98+
7599
def test_bug_2671682(self):
76100
from MySQLdb.constants import ER
77101
try:
@@ -102,7 +126,7 @@ def test_reraise_exception(self):
102126
return
103127
self.fail("Should raise ProgrammingError")
104128

105-
129+
106130
if __name__ == '__main__':
107131
if test_MySQLdb.leak_test:
108132
import gc

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.