Skip to content

Commit

Permalink
BUG: add support for inspecting built-in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed May 16, 2024
1 parent 8efd0f7 commit 48135f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
23 changes: 15 additions & 8 deletions src/wxc/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ def get_sourcefile(obj):
# this happens for instance with `math.sqrt`
# because inspect.getfile doesn't work on compiled code
# the second condition is met for os.fspath
if inspect.ismodule(obj) or is_builtin_func(obj):
raise
if isinstance(obj, property):
if (
inspect.ismodule(obj)
or is_builtin_func(obj)
or inspect.getmodule(obj) is builtins
or isinstance(obj, property)
):
raise
return get_sourcefile(inspect.getmodule(obj))
return file
Expand Down Expand Up @@ -164,11 +167,15 @@ def get_full_data(name: str) -> dict:
source = get_sourcefile(obj)
except RecursionError:
pass
except TypeError:
# as of Python 3.11, inspect.getfile doesn't have support for properties
# but we're not making this a hard failure in case it is added in the future
# and we fallback to finding out the sourcefile of the class itself
if isinstance(obj, property):
except TypeError as exc:
if "built-in module" in str(exc):
# see https://github.com/neutrinoceros/wxc/issues/233
data["source"] = "built-in"
break
elif isinstance(obj, property):
# as of Python 3.11, inspect.getfile doesn't have support for properties
# but we're not making this a hard failure in case it is added in the future
# and we fallback to finding out the sourcefile of the class itself
continue
else:
raise
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def test_finder(package_name):
assert "source" in imp

filename, _, line = imp["source"].partition(":")
if filename == "built-in":
return
p = Path(filename)
assert p.exists()
if not imp["in_stdlib"]:
Expand Down
10 changes: 0 additions & 10 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from importlib import import_module
from importlib.util import find_spec

Expand Down Expand Up @@ -26,15 +25,6 @@ def test_elementary_queries(capsys, package_name):
ret = main([package_name, "--version"])

out, err = capsys.readouterr()

if package_name == "math" and sys.platform.startswith("win"):
# rich may output an unspecified amount of newlines
# that don't actually affect the result visually
assert out.strip() == ""
assert err == "ERROR failed to locate source data.\n"
assert ret != 0
return

assert out != "unknown"
assert err == ""
assert ret == 0
Expand Down

0 comments on commit 48135f6

Please sign in to comment.