-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support to check for compiled extension modules
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |