Skip to content
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

test(python): Add test coverage for _cpu_check module #14768

Merged
merged 4 commits into from
Feb 29, 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
1 change: 1 addition & 0 deletions .github/workflows/release-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
env:
IS_LTS_CPU: ${{ matrix.package == 'polars-lts-cpu' }}
IS_MACOS: ${{ matrix.os == 'macos-latest' }}
# IMPORTANT: All features enabled here should also be included in py-polars/polars/_cpu_check.py
run: |
if [[ "$IS_LTS_CPU" = true ]]; then
FEATURES=+sse3,+ssse3,+sse4.1,+sse4.2,+popcnt
Expand Down
14 changes: 3 additions & 11 deletions py-polars/polars/_cpu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ class CPUID_struct(ctypes.Structure):

class CPUID:
def __init__(self) -> None:
if _POLARS_ARCH != "x86-64":
msg = "CPUID is only available for x86"
raise SystemError(msg)
Comment on lines -125 to -127
Copy link
Member Author

Choose a reason for hiding this comment

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

Idem.


if _IS_WINDOWS:
if _IS_64BIT:
# VirtualAlloc seems to fail under some weird
Expand Down Expand Up @@ -187,11 +183,7 @@ def __del__(self) -> None:
self.win.VirtualFree(self.addr, 0, _MEM_RELEASE)


def read_cpu_flags() -> dict[str, bool]:
# Right now we only enable extra feature flags for x86.
if _POLARS_ARCH != "x86-64":
return {}
Comment on lines -192 to -193
Copy link
Member Author

Choose a reason for hiding this comment

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

It was not possible to end up in this code path as non-x86-64 architectures have no features enabled which causes an early return. So I removed this check.


def _read_cpu_flags() -> dict[str, bool]:
# CPU flags from https://en.wikipedia.org/wiki/CPUID
cpuid = CPUID()
cpuid1 = cpuid(1, 0)
Expand Down Expand Up @@ -223,12 +215,12 @@ def check_cpu_flags() -> None:
return

expected_cpu_flags = [f.lstrip("+") for f in _POLARS_FEATURE_FLAGS.split(",")]
supported_cpu_flags = read_cpu_flags()
supported_cpu_flags = _read_cpu_flags()

missing_features = []
for f in expected_cpu_flags:
if f not in supported_cpu_flags:
msg = f'unknown feature flag "{f}"'
msg = f"unknown feature flag: {f!r}"
raise RuntimeError(msg)

if not supported_cpu_flags[f]:
Expand Down
80 changes: 80 additions & 0 deletions py-polars/tests/unit/test_cpu_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from unittest.mock import Mock

import pytest

from polars import _cpu_check
from polars._cpu_check import check_cpu_flags


@pytest.fixture()
def _feature_flags(monkeypatch: pytest.MonkeyPatch) -> None:
"""Use the default set of feature flags."""
feature_flags = "+sse3,+ssse3"
monkeypatch.setattr(_cpu_check, "_POLARS_FEATURE_FLAGS", feature_flags)


@pytest.mark.usefixtures("_feature_flags")
def test_check_cpu_flags(
monkeypatch: pytest.MonkeyPatch, recwarn: pytest.WarningsRecorder
) -> None:
cpu_flags = {"sse3": True, "ssse3": True}
mock_read_cpu_flags = Mock(return_value=cpu_flags)
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)

check_cpu_flags()

assert len(recwarn) == 0


@pytest.mark.usefixtures("_feature_flags")
def test_check_cpu_flags_missing_features(monkeypatch: pytest.MonkeyPatch) -> None:
cpu_flags = {"sse3": True, "ssse3": False}
mock_read_cpu_flags = Mock(return_value=cpu_flags)
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)

with pytest.warns(RuntimeWarning, match="Missing required CPU features") as w:
check_cpu_flags()

assert "ssse3" in str(w[0].message)


def test_check_cpu_flags_unknown_flag(
monkeypatch: pytest.MonkeyPatch,
) -> None:
feature_flags = "+sse3,+ssse3,+HelloWorld!"
monkeypatch.setattr(_cpu_check, "_POLARS_FEATURE_FLAGS", feature_flags)
with pytest.raises(RuntimeError, match="unknown feature flag: 'HelloWorld!'"):
check_cpu_flags()


def test_check_cpu_flags_skipped_no_flags(monkeypatch: pytest.MonkeyPatch) -> None:
mock_read_cpu_flags = Mock()
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)

check_cpu_flags()

assert mock_read_cpu_flags.call_count == 0


@pytest.mark.usefixtures("_feature_flags")
def test_check_cpu_flags_skipped_lts_cpu(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(_cpu_check, "_POLARS_LTS_CPU", True)

mock_read_cpu_flags = Mock()
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)

check_cpu_flags()

assert mock_read_cpu_flags.call_count == 0


@pytest.mark.usefixtures("_feature_flags")
def test_check_cpu_flags_skipped_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("POLARS_SKIP_CPU_CHECK", "1")

mock_read_cpu_flags = Mock()
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)

check_cpu_flags()

assert mock_read_cpu_flags.call_count == 0
Loading