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

Logging to file does not accept UTF16 #63466

Closed
pwronisz mannequin opened this issue Oct 15, 2013 · 10 comments
Closed

Logging to file does not accept UTF16 #63466

pwronisz mannequin opened this issue Oct 15, 2013 · 10 comments
Labels
stdlib Python modules in the Lib dir topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@pwronisz
Copy link
Mannequin

pwronisz mannequin commented Oct 15, 2013

BPO 19267
Nosy @vsajip, @jcea, @vstinner, @ezio-melotti, @hobbestigrou
Files
  • patch_utf16
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-10-15.21:48:18.573>
    created_at = <Date 2013-10-15.15:08:24.144>
    labels = ['type-bug', 'library', 'expert-unicode']
    title = 'Logging to file does not accept UTF16'
    updated_at = <Date 2013-10-16.02:31:55.634>
    user = 'https://bugs.python.org/pwronisz'

    bugs.python.org fields:

    activity = <Date 2013-10-16.02:31:55.634>
    actor = 'jcea'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-10-15.21:48:18.573>
    closer = 'python-dev'
    components = ['Library (Lib)', 'Unicode']
    creation = <Date 2013-10-15.15:08:24.144>
    creator = 'pwronisz'
    dependencies = []
    files = ['32137']
    hgrepos = []
    issue_num = 19267
    keywords = []
    message_count = 10.0
    messages = ['200001', '200002', '200003', '200013', '200014', '200019', '200022', '200025', '200026', '200027']
    nosy_count = 7.0
    nosy_names = ['vinay.sajip', 'jcea', 'vstinner', 'ezio.melotti', 'python-dev', 'pwronisz', 'hobbestigrou']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue19267'
    versions = ['Python 2.7']

    @pwronisz
    Copy link
    Mannequin Author

    pwronisz mannequin commented Oct 15, 2013

    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'

    @pwronisz pwronisz mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Oct 15, 2013
    @vstinner
    Copy link
    Member

    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

    @vstinner
    Copy link
    Member

    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'?

    @pwronisz
    Copy link
    Mannequin Author

    pwronisz mannequin commented Oct 15, 2013

    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.

    @pwronisz
    Copy link
    Mannequin Author

    pwronisz mannequin commented Oct 15, 2013

    Btw I also don't see the purspose of fs.decode(stream.encoding), as it should rather be encoded and not decoded...

    @hobbestigrou
    Copy link
    Mannequin

    hobbestigrou mannequin commented Oct 15, 2013

    Hi,

    here the patch to fix this bug on Python 2.7 with a test suite.

    @vstinner
    Copy link
    Member

    patch_utf16 looks good to me.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 15, 2013

    New changeset e94e29dab32c by Victor Stinner in branch '2.7':
    Close bpo-19267: Fix support of multibyte encoding (ex: UTF-16) in the logging
    http://hg.python.org/cpython/rev/e94e29dab32c

    @python-dev python-dev mannequin closed this as completed Oct 15, 2013
    @vstinner
    Copy link
    Member

    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/

    @pwronisz
    Copy link
    Mannequin Author

    pwronisz mannequin commented Oct 15, 2013

    Cool - looks good to me as well. Thanks guys.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir topic-unicode type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant