Issue7077
Created on 2009-10-07 12:42 by rszefler, last changed 2009-11-05 17:53 by vinay.sajip.
| Messages (7) | |||
|---|---|---|---|
| msg93694 - (view) | Author: Robert Szefler (rszefler) | Date: 2009-10-07 12:42 | |
Trying to .emit() a Unicode string causes an awkward exception to be thrown:
Traceback (most recent call last):
File "/usr/lib/python2.5/logging/handlers.py", line 672, in emit
self.socket.sendto(msg, self.address)
TypeError: sendto() takes exactly 3 arguments (2 given)
The issue is fixed simply by adding some sort of encoding coercion
before the sendto, for example:
if type(msg)==unicode: msg=msg.encode('utf-8')
|
|||
| msg93724 - (view) | Author: Vinay Sajip (vinay.sajip) | Date: 2009-10-07 21:15 | |
To do this in a non-arbitrary way, it would make sense for the SysLogHandler (and perhaps the other socket-based handlers, too) to grow an optional encoding argument to their constructors, to be used to encode when converting from unicode to str (str -> bytes for Py3K). How does that sound? |
|||
| msg93734 - (view) | Author: Robert Szefler (rszefler) | Date: 2009-10-08 08:09 | |
Fine with me, though problems would arise. Default encoding for example. If encoding selection is mandatory it would break compatibility. Using default locale is not such a good idea - local machine's locale would generally not need to have any correlation to the remote logger's. Maybe the best solution would be to coerce the text to ASCII per default (such as not to break current semantics) but fix the exception thrown (throw an Unicode*Error) and allow an optional encoding parameter to handle non-ASCII characters? |
|||
| msg93737 - (view) | Author: Vinay Sajip (vinay.sajip) | Date: 2009-10-08 09:03 | |
> Robert Szefler added the comment: > > Fine with me, though problems would arise. Default encoding for example. > If encoding selection is mandatory it would break compatibility. Using > default locale is not such a good idea - local machine's locale would > generally not need to have any correlation to the remote logger's. I'm not planning to make encoding selection mandatory: I would provide a parameter encoding=None so backward compatibility is preserved. On 2.x: During output, a check will be made for Unicode. If not found, the data is output as is. Otherwise (if Unicode) it's encoded using either the specified encoding (if not None) or some default - for example, locale.getpreferredencoding(). I understand what you're saying about the locales of two different machines not being the same - but there's no way around this, because if a socket receives bytes representing text, it needs to know what encoding was used so that it can reconstruct the Unicode correctly. So that information at least needs to be known at the receiving end, rather than guessd. While 'utf-8' might be a reasonable choice, I'm not sure it should be enforced. So the code sending the bytes can specify e.g. 'cp1251' and the other end has to know so it can decode correctly. I've posted on python-dev for advice about what encoding to use if none is specified. On 3.x: We will always be passing Unicode in so we will always need to convert to bytes using some encoding. Again, if not specified, a suitable default encoding needs to be chosen. > Maybe the best solution would be to coerce the text to ASCII per default > (such as not to break current semantics) but fix the exception thrown > (throw an Unicode*Error) and allow an optional encoding parameter to > handle non-ASCII characters? I'm not exactly sure what you mean, but I think I've covered it in my comments above. To summarise: On 2.x, encoding is not mandatory but if Unicode is passed in, either a specified encoding or a suitable default encoding will be used to encode the Unicode into str. On 3.x, encoding is not mandatory and Unicode should always be passed in, which will be encoded to bytes using either a specified encoding or a suitable default encoding. |
|||
| msg94141 - (view) | Author: Vinay Sajip (vinay.sajip) | Date: 2009-10-16 16:28 | |
According to information from Martin von Löwis - see http://mail.python.org/pipermail/python-dev/2009-October/092825.html - UTF-8 should always be used, with a BOM, when sending Unicode (according to RFC 5424). The fix will use this approach. No encoding parameter will be added. |
|||
| msg94323 - (view) | Author: Vinay Sajip (vinay.sajip) | Date: 2009-10-21 20:34 | |
Fix checked into trunk and py3k (r75586). Please verify in your environment and post your results here. There are no plans to backport this to 2.6 or earlier. |
|||
| msg94937 - (view) | Author: Vinay Sajip (vinay.sajip) | Date: 2009-11-05 17:53 | |
No adverse feedback on fix, so closing. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2009-11-05 17:53:11 | vinay.sajip | set | status: pending -> closed messages: + msg94937 |
| 2009-10-21 20:34:43 | vinay.sajip | set | status: open -> pending resolution: fixed messages: + msg94323 |
| 2009-10-16 16:28:47 | vinay.sajip | set | messages: + msg94141 |
| 2009-10-09 21:45:55 | vinay.sajip | set | assignee: vinay.sajip |
| 2009-10-08 09:03:32 | vinay.sajip | set | messages: + msg93737 |
| 2009-10-08 08:09:16 | rszefler | set | messages: + msg93734 |
| 2009-10-07 21:15:19 | vinay.sajip | set | messages: + msg93724 |
| 2009-10-07 19:42:57 | r.david.murray | set | nosy:
+ vinay.sajip |
| 2009-10-07 12:42:37 | rszefler | create | |