From c8fff23ca94cc7187849d64cce42dbf8c27a09da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 18 Feb 2019 14:03:43 +0100 Subject: [PATCH 1/7] Convert key to string when accessing ConfigParser --- Lib/configparser.py | 3 ++- Lib/test/test_configparser.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 79a991084b8f33..44c089f615d1dc 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -956,7 +956,8 @@ def remove_section(self, section): return existed def __getitem__(self, key): - if key != self.default_section and not self.has_section(key): + key = str(key) + if not key in self: raise KeyError(key) return self._proxies[key] diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index f16da116a745f3..9348d3a98ae04a 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -428,6 +428,11 @@ def test_basic_from_dict(self): }, }) + def test_string_conversion(self): + cf = self.newconfig() + cf[123] = {} + self.assertEqual(cf[123], {}) + def test_case_sensitivity(self): cf = self.newconfig() cf.add_section("A") From 3f84272af38d53698c2c3d06c481155970987899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 20 Jan 2020 16:06:59 +0100 Subject: [PATCH 2/7] Revert "Convert key to string when accessing ConfigParser" This reverts commit c8fff23ca94cc7187849d64cce42dbf8c27a09da. --- Lib/configparser.py | 3 +-- Lib/test/test_configparser.py | 5 ----- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 44c089f615d1dc..79a991084b8f33 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -956,8 +956,7 @@ def remove_section(self, section): return existed def __getitem__(self, key): - key = str(key) - if not key in self: + if key != self.default_section and not self.has_section(key): raise KeyError(key) return self._proxies[key] diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py index 9348d3a98ae04a..f16da116a745f3 100644 --- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -428,11 +428,6 @@ def test_basic_from_dict(self): }, }) - def test_string_conversion(self): - cf = self.newconfig() - cf[123] = {} - self.assertEqual(cf[123], {}) - def test_case_sensitivity(self): cf = self.newconfig() cf.add_section("A") From 8ecc7bdd3889d0d7062ef8d6cb27c072afa25a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 20 Jan 2020 16:16:19 +0100 Subject: [PATCH 3/7] Document that keys are always strings --- Doc/library/configparser.rst | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 185b4a10ec990e..4de2191859d1fc 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -139,12 +139,20 @@ case-insensitive and stored in lowercase [1]_. Supported Datatypes ------------------- -Config parsers do not guess datatypes of values in configuration files, always -storing them internally as strings. This means that if you need other -datatypes, you should convert on your own: +Config parsers do not guess datatypes in configuration files, always storing +both section names and values as strings. This means that if you need other +datatypes, you should convert on your own and that section names will always be +converted to strings when writting a new value: .. doctest:: + >>> config[123] = {} + >>> config[123] + Traceback (most recent call last): + ... + KeyError: 123 + >>> config['123'] + >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) From 6489fad261b50646befd66f2887a4e21314f870e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 1 Jun 2020 02:22:50 +0200 Subject: [PATCH 4/7] Fix typo --- Doc/library/configparser.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 4de2191859d1fc..d8e6953e42f5d0 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -142,7 +142,7 @@ Supported Datatypes Config parsers do not guess datatypes in configuration files, always storing both section names and values as strings. This means that if you need other datatypes, you should convert on your own and that section names will always be -converted to strings when writting a new value: +converted to strings when writing a new value: .. doctest:: From 7885da48114e3080d3627d7728c226eb11ffc86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 1 Jun 2020 02:29:29 +0200 Subject: [PATCH 5/7] Move the remark in its own section --- Doc/library/configparser.rst | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index d8e6953e42f5d0..f296602b4c76f1 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -139,20 +139,13 @@ case-insensitive and stored in lowercase [1]_. Supported Datatypes ------------------- -Config parsers do not guess datatypes in configuration files, always storing -both section names and values as strings. This means that if you need other -datatypes, you should convert on your own and that section names will always be -converted to strings when writing a new value: + +Config parsers do not guess datatypes of values in configuration files, always +storing them internally as strings. This means that if you need other +datatypes, you should convert on your own:: .. doctest:: - >>> config[123] = {} - >>> config[123] - Traceback (most recent call last): - ... - KeyError: 123 - >>> config['123'] - >>> int(topsecret['Port']) 50022 >>> float(topsecret['CompressionLevel']) @@ -180,6 +173,20 @@ provide equivalent :meth:`~ConfigParser.getint` and :meth:`~ConfigParser.getfloat` methods. You can register your own converters and customize the provided ones. [1]_ +Section names will also be stored as strings and will need to be converted +manually when reading a section: + +.. doctest:: + + >>> config[123] = {} + >>> config[123] + Traceback (most recent call last): + ... + KeyError: 123 + >>> config['123'] + + + Fallback Values --------------- From 57c3fac03caefae7adb60be9cbed50fe43861eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Mon, 1 Jun 2020 02:31:42 +0200 Subject: [PATCH 6/7] Reduce diff --- Doc/library/configparser.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index f296602b4c76f1..452790f3a174fa 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -139,10 +139,9 @@ case-insensitive and stored in lowercase [1]_. Supported Datatypes ------------------- - Config parsers do not guess datatypes of values in configuration files, always storing them internally as strings. This means that if you need other -datatypes, you should convert on your own:: +datatypes, you should convert on your own: .. doctest:: From 5a6a8f7fe348c3c53ae0826a4a7a430bc72b730e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Thu, 2 May 2024 19:07:45 +0200 Subject: [PATCH 7/7] Update Doc/library/configparser.rst Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Doc/library/configparser.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 452790f3a174fa..1667c81dc7de90 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -172,7 +172,7 @@ provide equivalent :meth:`~ConfigParser.getint` and :meth:`~ConfigParser.getfloat` methods. You can register your own converters and customize the provided ones. [1]_ -Section names will also be stored as strings and will need to be converted +Section names are also stored as strings and need to be converted manually when reading a section: .. doctest::