From 9f136c2ba6e88641fc36194b98fee18e8ba06d68 Mon Sep 17 00:00:00 2001 From: Timon Viola <44016238+timonviola@users.noreply.github.com> Date: Sat, 13 Jul 2024 11:19:33 +0200 Subject: [PATCH 1/6] docs: Update ConfigParser.read() docs with multi-file read example Fixes ##96765 --- Doc/library/configparser.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index e84fb513e45267..74193d41de002f 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -984,6 +984,24 @@ ConfigParser Objects converter gets its own corresponding :meth:`!get*()` method on the parser object and section proxies. + It is possible to read several configurations into a single + :class:`ConfigParser`, where the most recently added configuration has the + highest priority. Any conflicting keys are taken from the more recent + configuration while the previously existing keys are retained. The example + below reads in an `override.ini` file, which will override any conflicting + keys from the `example.ini` file. + + .. code-block:: ini + + [DEFAULT] + ServerAliveInterval = -1 + + .. doctest:: + + >>> config = configparser.ConfigParser() + >>> config.read(['example.ini','override.ini']) + >>> print(config.get('DEFAULT', 'ServerAliveInterval')) # 45 -> -1 + .. versionchanged:: 3.1 The default *dict_type* is :class:`collections.OrderedDict`. From 7cca12d62198e703f7b9c0d47b7dcf4a0eef2eca Mon Sep 17 00:00:00 2001 From: Timon Viola <44016238+timonviola@users.noreply.github.com> Date: Sat, 13 Jul 2024 11:29:02 +0200 Subject: [PATCH 2/6] fix: fix double backtick --- Doc/library/configparser.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 74193d41de002f..1b02cd6028b88e 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -988,8 +988,8 @@ ConfigParser Objects :class:`ConfigParser`, where the most recently added configuration has the highest priority. Any conflicting keys are taken from the more recent configuration while the previously existing keys are retained. The example - below reads in an `override.ini` file, which will override any conflicting - keys from the `example.ini` file. + below reads in an ``override.ini`` file, which will override any conflicting + keys from the ``example.ini`` file. .. code-block:: ini From 902afb8e60303afadd90c8c2676a9ea80ae0b1b9 Mon Sep 17 00:00:00 2001 From: Timon Viola <44016238+timonviola@users.noreply.github.com> Date: Sat, 13 Jul 2024 12:09:58 +0200 Subject: [PATCH 3/6] fix: fix doctest --- Doc/library/configparser.rst | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 1b02cd6028b88e..3866e48606b20c 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -998,9 +998,16 @@ ConfigParser Objects .. doctest:: + >>> config = configparser.ConfigParser() + >>> config['DEFAULT'] = {'ServerAliveInterval': '-1'} + >>> with open('override.ini', 'w') as configfile: + ... config.write(configfile) + ... >>> config = configparser.ConfigParser() >>> config.read(['example.ini','override.ini']) - >>> print(config.get('DEFAULT', 'ServerAliveInterval')) # 45 -> -1 + ['example.ini', 'override.ini'] + >>> print(config.get('DEFAULT', 'ServerAliveInterval')) + -1 .. versionchanged:: 3.1 The default *dict_type* is :class:`collections.OrderedDict`. From 29343d9889adce446086ab7a0a345aeb8ccef0e7 Mon Sep 17 00:00:00 2001 From: Timon Viola <44016238+timonviola@users.noreply.github.com> Date: Sat, 13 Jul 2024 12:43:03 +0200 Subject: [PATCH 4/6] docs: add override example to quick start section --- Doc/library/configparser.rst | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 3866e48606b20c..84f1a3fcbf9fb7 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -147,23 +147,28 @@ case-insensitive and stored in lowercase [1]_. It is possible to read several configurations into a single :class:`ConfigParser`, where the most recently added configuration has the highest priority. Any conflicting keys are taken from the more recent -configuration while the previously existing keys are retained. +configuration while the previously existing keys are retained. The example +below reads in an ``override.ini`` file, which will override any conflicting +keys from the ``example.ini`` file. + +.. code-block:: ini + + [DEFAULT] + ServerAliveInterval = -1 .. doctest:: - >>> another_config = configparser.ConfigParser() - >>> another_config.read('example.ini') - ['example.ini'] - >>> another_config['topsecret.server.example']['Port'] - '50022' - >>> another_config.read_string("[topsecret.server.example]\nPort=48484") - >>> another_config['topsecret.server.example']['Port'] - '48484' - >>> another_config.read_dict({"topsecret.server.example": {"Port": 21212}}) - >>> another_config['topsecret.server.example']['Port'] - '21212' - >>> another_config['topsecret.server.example']['ForwardX11'] - 'no' + >>> config = configparser.ConfigParser() + >>> config['DEFAULT'] = {'ServerAliveInterval': '-1'} + >>> with open('override.ini', 'w') as configfile: + ... config.write(configfile) + ... + >>> config = configparser.ConfigParser() + >>> config.read(['example.ini','override.ini']) + ['example.ini', 'override.ini'] + >>> print(config.get('DEFAULT', 'ServerAliveInterval')) + -1 + This behaviour is equivalent to a :meth:`ConfigParser.read` call with several files passed to the *filenames* parameter. From 31ba4f44c1a53d8e1fe25c29c7f1064af10f62d1 Mon Sep 17 00:00:00 2001 From: Timon Viola <44016238+timonviola@users.noreply.github.com> Date: Sat, 13 Jul 2024 14:00:29 +0200 Subject: [PATCH 5/6] docs: fix doc tests --- Doc/library/configparser.rst | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 84f1a3fcbf9fb7..1e08961a2a6bfe 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -158,15 +158,15 @@ keys from the ``example.ini`` file. .. doctest:: - >>> config = configparser.ConfigParser() - >>> config['DEFAULT'] = {'ServerAliveInterval': '-1'} + >>> config_override = configparser.ConfigParser() + >>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'} >>> with open('override.ini', 'w') as configfile: - ... config.write(configfile) + ... config_override.write(configfile) ... - >>> config = configparser.ConfigParser() - >>> config.read(['example.ini','override.ini']) + >>> config_override = configparser.ConfigParser() + >>> config_override.read(['example.ini','override.ini']) ['example.ini', 'override.ini'] - >>> print(config.get('DEFAULT', 'ServerAliveInterval')) + >>> print(config_override.get('DEFAULT', 'ServerAliveInterval')) -1 @@ -1003,15 +1003,15 @@ ConfigParser Objects .. doctest:: - >>> config = configparser.ConfigParser() - >>> config['DEFAULT'] = {'ServerAliveInterval': '-1'} + >>> config_override = configparser.ConfigParser() + >>> config_override['DEFAULT'] = {'ServerAliveInterval': '-1'} >>> with open('override.ini', 'w') as configfile: - ... config.write(configfile) + ... config_override.write(configfile) ... - >>> config = configparser.ConfigParser() - >>> config.read(['example.ini','override.ini']) + >>> config_override = configparser.ConfigParser() + >>> config_override.read(['example.ini','override.ini']) ['example.ini', 'override.ini'] - >>> print(config.get('DEFAULT', 'ServerAliveInterval')) + >>> print(config_override.get('DEFAULT', 'ServerAliveInterval')) -1 .. versionchanged:: 3.1 From 6f364df6ebc5a84a4055b59cc3dbdff513eb86a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Sat, 13 Jul 2024 14:31:06 +0200 Subject: [PATCH 6/6] Apply suggestions from code review --- Doc/library/configparser.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/configparser.rst b/Doc/library/configparser.rst index 1e08961a2a6bfe..6fae03cd5c96fd 100644 --- a/Doc/library/configparser.rst +++ b/Doc/library/configparser.rst @@ -164,7 +164,7 @@ keys from the ``example.ini`` file. ... config_override.write(configfile) ... >>> config_override = configparser.ConfigParser() - >>> config_override.read(['example.ini','override.ini']) + >>> config_override.read(['example.ini', 'override.ini']) ['example.ini', 'override.ini'] >>> print(config_override.get('DEFAULT', 'ServerAliveInterval')) -1 @@ -1009,7 +1009,7 @@ ConfigParser Objects ... config_override.write(configfile) ... >>> config_override = configparser.ConfigParser() - >>> config_override.read(['example.ini','override.ini']) + >>> config_override.read(['example.ini', 'override.ini']) ['example.ini', 'override.ini'] >>> print(config_override.get('DEFAULT', 'ServerAliveInterval')) -1