Skip to content

Commit

Permalink
Added support to check for compiled extension modules
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Apr 13, 2022
1 parent 097cc1e commit b180d8d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: poetry install --extras turbo

- name: Run Tests (With Extension Modules)
run: poetry run pytest -q tests
run: poetry run pytest -q tests --turbo=1

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [0.2.6] - Unreleased
- Added `KY` and `MM` to valid prefixes
- Added support to check for compiled extension modules

## [0.2.5] - 2022-04-10
- Fixed missing `jcs` dependency
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [0.2.6] - Unreleased
- Added `KY` and `MM` to valid prefixes
- Added support to check for compiled extension modules

## [0.2.5] - 2022-04-10
- Fixed missing `jcs` dependency
Expand Down
38 changes: 38 additions & 0 deletions iscc_core/check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
"""Inspect lib environment/installation"""
import importlib.machinery
import inspect


EXTENSION_SUFFIXES = tuple(sf.lstrip(".") for sf in importlib.machinery.EXTENSION_SUFFIXES)


def suffix(filename):
return "." in filename and filename.rpartition(".")[-1] or ""


def isnativemodule(module):
"""isnativemodule(thing) → boolean predicate, True if `module`
is a native-compiled (“extension”) module.
Q.v. this fine StackOverflow answer on this subject:
https://stackoverflow.com/a/39304199/298171
"""
# Step one: modules only beyond this point:
if not inspect.ismodule(module): # pragma: no cover
return False

# Step two: return truly when “__loader__” is set:
if isinstance(
getattr(module, "__loader__", None), importlib.machinery.ExtensionFileLoader
): # pragma: no cover
return True

# Step three: in leu of either of those indicators,
# check the module path’s file suffix:
try:
ext = suffix(inspect.getfile(module))
except TypeError as exc:
return "is a built-in" in str(exc)

return ext in EXTENSION_SUFFIXES
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
MB1 = 1024 * 1024


def pytest_addoption(parser):
parser.addoption(
"--turbo", action="store_true", default=False, help="run extension module tests"
)


@pytest.fixture
def turbo(request):
return request.config.getoption("--turbo")


@pytest.fixture(scope="module", name="static_bytes")
def static_bytes_(n: int = MB1, block_size: int = 4) -> bytes:
"""Wraps static_bytes function as a fixture (both can be used)."""
Expand Down
24 changes: 24 additions & 0 deletions tests/test_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from iscc_core import cdc, minhash, simhash
from iscc_core import check
import array


def test_check_suffix():
assert check.suffix("hello.world") == "world"


def test_check_isnativemodule():
assert check.isnativemodule(check) is False
assert check.isnativemodule(array) is True


def test_check_turbo(turbo):
if turbo is False:
assert check.isnativemodule(cdc) is False
assert check.isnativemodule(simhash) is False
assert check.isnativemodule(minhash) is False
else:
assert check.isnativemodule(cdc) is True
assert check.isnativemodule(simhash) is True
assert check.isnativemodule(minhash) is True

0 comments on commit b180d8d

Please sign in to comment.