Skip to content
Open
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
5 changes: 5 additions & 0 deletions changelog/59594.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Added ``pythonrelease`` and ``pythonrelease_info`` grains, which follow the
``<foo>release`` / ``<foo>release_info`` convention already used by grains such
as ``osrelease`` / ``osrelease_info``. ``pythonrelease`` is the dotted version
string (for example ``3.11.0``) and ``pythonrelease_info`` is the equivalent
list. The existing list-valued ``pythonversion`` grain is unchanged.
14 changes: 13 additions & 1 deletion salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3193,7 +3193,19 @@ def pythonversion():
"""
# Provides:
# pythonversion
return {"pythonversion": list(sys.version_info)}
# pythonrelease
# pythonrelease_info
#
# ``pythonversion`` is a list rather than a dotted string, which is
# inconsistent with the ``<foo>release`` / ``<foo>release_info`` pair used
# by the other version grains (e.g. ``osrelease`` / ``osrelease_info``).
# ``pythonrelease`` and ``pythonrelease_info`` follow that convention;
# ``pythonversion`` is kept unchanged for backwards compatibility.
return {
"pythonversion": list(sys.version_info),
"pythonrelease": platform.python_version(),
"pythonrelease_info": list(sys.version_info),
}


def pythonpath():
Expand Down
21 changes: 21 additions & 0 deletions tests/pytests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4504,6 +4504,27 @@ def test_pythonversion():
assert ret["pythonversion"] == python_version


def test_pythonrelease():
"""
test pythonrelease and pythonrelease_info

These follow the ``<foo>release`` / ``<foo>release_info`` convention used
by the other version grains, unlike the older list-valued
``pythonversion`` grain which is retained for backwards compatibility.
"""
ret = core.pythonversion()

assert "pythonrelease" in ret
assert ret["pythonrelease"] == platform.python_version()
# dotted major.minor.micro, matching the shape of e.g. osrelease
assert ret["pythonrelease"] == "{}.{}.{}".format(*sys.version_info[:3])

assert "pythonrelease_info" in ret
assert ret["pythonrelease_info"] == [*sys.version_info]
# the pre-existing grain keeps working and stays in sync
assert ret["pythonrelease_info"] == ret["pythonversion"]


@pytest.mark.skip_unless_on_linux
def test_get_machine_id():
"""
Expand Down
Loading