From f80b73a9f6530c6626948b83b35fde3c74eb5c7d Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Mon, 31 May 2021 00:28:16 -0400 Subject: [PATCH 01/12] added getLevelNamesDict() --- Lib/logging/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 555f598de7a925..55a207d2eaf13e 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -116,6 +116,9 @@ 'NOTSET': NOTSET, } +def getLevelNamesDict(): + return _nameToLevel.copy() + def getLevelName(level): """ Return the textual or numeric representation of logging level 'level'. From e5601b0d046efcce8267a0e56a60c8c6dcd5ab2f Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Mon, 31 May 2021 00:41:56 -0400 Subject: [PATCH 02/12] Added getLevelNamesDict doc --- Doc/library/logging.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 70a703dde18a03..58d5c8d930dd57 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1111,6 +1111,12 @@ functions. .. note:: If you are thinking of defining your own levels, please see the section on :ref:`custom-levels`. +.. function:: getLevelNamesDict() + + Returns the dictionary of all available level names and respective logging levels. + + .. versionadded:: 3.11 + .. function:: getLevelName(level) Returns the textual or numeric representation of logging level *level*. From 72a6efac423129fcc576913fb9e974d0d65403db Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 31 May 2021 04:51:03 +0000 Subject: [PATCH 03/12] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst new file mode 100644 index 00000000000000..b92e45e9c64f73 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -0,0 +1 @@ +Added a function that returns a copy of a dict of logging levels: func::`logging.getLevelNamesDict()` \ No newline at end of file From e59141b348bf2c67f6f8c233750188193057c563 Mon Sep 17 00:00:00 2001 From: andrei kulakov Date: Mon, 31 May 2021 01:26:30 -0400 Subject: [PATCH 04/12] Add getLevelNamesDict() to __all__ --- Lib/logging/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 55a207d2eaf13e..45b548569b7104 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -30,14 +30,14 @@ __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', - 'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', - 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', - 'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', - 'captureWarnings', 'critical', 'debug', 'disable', 'error', - 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', - 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', - 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', - 'lastResort', 'raiseExceptions'] +'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', +'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', +'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', +'captureWarnings', 'critical', 'debug', 'disable', 'error', +'exception', 'fatal', 'getLevelNamesDict', 'getLevelName', 'getLogger', +'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', +'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', +'lastResort', 'raiseExceptions'] import threading From bebd56a3d606dcb1b5dc8b6622039cfe79270fb8 Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 10:53:46 -0400 Subject: [PATCH 05/12] Updated func name; expanded doc; added unit test --- Doc/library/logging.rst | 8 +++++--- Lib/logging/__init__.py | 6 +++--- Lib/test/test_logging.py | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 58d5c8d930dd57..32e46a8be0e107 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1111,10 +1111,12 @@ functions. .. note:: If you are thinking of defining your own levels, please see the section on :ref:`custom-levels`. -.. function:: getLevelNamesDict() +.. function:: getLevelNamesMapping() + + Returns a mapping from level names to their corresponding logging + levels. The returned mapping is copied from internal mapping on each call to this + function. - Returns the dictionary of all available level names and respective logging levels. - .. versionadded:: 3.11 .. function:: getLevelName(level) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 45b548569b7104..720efe57b792eb 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -34,10 +34,10 @@ 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', 'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', 'captureWarnings', 'critical', 'debug', 'disable', 'error', -'exception', 'fatal', 'getLevelNamesDict', 'getLevelName', 'getLogger', +'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', -'lastResort', 'raiseExceptions'] +'lastResort', 'raiseExceptions', 'getLevelNamesMapping'] import threading @@ -116,7 +116,7 @@ 'NOTSET': NOTSET, } -def getLevelNamesDict(): +def getLevelNamesMapping(): return _nameToLevel.copy() def getLevelName(level): diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index ee00a32026f65e..bb3d2e60980165 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4390,6 +4390,10 @@ def rec(): self.assertNotIn("Cannot recover from stack overflow.", err) self.assertEqual(rc, 1) + def test_get_level_names_mapping(self): + self.assertEqual(logging._nameToLevel, logging.getLevelNamesMapping()) + self.assertNotEqual(id(logging._nameToLevel), id(logging.getLevelNamesMapping())) + class LogRecordTest(BaseTest): def test_str_rep(self): From 0fffa1cbd5141e73404d1afbf706e7e8e80d0563 Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 10:57:02 -0400 Subject: [PATCH 06/12] fix indentation on __all__ --- Lib/logging/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 720efe57b792eb..b0da0ead6116db 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -30,14 +30,14 @@ __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', -'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', -'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', -'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', -'captureWarnings', 'critical', 'debug', 'disable', 'error', -'exception', 'fatal', 'getLevelName', 'getLogger', -'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', -'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', -'lastResort', 'raiseExceptions', 'getLevelNamesMapping'] + 'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', + 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', + 'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', + 'captureWarnings', 'critical', 'debug', 'disable', 'error', + 'exception', 'fatal', 'getLevelName', 'getLogger', + 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', + 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', + 'lastResort', 'raiseExceptions', 'getLevelNamesMapping'] import threading From f56e012ecbabc3ebff9e79cc18b88d48283e4b77 Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 10:58:44 -0400 Subject: [PATCH 07/12] fix __all__ again --- Lib/logging/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index b0da0ead6116db..723d9ba5118c2a 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -34,8 +34,8 @@ 'LogRecord', 'Logger', 'LoggerAdapter', 'NOTSET', 'NullHandler', 'StreamHandler', 'WARN', 'WARNING', 'addLevelName', 'basicConfig', 'captureWarnings', 'critical', 'debug', 'disable', 'error', - 'exception', 'fatal', 'getLevelName', 'getLogger', - 'getLoggerClass', 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', + 'exception', 'fatal', 'getLevelName', 'getLogger', 'getLoggerClass', + 'info', 'log', 'makeLogRecord', 'setLoggerClass', 'shutdown', 'warn', 'warning', 'getLogRecordFactory', 'setLogRecordFactory', 'lastResort', 'raiseExceptions', 'getLevelNamesMapping'] From f578c798a46e5c5ba25f19a8c8ae37d954d0dbcd Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 11:01:59 -0400 Subject: [PATCH 08/12] updated new entry --- .../next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst index b92e45e9c64f73..fb37d693b41321 100644 --- a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -1 +1 @@ -Added a function that returns a copy of a dict of logging levels: func::`logging.getLevelNamesDict()` \ No newline at end of file +Added a function that returns a copy of a dict of logging levels: func::`logging.getLevelNamesMapping()` From cc7a8c83ee4f79aa34ff240a391078ff5fb192ad Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 21:39:56 -0400 Subject: [PATCH 09/12] fixed typo in news; expanded tests; added example to the doc --- Doc/library/logging.rst | 6 +++--- Lib/test/test_logging.py | 9 +++++++-- .../Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 32e46a8be0e107..313cff4ff9658f 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1113,9 +1113,9 @@ functions. .. function:: getLevelNamesMapping() - Returns a mapping from level names to their corresponding logging - levels. The returned mapping is copied from internal mapping on each call to this - function. + Returns a mapping from level names to their corresponding logging levels. For example, the + string "CRITICAL" maps to :const:`CRITICAL`. The returned mapping is copied from an internal + mapping on each call to this function. .. versionadded:: 3.11 diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index bb3d2e60980165..addb66684c6261 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4391,8 +4391,13 @@ def rec(): self.assertEqual(rc, 1) def test_get_level_names_mapping(self): - self.assertEqual(logging._nameToLevel, logging.getLevelNamesMapping()) - self.assertNotEqual(id(logging._nameToLevel), id(logging.getLevelNamesMapping())) + def test_get_level_names_mapping(self): + mapping = logging.getLevelNamesMapping() + self.assertEqual(logging._nameToLevel, mapping) # value is equivalent + self.assertIsNot(logging._nameToLevel, mapping) # but not the internal data + new_mapping = logging.getLevelNamesMapping() # another call -> another copy + self.assertIsNot(mapping, new_mapping) # verify not the same object as before + self.assertEqual(mapping, new_mapping) # but equivalent in value class LogRecordTest(BaseTest): diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst index fb37d693b41321..56985bb3ff14ed 100644 --- a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -1 +1 @@ -Added a function that returns a copy of a dict of logging levels: func::`logging.getLevelNamesMapping()` +Added a function that returns a copy of a dict of logging levels: func:`logging.getLevelNamesMapping()` From e71183eb683f32c05091d6e088c3856d7bbfd99c Mon Sep 17 00:00:00 2001 From: AK Date: Mon, 31 May 2021 21:47:21 -0400 Subject: [PATCH 10/12] fix2 for news --- .../next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst index 56985bb3ff14ed..9bb0d4abf4a8b7 100644 --- a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -1 +1 @@ -Added a function that returns a copy of a dict of logging levels: func:`logging.getLevelNamesMapping()` +Added a function that returns a copy of a dict of logging levels: :func:`logging.getLevelNamesMapping()` From 3d41503bfce4f7886f4404b311e41238d66fbe58 Mon Sep 17 00:00:00 2001 From: AK Date: Tue, 1 Jun 2021 12:59:37 -0400 Subject: [PATCH 11/12] remove parens from doc link --- .../next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst index 9bb0d4abf4a8b7..d864e1b4e51e3d 100644 --- a/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst +++ b/Misc/NEWS.d/next/Library/2021-05-31-04-51-02.bpo-43858.r7LOu6.rst @@ -1 +1 @@ -Added a function that returns a copy of a dict of logging levels: :func:`logging.getLevelNamesMapping()` +Added a function that returns a copy of a dict of logging levels: :func:`logging.getLevelNamesMapping` From 5b1631c47792422591326a9df40b7a6d174a7429 Mon Sep 17 00:00:00 2001 From: AK Date: Wed, 2 Jun 2021 11:09:34 -0400 Subject: [PATCH 12/12] fixed double definition of test --- Lib/test/test_logging.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index addb66684c6261..6d111908e7c395 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4391,13 +4391,12 @@ def rec(): self.assertEqual(rc, 1) def test_get_level_names_mapping(self): - def test_get_level_names_mapping(self): - mapping = logging.getLevelNamesMapping() - self.assertEqual(logging._nameToLevel, mapping) # value is equivalent - self.assertIsNot(logging._nameToLevel, mapping) # but not the internal data - new_mapping = logging.getLevelNamesMapping() # another call -> another copy - self.assertIsNot(mapping, new_mapping) # verify not the same object as before - self.assertEqual(mapping, new_mapping) # but equivalent in value + mapping = logging.getLevelNamesMapping() + self.assertEqual(logging._nameToLevel, mapping) # value is equivalent + self.assertIsNot(logging._nameToLevel, mapping) # but not the internal data + new_mapping = logging.getLevelNamesMapping() # another call -> another copy + self.assertIsNot(mapping, new_mapping) # verify not the same object as before + self.assertEqual(mapping, new_mapping) # but equivalent in value class LogRecordTest(BaseTest):