Skip to content

Commit

Permalink
Fix py3 blob (#520)
Browse files Browse the repository at this point in the history
* testing pypy3

* Fix py3 blob encoded as latin1, added test. close web2py/web2py#1861, close #516
  • Loading branch information
ilvalle authored and mdipierro committed Feb 24, 2018
1 parent 53661bb commit 79fa015
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cache: pip
python:
- '2.7'
- 'pypy'
- 'pypy3'
- '3.4'
- '3.5'
- '3.6'
Expand Down Expand Up @@ -35,6 +36,12 @@ matrix:
env: ADAPTER=postgres3
- python: 'pypy'
env: ADAPTER=google
- python: 'pypy3'
env: ADAPTER=google
- python: 'pypy3'
env: ADAPTER=postgres
- python: 'pypy3'
env: ADAPTER=postgres3
- python: '3.4'
env: ADAPTER=google
- python: '3.5'
Expand Down
7 changes: 6 additions & 1 deletion pydal/parsers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ def _boolean(self, value):

@for_type('blob')
def _blob(self, value):
return to_native(b64decode(to_bytes(value)))
decoded = b64decode(to_bytes(value))
try:
decoded = to_native(decoded)
except:
pass
return decoded

@before_parse('reference')
def reference_extras(self, field_type):
Expand Down
18 changes: 15 additions & 3 deletions tests/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import glob
import datetime
import json
import pickle

from pydal._compat import basestring, StringIO, integer_types, xrange
from pydal._compat import basestring, StringIO, integer_types, xrange, BytesIO, to_bytes
from pydal import DAL, Field
from pydal.helpers.classes import SQLALL, OpRow
from pydal.objects import Table, Expression, Row
Expand Down Expand Up @@ -218,13 +219,24 @@ def testUploadField(self):
except ImportError:
pass

def testBlobBytes(self):
#Test blob with latin1 encoded bytes
db = self.connect()
obj = pickle.dumps('0')
db.define_table('tt', Field('aa', 'blob'))
self.assertEqual(db.tt.insert(aa=obj), 1)
self.assertEqual(db().select(db.tt.aa)[0].aa, obj)
self.assertEqual(db.tt[1].aa, obj)
self.assertEqual(BytesIO(to_bytes(db.tt[1].aa)).read(), obj)
db.tt.drop()

def testRun(self):
"""Test all field types and their return values"""
db = self.connect()
for ft in ['string', 'text', 'password', 'upload', 'blob']:
db.define_table('tt', Field('aa', ft, default=''))
self.assertEqual(db.tt.insert(aa='x'), 1)
self.assertEqual(db().select(db.tt.aa)[0].aa, 'x')
self.assertEqual(db.tt.insert(aa='ö'), 1)
self.assertEqual(db().select(db.tt.aa)[0].aa, 'ö')
db.tt.drop()
db.define_table('tt', Field('aa', 'integer', default=1))
self.assertEqual(db.tt.insert(aa=3), 1)
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = {py27,pypy,py33,py34,py35,py36}-{sqlite,mongo,postgresPG8000,postgres3PG8000,mysql}, {py27,py33,py34}-{postgres,postgres3}, py27-{google,mssql}
envlist = {py27,pypy,pypy3,py33,py34,py35,py36}-{sqlite,mongo,postgresPG8000,postgres3PG8000,mysql}, {py27,py33,py34}-{postgres,postgres3}, py27-{google,mssql}

[testenv]
setenv =
Expand All @@ -25,7 +25,7 @@ deps =
mssqln: pypyodbc
coverage: coverage
commands =
py27,pypy,py33,py34,py35,py36: {envpython} -m unittest -v -f tests
py27,pypy,pypy3,py33,py34,py35,py36: {envpython} -m unittest -v -f tests
coverage: coverage erase
coverage: coverage run -m unittest -v -f tests
coverage: coverage combine

1 comment on commit 79fa015

@dartg
Copy link

@dartg dartg commented on 79fa015 Feb 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't except UnicodeDecodeError be used instead?

Please sign in to comment.