classification
Title: Python 3.6 regression/change using logging.addLevelName() to clear a name
Type: behavior Stage: needs patch
Components: Library (Lib) Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: markb, python-dev, serhiy.storchaka, vinay.sajip
Priority: normal Keywords:

Created on 2017-01-10 02:47 by markb, last changed 2017-01-11 17:45 by vinay.sajip. This issue is now closed.

Messages (12)
msg285085 - (view) Author: Mark Blakeney (markb) Date: 2017-01-10 02:47
I have code which does a logging.addLevelName(logging.INFO, '') so that the logging level name is not displayed in INFO messages, but is in all other levels. I have been running this code fine since many versions of Python 2 through to 3.5.

Now running with python 3.6.0-1 (on Arch Linux), the name is output in messages as "Level 20". Changing the empty string '' in the call above to a space ' ' is my current work-around.
msg285096 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-01-10 07:39
This change was due to fixing #27937. Since yours is not a common use case, and since the workaround works for you, I propose not to change things back.
msg285098 - (view) Author: Mark Blakeney (markb) Date: 2017-01-10 07:44
Well my work-around is not ideal as I get an extra embedded space in those messages. I'm surprised this is "not a common use case"? It doesn't really worry me but it is a 3.6 incompatibility change which I would think is always far better to avoid.
msg285193 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-01-11 06:58
New changeset 99ad6e871459 by Vinay Sajip in branch 'default':
Closes #29220: Fixed regression in logging.getLevelName().
https://hg.python.org/cpython/rev/99ad6e871459
msg285195 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-11 07:08
Could you provide a working example of using your trick Mark? I think that even with empty name the output contains extra separators.
msg285196 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-11 07:25
As for the patch itself, I would add an indentation for the second "if":

    result = _levelToName.get(level)
    if result is None:
        result = _nameToLevel.get(level)
        if result is None:
            result = "Level %s" % level
    return result

or even use multiple returns for the sake of microoptimization:

    result = _levelToName.get(level)
    if result is not None:
        return result
    result = _nameToLevel.get(level)
    if result is not None:
        return result
    return "Level %s" % level

But I'm not sure that empty name is valid. It can cause problems when parse a configuration file or logs.

I don't understand the use of _nameToLevel. getLevelName('CRITICAL') returns 50, that even is not a string.
msg285204 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-01-11 09:40
> I think that even with empty name the output contains extra separators

Not sure what you mean - please clarify with an example.

> I would add an indentation for the second "if" ... or even use multiple returns for the sake of microoptimization

I will do a further update soon.

> I don't understand the use of _nameToLevel. getLevelName('CRITICAL') returns 50, that even is not a string.

Historically, getLevelName() gets the name for a level *but also the level for a name* (I perhaps should have done a separate API for that, like getLevelForName(), but it's too late now due to backward compatibility requirements - it's been like this for a long time.)
msg285206 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-11 10:07
> Historically, getLevelName() gets the name for a level *but also the level for a name*

Ah, then getLevelName() was broken by issue27937.

>>> logging.getLevelName('NOTSET')
'Level NOTSET'

The fix should be applied to 3.5.
msg285224 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-01-11 14:16
> The fix should be applied to 3.5.

Are you sure?

$ python3.5
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging.getLevelName(logging.NOTSET)
'NOTSET'
>>> logging.getLevelName('NOTSET')
0
>>> 

I think just 3.6 and default are affected - doesn't 3.5 seem OK?
msg285225 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-11 14:25
I'm sure. e4b6faf22e8d is in 3.5 branch.
msg285226 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2017-01-11 14:29
OK, will apply to 3.5 -> default.
msg285248 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-01-11 17:44
New changeset adf8312f377f by Vinay Sajip in branch '3.5':
Issue #29220: Improved fix and test.
https://hg.python.org/cpython/rev/adf8312f377f

New changeset a76eed0baa0f by Vinay Sajip in branch 'default':
Issue #29220: Merged fixes from 3.6.
https://hg.python.org/cpython/rev/a76eed0baa0f
History
Date User Action Args
2017-01-11 17:45:29vinay.sajipsetstatus: open -> closed
resolution: fixed
2017-01-11 17:44:21python-devsetmessages: + msg285248
2017-01-11 14:29:38vinay.sajipsetmessages: + msg285226
2017-01-11 14:25:03serhiy.storchakasetmessages: + msg285225
2017-01-11 14:16:19vinay.sajipsetmessages: + msg285224
2017-01-11 10:07:06serhiy.storchakasetstatus: closed -> open
versions: + Python 3.5, Python 3.7
messages: + msg285206

resolution: fixed -> (no value)
stage: resolved -> needs patch
2017-01-11 09:40:23vinay.sajipsetmessages: + msg285204
2017-01-11 07:25:14serhiy.storchakasetmessages: + msg285196
2017-01-11 07:08:51serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg285195
2017-01-11 06:58:06python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg285193

resolution: fixed
stage: resolved
2017-01-10 07:44:20markbsetstatus: pending -> open

messages: + msg285098
2017-01-10 07:39:32vinay.sajipsetstatus: open -> pending
assignee: vinay.sajip
messages: + msg285096
2017-01-10 03:41:40xiang.zhangsetnosy: + vinay.sajip
2017-01-10 02:47:19markbcreate