Skip to content

Commit

Permalink
Monkey-patch inspect.getsourcefile to reject Hy
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodiologist committed Apr 30, 2024
1 parent 225017d commit ac83f78
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ New Features
Bug Fixes
------------------------------
* Tracebacks now point to the correct code in more cases.
* `help` should no longer crash when objects are missing docstrings.

0.28.0 (released 2024-01-05)
=============================
Expand Down
13 changes: 13 additions & 0 deletions hy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ def rewriting_unparse(ast_obj):
return true_unparse(ast_obj)

ast.unparse = rewriting_unparse


if True:
import inspect
true_getsourcefile = inspect.getsourcefile
def getsourcefile_check_for_hy(object):
"""A monkey-patched `inspect.getsourcefile` that first checks whether
the object was defined in Hy source, and, in that case, returns None
rather than trying to parse the Hy as Python."""
if inspect.getfile(object).endswith('.hy'):
return None
return true_getsourcefile(object)
inspect.getsourcefile = getsourcefile_check_for_hy
9 changes: 9 additions & 0 deletions tests/native_tests/other.hy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; Tests miscellaneous features of the language not covered elsewhere

(import
inspect
typing [get-type-hints]
pytest
hy.errors [HyLanguageError])
Expand Down Expand Up @@ -50,3 +51,11 @@
(setv annotations (get-type-hints AnnotationContainer))
(assert (= (get annotations "x") int))
(assert (= (get annotations "z") bool)))


(defn test-inspect-module-with-missing-docstring [tmp-path monkeypatch]
; https://github.com/hylang/hy/issues/2578
(monkeypatch.syspath-prepend tmp-path)
(.write-text (/ tmp-path "ex_no_ds.hy") "; hello world\n(defclass C)")
(import ex-no-ds)
(assert (is (inspect.getcomments ex-no-ds.C) None)))

0 comments on commit ac83f78

Please sign in to comment.