-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
if _IS_WINDOWS: | ||
if _IS_64BIT: | ||
# VirtualAlloc seems to fail under some weird | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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]: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idem.