Skip to content

Commit

Permalink
Simplify native module detection
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Nov 29, 2023
1 parent 7e6d903 commit 7c115e9
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
37 changes: 2 additions & 35 deletions iscc_core/check.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
# -*- coding: utf-8 -*-
"""Inspect lib environment/installation"""
import importlib.machinery
import inspect


__all__ = ["turbo"]

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


def turbo():
# type: () -> bool
Expand All @@ -47,6 +13,7 @@ def turbo():

modules = (cdc, minhash, simhash)
for module in modules:
if not isnativemodule(module):
module_file = inspect.getfile(module)
if module_file.endswith(".py"):
return False
return True # pragma: no cover
11 changes: 0 additions & 11 deletions tests/test_check.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
# -*- coding: utf-8 -*-
from iscc_core import check
import iscc_core as ic
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):
Expand Down

0 comments on commit 7c115e9

Please sign in to comment.