Skip to main content

platform.py

platform.py collects machine, OS, and Python runtime facts into a single portable API. Most results are cached on first call.

Map

LinesSymbolRole
1–60module header, importsstdlib imports, _UNIXCONFDIR constant
61–120_platform_cachemodule-level dict caching expensive calls
121–200uname()wraps os.uname / platform.uname into a named tuple
201–260system() node() release() version() machine() processor()thin accessors over uname() fields
261–340platform()assembles the human-readable platform string
341–430mac_ver()parses sw_vers output on macOS
431–510win32_ver()reads Windows version from the registry
511–600freedesktop_os_release()parses /etc/os-release on Linux
601–700python_version() python_version_tuple()exposes sys.version_info
701–760python_implementation()returns sys.implementation.name
761–900architecture() java_ver() system_alias()misc platform probes

Reading

uname and the field accessors

uname() is the central call. Every single-field accessor delegates to it and pulls one attribute from the named tuple.

# CPython: Lib/platform.py:206 system
def system():
return uname().system

uname() itself tries os.uname() and falls back gracefully on Windows where that call is absent:

# CPython: Lib/platform.py:160 uname
def uname():
global _uname_cache
if _uname_cache is not None:
return _uname_cache
...
try:
system, node, release, version, machine, processor = \
os.uname()
except AttributeError:
...

platform() string assembly

platform() concatenates the most useful fields and optionally appends the processor name. The result is stored in _platform_cache.

# CPython: Lib/platform.py:276 platform
def platform(aliased=False, terse=False):
result = _platform_cache.get((aliased, terse), None)
if result is not None:
return result
...
_platform_cache[(aliased, terse)] = result
return result

freedesktop_os_release on Linux

On Linux, freedesktop_os_release() opens /etc/os-release (falling back to /usr/lib/os-release) and returns a plain dict of key-value pairs. The parser strips shell-style quoting.

# CPython: Lib/platform.py:543 freedesktop_os_release
def freedesktop_os_release():
for candidate in ("/etc/os-release", "/usr/lib/os-release"):
try:
with open(candidate, encoding="utf-8") as f:
return _parse_os_release(f)
except OSError:
pass
raise OSError("...")

mac_ver

mac_ver() shells out to sw_vers and parses its output. A cached result is returned on repeated calls.

# CPython: Lib/platform.py:370 mac_ver
def mac_ver(release='', versioninfo=('', '', ''), machine=''):
...
try:
info = subprocess.check_output(['sw_vers'], text=True)
except OSError:
return release, versioninfo, machine
...

gopy notes

  • uname() maps directly to syscall.Uname (Linux) or syscall.Sysctl variants (macOS). Cache with a sync.Once.
  • freedesktop_os_release() can be ported as a plain file parser; no C dependency.
  • win32_ver() reads the Windows registry; skip or stub for non-Windows builds via a build tag.
  • _platform_cache should use a sync.Map or a mutex-guarded map keyed on the (aliased, terse) pair.
  • python_version() and python_implementation() have no direct counterpart; expose gopy's own version constants instead.

CPython 3.14 changes

  • linux_distribution() was removed in 3.8; it does not appear in 3.14.
  • uname_result became a proper typing.NamedTuple subclass with default values, making all fields optional for callers that construct it directly.
  • freedesktop_os_release() gained stricter quote-stripping to match the os-release spec more closely.
  • No new functions were added between 3.13 and 3.14 for this module.