Skip to content

gh-122549: Allow to invalidate platform's cached results #122547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/library/platform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

@picnixz picnixz Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have used a versionadded:: next here but at that time, it didn't exist yet. Not sure if this warrants an additional commit though. WDYT @vstinner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next will be replaced with 3.14 anyway, there is no need to replace it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea but sometimes users are confused that the feature is present in the docs but not in the current release. That was the purpose of next

8 changes: 8 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
15 changes: 14 additions & 1 deletion Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -109,7 +110,7 @@

"""

__version__ = '1.0.8'
__version__ = '1.0.9'

import collections
import os
Expand Down Expand Up @@ -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__':
Expand Down
32 changes: 32 additions & 0 deletions Lib/test/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :func:`platform.invalidate_caches` to invalidate cached results.
Loading