]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122549: Add platform.invalidate_caches() (#122547)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Fri, 15 Nov 2024 13:52:56 +0000 (14:52 +0100)
committerGitHub <noreply@github.com>
Fri, 15 Nov 2024 13:52:56 +0000 (13:52 +0000)
Allow to invalidate platform's cached results.

Doc/library/platform.rst
Doc/whatsnew/3.14.rst
Lib/platform.py
Lib/test/test_platform.py
Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst [new file with mode: 0644]

index 1beb3b9eb89d22a1d0e85813c2f40b83f5af86b4..cfe1e7ba48da32c15d7ed53b7d985d590d835e4a 100644 (file)
@@ -359,3 +359,15 @@ Android Platform
    <https://storage.googleapis.com/play_public/supported_devices.html>`__.
 
    .. 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
index d38188f0054754976ef8414618884dd50ac16a99..6cec611d111ece38bd745f986d0b1e72dc539228 100644 (file)
@@ -454,6 +454,14 @@ pathlib
   (Contributed by Barney Gale in :gh:`125413`.)
 
 
+platform
+--------
+
+* Add :func:`platform.invalidate_caches` to invalidate the cached results.
+
+  (Contributed by Bénédikt Tran in :gh:`122549`.)
+
+
 pdb
 ---
 
index d6322c9d99d2f3076a3f09d3c4d00a625800024e..239e660cd1621d2b552ad5b644a67813c5502b48 100644 (file)
@@ -31,6 +31,7 @@
 #
 #    <see CVS and SVN checkin messages for history>
 #
+#    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()
@@ -109,7 +110,7 @@ __copyright__ = """
 
 """
 
-__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__':
index 40d5fb338ce563e619a2804e0db0e30e50bd0b25..e04ad142061ad30e9129f6374c3718a6a47ceb04 100644 (file)
@@ -83,6 +83,38 @@ class PlatformTest(unittest.TestCase):
         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()
 
diff --git a/Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst b/Misc/NEWS.d/next/Library/2024-08-01-11-15-55.gh-issue-122549.ztV4Kz.rst
new file mode 100644 (file)
index 0000000..6b2cbc0
--- /dev/null
@@ -0,0 +1 @@
+Add :func:`platform.invalidate_caches` to invalidate cached results.