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
Conversation
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
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 ? |
Yes, I didn't encounter it on a local machine. On Sun, May 10, 2015 at 9:49 PM niphlod notifications@github.com wrote:
|
weird they should not be unicode but GAE is weird in other ways too. |
#963 : Convert subject and body to unicode before sending mail
Mmmmh, I just stumble on a problem where on og the string was not
I'v replaced |
(Just a ping to @mdipierro, as i do not know if my previous comment raised a notification, the pull request being merged) |
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.
Patch for issue #963.
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 restrictedsacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 127, inexec ccode in environment
File /base/data/home/apps/e
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/gluon/globals.py, line 393, insacred-bonus-88417/1.384178859090314065/applications/foundit/controllers/default.py, line 34, in userself._caller = lambda f: f()
File /base/data/home/apps/e
return dict(form=auth())
File /base/data/home/apps/e
sacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 1595, in callsacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 3272, in request_reset_passwordreturn getattr(self, args[0])()
File /base/data/home/apps/e
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_passwordsacred-bonus-88417/1.384178859090314065/gluon/tools.py, line 798, in sendmessage=self.messages.reset_password % d):
File /base/data/home/apps/e
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