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

fix String encoding #235

merged 4 commits into from
Jun 23, 2015

Conversation

tisdall
Copy link
Contributor

@tisdall tisdall commented Jun 16, 2015

fixes #233

if self.encoding:
result = text_type(appstruct).encode(self.encoding)
else:
result = text_type(appstruct)
Copy link
Member

Choose a reason for hiding this comment

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

I prefer that the result = text_type(appstruct) to be outside the if, with no else. E.g.:

    result = text_type(appstruct)
    if self.encoding:
        result = result.encode(self.encoding)
    return result

@tisdall
Copy link
Contributor Author

tisdall commented Jun 22, 2015

@tseaver - updated as requested...

@@ -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).

@tisdall
Copy link
Contributor Author

tisdall commented Jun 23, 2015

@tseaver - I went with using text_type('123').encode('utf-8') so it's totally explicit that the result is a utf-8 encoded string.

@tisdall
Copy link
Contributor Author

tisdall commented Jun 23, 2015

@tseaver - should I add a note in the CHANGES.rst? Is there anything else I need to change to complete this?

@tseaver
Copy link
Member

tseaver commented Jun 23, 2015

A note in CHANGES.rst would be great, thanks!

@tisdall
Copy link
Contributor Author

tisdall commented Jun 23, 2015

done

tseaver added a commit that referenced this pull request Jun 23, 2015
@tseaver tseaver merged commit ccc31c1 into Pylons:master Jun 23, 2015
@tseaver
Copy link
Member

tseaver commented Jun 23, 2015

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String with encoding
2 participants