This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Logging to file does not accept UTF16
Type: behavior Stage: resolved
Components: Library (Lib), Unicode Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, hobbestigrou, jcea, pwronisz, python-dev, vinay.sajip, vstinner
Priority: normal Keywords:

Created on 2013-10-15 15:08 by pwronisz, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
patch_utf16 hobbestigrou, 2013-10-15 21:23 review
Messages (10)
msg200001 - (view) Author: Paweł Wroniszewski (pwronisz) Date: 2013-10-15 15:08
The following code reproduces the error:

import logging
logging.root.addHandler(logging.FileHandler(filename='test.log',encoding='UTF16'))
logging.error( u'b\u0142\u0105d')

I think the problem is in the line
logging/__init__.py:860: ufs = fs.decode(stream.encoding)

as Python can't really handle the following code:

fs = "%s\n"
print fs.decode('utf16') % u'foo'
msg200002 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-10-15 15:11
The example works fine on Python3:

localhost$ python3
Python 3.3.2 (default, Aug 23 2013, 19:00:04) 
[GCC 4.8.1 20130603 (Red Hat 4.8.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.root.addHandler(logging.FileHandler(filename='test.log',encoding='UTF16'))
>>> logging.error( u'b\u0142\u0105d')
>>> 

localhost$ hexdump test.log
0000000 feff 0062 0142 0105 0064 000a          
000000c
msg200003 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-10-15 15:16
I don't understand the purpose of fs.decode(stream.encoding): why not directly using ufs=u'%s\n"?

@Pawel: Can you please try to replace ufs=fs.decode(stream.encoding) with ufs=u'%s\n'?
msg200013 - (view) Author: Paweł Wroniszewski (pwronisz) Date: 2013-10-15 18:46
Hi Victor. Your fix works, but actually using simply ufs='%s\n' also seem to work, as 
type( '%s' % u'foo')
and 
type( u'%s' % u'foo')
returns the same - unicode. 

So I would suggest dropping ufs completely, and changing the two occurences to fs. It works - I checked. Maybe some code refactoring could be done then, as I'm not sure if the two nested 'try' statements are needed in such case.
msg200014 - (view) Author: Paweł Wroniszewski (pwronisz) Date: 2013-10-15 18:48
Btw I also don't see the purspose of fs.decode(stream.encoding), as it should rather be encoded and not decoded...
msg200019 - (view) Author: Natal Ngétal (hobbestigrou) * Date: 2013-10-15 21:23
Hi,

here the patch to fix this bug on Python 2.7 with a test suite.
msg200022 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-10-15 21:25
patch_utf16 looks good to me.
msg200025 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-15 21:48
New changeset e94e29dab32c by Victor Stinner in branch '2.7':
Close #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging
http://hg.python.org/cpython/rev/e94e29dab32c
msg200026 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2013-10-15 21:50
This bug is specific to Python 2. I should now be fixed, thanks Paweł for the report and Natal for the patch.

@Natal: Could you please sign the Contributor Agreement for next contributions?
http://www.python.org/psf/contrib/contrib-form/
msg200027 - (view) Author: Paweł Wroniszewski (pwronisz) Date: 2013-10-15 21:51
Cool - looks good to me as well. Thanks guys.
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63466
2013-10-16 02:31:55jceasetnosy: + jcea
2013-10-15 21:51:50pwroniszsetmessages: + msg200027
2013-10-15 21:50:53vstinnersetmessages: + msg200026
2013-10-15 21:48:18python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg200025

resolution: fixed
stage: resolved
2013-10-15 21:26:22vstinnersetnosy: + ezio.melotti
components: + Unicode
2013-10-15 21:25:51vstinnersetmessages: + msg200022
2013-10-15 21:23:29hobbestigrousetfiles: + patch_utf16
nosy: + hobbestigrou
messages: + msg200019

2013-10-15 18:48:49pwroniszsetmessages: + msg200014
2013-10-15 18:46:35pwroniszsetmessages: + msg200013
2013-10-15 15:16:25vstinnersetmessages: + msg200003
2013-10-15 15:11:02vstinnersetnosy: + vstinner
messages: + msg200002
2013-10-15 15:08:24pwroniszcreate