From 85a5629954671c46cf25c63067f3a1b1f7d96ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:04:29 +0200 Subject: [PATCH 1/7] allow to invalidate platform's information caches --- Lib/platform.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Lib/platform.py b/Lib/platform.py index d6322c9d99d2f3..8ec2595bd8fe3f 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -31,6 +31,7 @@ # # # +# 1.0.9 - added cache_clear() function to invalidate cached values # 1.0.8 - changed Windows support to read version from kernel32.dll # 1.0.7 - added DEV_NULL # 1.0.6 - added linux_distribution() @@ -109,7 +110,7 @@ """ -__version__ = '1.0.8' +__version__ = '1.0.9' import collections import os @@ -1441,6 +1442,18 @@ def freedesktop_os_release(): return _os_release_cache.copy() +def invalidate_caches(): + """Invalidate the cached results.""" + global _uname_cache + _uname_cache = None + + global _os_release_cache + _os_release_cache = None + + _sys_version_cache.clear() + _platform_cache.clear() + + ### Command line interface if __name__ == '__main__': From 992b5619d92af92d6f69f381462ae02f632c5f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:04:35 +0200 Subject: [PATCH 2/7] add test --- Lib/test/test_platform.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index 40d5fb338ce563..e04ad142061ad3 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -83,6 +83,38 @@ def clear_caches(self): platform._uname_cache = None platform._os_release_cache = None + def test_invalidate_caches(self): + self.clear_caches() + + self.assertDictEqual(platform._platform_cache, {}) + self.assertDictEqual(platform._sys_version_cache, {}) + self.assertIsNone(platform._uname_cache) + self.assertIsNone(platform._os_release_cache) + + # fill the cached entries (some have side effects on others) + platform.platform() # for platform._platform_cache + platform.python_implementation() # for platform._sys_version_cache + platform.uname() # for platform._uname_cache + + # check that the cache are filled + self.assertNotEqual(platform._platform_cache, {}) + self.assertNotEqual(platform._sys_version_cache, {}) + self.assertIsNotNone(platform._uname_cache) + + try: + platform.freedesktop_os_release() + except OSError: + self.assertIsNone(platform._os_release_cache) + else: + self.assertIsNotNone(platform._os_release_cache) + + with self.subTest('clear platform caches'): + platform.invalidate_caches() + self.assertDictEqual(platform._platform_cache, {}) + self.assertDictEqual(platform._sys_version_cache, {}) + self.assertIsNone(platform._uname_cache) + self.assertIsNone(platform._os_release_cache) + def test_architecture(self): res = platform.architecture() From 2b89d91ad4a215daf128da7fca861ed0d752664b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:04:41 +0200 Subject: [PATCH 3/7] add docs --- Doc/library/platform.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Doc/library/platform.rst b/Doc/library/platform.rst index f082393ef9363c..b014f49423edff 100644 --- a/Doc/library/platform.rst +++ b/Doc/library/platform.rst @@ -359,3 +359,15 @@ Android Platform `__. .. versionadded:: 3.13 + + +Miscellaneous +------------- + +.. function:: invalidate_caches() + + Clear out the internal cache of information, such as the :func:`uname`. + This is typically useful when the platform's :func:`node` is changed + by an external process and one needs to retrieve the updated value. + + .. versionadded:: 3.14 From 8f871795408503ecb237fa02200e7357d41d26bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:04:44 +0200 Subject: [PATCH 4/7] add NEWS --- .../next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst b/Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst new file mode 100644 index 00000000000000..035f57a6d1802d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst @@ -0,0 +1,2 @@ +Add :func:`platform.invalidate_caches` to invalidate cached results. Patch +by Bénédikt Tran. From 2d9f32863324326a0bd5f685bf9f687d098d9448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:06:10 +0200 Subject: [PATCH 5/7] add What's New --- Doc/whatsnew/3.14.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index aecc7cabd0d1f5..9c841c94f23a09 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -146,6 +146,13 @@ pathlib (Contributed by Barney Gale in :gh:`73991`.) +platform +-------- + +* Add :func:`platform.invalidate_caches` to invalidate the cached results. + + (Contributed by Bénédikt Tran in :gh:`122525`.) + pdb --- From 8aeccdb99a1d9071843cc17fda9ea129801dc0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:16:06 +0200 Subject: [PATCH 6/7] update issue numbers --- Doc/whatsnew/3.14.rst | 2 +- ...onzO.rst => 2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst} | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) rename Misc/NEWS.d/next/Library/{2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst => 2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst} (67%) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 9c841c94f23a09..fae8dc1a990219 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -151,7 +151,7 @@ platform * Add :func:`platform.invalidate_caches` to invalidate the cached results. - (Contributed by Bénédikt Tran in :gh:`122525`.) + (Contributed by Bénédikt Tran in :gh:`122549`.) pdb --- diff --git a/Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst b/Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst similarity index 67% rename from Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst rename to Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst index 035f57a6d1802d..6b2cbc0a6c9b7a 100644 --- a/Misc/NEWS.d/next/Library/2024-08-01-10-54-46.gh-issue-122525.a_onzO.rst +++ b/Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst @@ -1,2 +1 @@ -Add :func:`platform.invalidate_caches` to invalidate cached results. Patch -by Bénédikt Tran. +Add :func:`platform.invalidate_caches` to invalidate cached results. From ce3201ba10fd1be2da1d2b07edf1d42e20fb01b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Thu, 1 Aug 2024 16:39:48 +0200 Subject: [PATCH 7/7] fix typo --- Lib/platform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/platform.py b/Lib/platform.py index 8ec2595bd8fe3f..239e660cd1621d 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -31,7 +31,7 @@ # # # -# 1.0.9 - added cache_clear() function to invalidate cached values +# 1.0.9 - added invalidate_caches() function to invalidate cached values # 1.0.8 - changed Windows support to read version from kernel32.dll # 1.0.7 - added DEV_NULL # 1.0.6 - added linux_distribution()