Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/tests/unit/test_cpu_check.py
6939 views
1
from unittest.mock import Mock
2
3
import pytest
4
5
from polars import _cpu_check
6
from polars._cpu_check import check_cpu_flags
7
8
9
@pytest.fixture
10
def _feature_flags(monkeypatch: pytest.MonkeyPatch) -> None:
11
"""Use the default set of feature flags."""
12
feature_flags = "+sse3,+ssse3"
13
monkeypatch.setattr(_cpu_check, "_POLARS_FEATURE_FLAGS", feature_flags)
14
15
16
@pytest.mark.usefixtures("_feature_flags")
17
def test_check_cpu_flags(
18
monkeypatch: pytest.MonkeyPatch, recwarn: pytest.WarningsRecorder
19
) -> None:
20
cpu_flags = {"sse3": True, "ssse3": True}
21
mock_read_cpu_flags = Mock(return_value=cpu_flags)
22
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)
23
24
check_cpu_flags()
25
26
assert len(recwarn) == 0
27
28
29
@pytest.mark.usefixtures("_feature_flags")
30
def test_check_cpu_flags_missing_features(monkeypatch: pytest.MonkeyPatch) -> None:
31
cpu_flags = {"sse3": True, "ssse3": False}
32
mock_read_cpu_flags = Mock(return_value=cpu_flags)
33
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)
34
35
with pytest.warns(RuntimeWarning, match="Missing required CPU features") as w:
36
check_cpu_flags()
37
38
assert "ssse3" in str(w[0].message)
39
40
41
def test_check_cpu_flags_unknown_flag(
42
monkeypatch: pytest.MonkeyPatch,
43
) -> None:
44
real_cpu_flags = {"sse3": True, "ssse3": False}
45
mock_read_cpu_flags = Mock(return_value=real_cpu_flags)
46
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)
47
unknown_feature_flags = "+sse3,+ssse3,+HelloWorld!"
48
monkeypatch.setattr(_cpu_check, "_POLARS_FEATURE_FLAGS", unknown_feature_flags)
49
with pytest.raises(RuntimeError, match="unknown feature flag: 'HelloWorld!'"):
50
check_cpu_flags()
51
52
53
def test_check_cpu_flags_skipped_no_flags(monkeypatch: pytest.MonkeyPatch) -> None:
54
mock_read_cpu_flags = Mock()
55
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)
56
57
check_cpu_flags()
58
59
assert mock_read_cpu_flags.call_count == 0
60
61
62
@pytest.mark.usefixtures("_feature_flags")
63
def test_check_cpu_flags_skipped_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
64
monkeypatch.setenv("POLARS_SKIP_CPU_CHECK", "1")
65
66
mock_read_cpu_flags = Mock()
67
monkeypatch.setattr(_cpu_check, "_read_cpu_flags", mock_read_cpu_flags)
68
69
check_cpu_flags()
70
71
assert mock_read_cpu_flags.call_count == 0
72
73