Opened 12 years ago
Closed 12 years ago
#20371 closed Uncategorized (invalid)
mail_admins should support strings as arguments
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Core (Mail) | Version: | 1.5 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
As the topic says, mail_admins should support str-types as arguments, not only unicode data. The following example triggers an exception which is somewhat critical when used in combination with logging and the AdminEmailHandler:
from django.core.mail import mail_admins subject="Über" mail.mail_admins(subject, "test") >>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Let me explain what the problem is here: A lot of people rely on the AdminEmailHandler which uses mail_admins. Now whenever an exception is raised it is a pretty common use-case to log the exception, like this:
try: bar = my_model.do_something() except Exception, e: logger.critical("do_something failed: %s" %e) return False else: return bar == 0
When the exception message contains unicode data, it will be converted to str. This will cause mail_admins to fail if non-ascii characaters were present.
Change History (3)
follow-up: 2 comment:1 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
Resolution: | invalid |
---|---|
Status: | closed → new |
Sorry, maybe the title of this bug is a bit misleading, it should probably be "AdminEmailHandler should not raise exception if non-unicode data is logged".
The problem here is that mail_admins is called by the logging handler (django.utils.log.AdminEmailHandler), and logging handlers should not raise exceptions (they should log them), even if garbage is passed to them.
Replying to russellm:
You're only getting a unicode decode error here because you've put unicode content into a byte string.
No, subject is a valid UTF-8 string:
In [1]: subject="Über" In [2]: print subject ------> print(subject) Über In [3]: type(subject) Out[3]: <type 'str'> In [4]: u_subject=subject.decode("utf-8") In [5]: type(u_subject) Out[5]: <type 'unicode'> In [6]: print u_subject ------> print(u_subject) Über
BTW, passing u_subject to mail_admins does work!
This could be fixed in two places:
- mail_admins
- AdminEmailHandler (probably the right place to fix this, sorry for initial fuzziness about mail_admins)
comment:3 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
This would lead to catching all exceptions that you get in the code and that is unacceptable.
You're only getting a unicode decode error here because you've put unicode content into a byte string.
If you use:
i.e., you correctly identify the string as unicode, you won't get an error.
mail_admin()
handles unicode content just fine. What you've described here is a simple case of Garbage In, Garbage Out.