Skip to content

Commit 221d44c

Browse files
committed
Merge pull request #85 from scraperwiki/fix-bytes
Fix broken strings on Python 2.7
2 parents df4413e + ac076d5 commit 221d44c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

scraperwiki/sql.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ class Blob(bytes):
3535
datetime.datetime: sqlalchemy.types.DateTime,
3636

3737
Blob: sqlalchemy.types.LargeBinary,
38-
bytes: sqlalchemy.types.LargeBinary,
3938
}
4039

40+
if bytes is not str:
41+
# On 2.7, bytes *is* str, so we don't want to overwrite that.
42+
PYTHON_SQLITE_TYPE_MAP[bytes] = sqlalchemy.types.LargeBinary
43+
4144
try:
4245
PYTHON_SQLITE_TYPE_MAP[long] = sqlalchemy.types.BigInteger
4346
except NameError:

tests.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import absolute_import
33

44
import datetime
5+
import json
56
import os
67
import re
78
import shutil
@@ -195,7 +196,7 @@ def test_add_column(self):
195196

196197
script = dedent(u"""
197198
import scraperwiki
198-
scraperwiki.sql.save(['id'], dict(id=1, a="bar\xaa", b="foo\xaa"))
199+
scraperwiki.sql.save(['id'], dict(id=1, a=u"bar\xaa", b=u"foo\xaa"))
199200
""")
200201
with open("/dev/null") as null:
201202
process = Popen([sys.executable, "-c", script],
@@ -218,6 +219,12 @@ def test_save_string(self):
218219
(u'LeTourneau\u1234',)]
219220
)
220221

222+
# Ensure we can round-trip a string and then json encode it.
223+
# https://github.com/scraperwiki/scraperwiki-python/pull/85
224+
scraperwiki.sql.save([], {"test": "teststring"}, table_name="teststring")
225+
data = scraperwiki.sql.select("* FROM teststring")
226+
json.dumps(data)
227+
221228
def test_save_twice(self):
222229
self.save_and_check(
223230
{u"modelNumber\xaa": 293}, u"modelNumbers", [(293,)]

0 commit comments

Comments
 (0)