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

Fixed #27848 -- Prevented crash when attaching a .eml file to a message #8281

Merged
merged 1 commit into from Apr 1, 2017

Conversation

claudep
Copy link
Member

@claudep claudep commented Apr 1, 2017

Thanks Sébastien Ramage for the report.

@timgraham
Copy link
Member

Is it worth a comment explaining the usage of force_text() as opposed to str() (the test seems to pass either way)?

@claudep claudep merged commit 9a9e228 into django:master Apr 1, 2017
@claudep claudep deleted the 27848 branch April 1, 2017 14:20
@claudep
Copy link
Member Author

claudep commented Apr 1, 2017

Oh, sorry, I didn't check your comment before the commit :-(
Generally, force_text is a bit more resilient when we are not sure about character encodings of the source. Agreed that we might add some tests. However I didn't investigate about accepted encodings for .eml files.

@timgraham
Copy link
Member

if isinstance(s, bytes):
    s = str(s, encoding, errors)
else:
    s = str(s)

It looks to me like force_text() (without encoding/errors args) behaves the same as str(), considering the the default values for encoding and errors is the same for force_text() and str(). Is this analysis correct? I'm just trying to understand a bit better.

@claudep
Copy link
Member Author

claudep commented Apr 1, 2017

Not really:

In [1]: from django.utils.encoding import force_text

In [2]: encoded = "café".encode()

In [3]: str(encoded)
Out[3]: "b'caf\\xc3\\xa9'"

In [4]: force_text(encoded)
Out[4]: 'café'

@timgraham
Copy link
Member

Thanks, I didn't read https://docs.python.org/3/library/stdtypes.html#str closely enough.

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