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

fix String encoding #235

Merged
merged 4 commits into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions colander/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,8 @@ def serialize(self, node, appstruct):
result = text_type(appstruct)
else:
result = text_type(appstruct)
if self.encoding:
result = result.encode(self.encoding)
return result
except Exception as e:
raise Invalid(node,
Expand Down
7 changes: 7 additions & 0 deletions colander/tests/test_colander.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,6 +1557,13 @@ def test_serialize_string_with_high_unresolveable_high_order_chars(self):
e = invalid_exc(typ.serialize, node, not_utf8)
self.assertTrue('cannot be serialized' in e.msg)

def test_serialize_encoding_with_non_string_type(self):
utf8 = '123'.encode('utf-8')
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, this one should start as text and then encode to bytes (Python2 lets us get away with being sloppy, but that doesn't mean we shouldn't be explicit).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I develop everything in Python 3 and then deal with Python 2 only when I need backwards compatibility. I know this is already doing what your describing in Py3, but I don't understand how it's not doing this in Py2. In Py2, should this be u'123'.encode('utf-8') to be explicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you saying this should be utf8 = text_type('123').encode('utf-8')?

Copy link
Member

Choose a reason for hiding this comment

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

That would be the better spelling, yes.

Copy link
Member

Choose a reason for hiding this comment

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

Or just b'123', in fact (ASCII and UTF-8 are identical encodings for 7-bit values).

node = DummySchemaNode(None)
typ = self._makeOne('utf-8')
result = typ.serialize(node, 123)
self.assertEqual(result, utf8)

class TestInteger(unittest.TestCase):
def _makeOne(self):
from colander import Integer
Expand Down