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

#963 : Convert subject and body to unicode before sending mail #964

Merged
merged 1 commit into from May 14, 2015

Conversation

matclab
Copy link

@matclab matclab commented May 9, 2015

Patch for issue #963.

Not doing this was raising an exception :

Traceback (most recent call last):
File /base/data/home/apps/esacred-bonus-88417/1.384178859090314065/gluon/restricted.py, line 227, in restricted
exec ccode in environment
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 127, in
File /base/data/home/apps/esacred-bonus-88417/1.384178859090314065/gluon/globals.py, line 393, in
self._caller = lambda f: f()
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 34, in user
return dict(form=auth())
File /base/data/home/apps/esacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 1595, in call
return getattr(self, args[0])()
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 3272, in request_reset_password
if self.email_reset_password(user):
File /base/data/home/apps/esacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 3296, in email_reset_password
message=self.messages.reset_password % d):
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 798, in send
subject=subject, body=text, **xcc)
File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 402, in send_mail
message.send(make_sync_call=make_sync_call)
File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1108, in send
message = self.ToProto()
File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1350, in ToProto
message = super(EmailMessage, self).ToProto()
File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1046, in ToProto
message.set_subject(_to_str(self.subject))
File cpp_message.pyx, line 124, in cpp_message.SetScalarAccessors.Setter (third_party/apphosting/python/protobuf/proto1/cpp_message.cc:2229)
TypeError: <class 'gluon.languages.lazyT'> has type <class 'gluon.languages.lazyT'>, but expected one of: str, unicode

Not doing this was raising an exception :

Traceback (most recent call last):
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/restricted.py, line 227, in restricted
    exec ccode in environment
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 127, in <module>
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/globals.py, line 393, in <lambda>
    self._caller = lambda f: f()
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 34, in user
    return dict(form=auth())
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 1595, in __call__
    return getattr(self, args[0])()
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 3272, in request_reset_password
    if self.email_reset_password(user):
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 3296, in email_reset_password
    message=self.messages.reset_password % d):
  File /base/data/home/apps/e~sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 798, in send
    subject=subject, body=text, **xcc)
  File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 402, in send_mail
    message.send(make_sync_call=make_sync_call)
  File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1108, in send
    message = self.ToProto()
  File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1350, in ToProto
    message = super(EmailMessage, self).ToProto()
  File /base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/mail.py, line 1046, in ToProto
    message.set_subject(_to_str(self.subject))
  File cpp_message.pyx, line 124, in cpp_message.SetScalarAccessors.Setter (third_party/apphosting/python/protobuf/proto1/cpp_message.cc:2229)
TypeError: <class 'gluon.languages.lazyT'> has type <class 'gluon.languages.lazyT'>, but expected one of: str, unicode
@coveralls
Copy link

Coverage Status

Coverage remained the same at 35.81% when pulling a6226d6 on matclab:mail-send-lazy-to-unicode into 587ff56 on web2py:master.

@niphlod
Copy link
Member

niphlod commented May 10, 2015

uhm. the exception is quite ... strange. it's coming from appengine code that seems to accept either a str or a unicode .. this doesn't seem to happen outside appengine, correct ?

@matclab
Copy link
Author

matclab commented May 10, 2015

Yes, I didn't encounter it on a local machine.

On Sun, May 10, 2015 at 9:49 PM niphlod notifications@github.com wrote:

uhm. the exception is quite ... strange. it's coming from appengine code
that seems to accept either a str or a unicode .. this doesn't seem to
happen outside appengine, correct ?


Reply to this email directly or view it on GitHub
#964 (comment).

@mdipierro
Copy link
Contributor

weird they should not be unicode but GAE is weird in other ways too.

mdipierro added a commit that referenced this pull request May 14, 2015
#963 : Convert subject and body to unicode before sending mail
@mdipierro mdipierro merged commit 76cfba7 into web2py:master May 14, 2015
@matclab
Copy link
Author

matclab commented May 14, 2015

Mmmmh, I just stumble on a problem where on og the string was not LazyT but a str with non ascii chars in it. It then raised a

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

I'v replaced unicode by str and it seems to work in both case now (but I'm not an expert of encoding in python2)…
Shall I update the pull request ?

@matclab
Copy link
Author

matclab commented May 18, 2015

(Just a ping to @mdipierro, as i do not know if my previous comment raised a notification, the pull request being merged)

rafaelol added a commit to rafaelol/web2py that referenced this pull request Jan 7, 2016
On PR web2py#964 @matclab forced the encoding of both subject and
text variables to unicode.

After merging it, matclab realized that when we send Unicode
text to the method it raises an exception and asked if he should
change the commit. Unfortunately this thing was kept untouched.

This problem exists because we previously encode the unicode variables
to utf-8 (for instance here https://github.com/web2py/web2py/blob/master/gluon/tools.py#L478-L481) and then force again to unicode. This piece of code shows what happens:

```
>>> a = u'áéí'
>>> a
u'\xe1\xe9\xed'
>>> b = a.encode('utf-8')
>>> b
'\xc3\xa1\xc3\xa9\xc3\xad'
>>> unicode(a)
u'\xe1\xe9\xed'
>>> unicode(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
```

If we force to str, just like @matclab suggested, we solve this issue.
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.

None yet

4 participants