hy icon indicating copy to clipboard operation
hy copied to clipboard

Make the `inspect` module compatible with Hy

Open brandonwillard opened this issue 5 years ago • 2 comments

The inspect module isn't compatible with some Hy-derived objects, and code that implicitly uses inspect (including other standard library modules) is generally error-prone because of this.

In particular, debuggers are a lot less useful due to these inspect incompatibilities. For example, ll doesn't work properly even when source is correctly associated with function/code objects.

For another particularly interesting example, notice how PyPy raises exceptions during calls to inspect.getsource:

hy 0.15.0+32.g4af87dc.dirty using PyPy(fdd60ed87e941677e8ea11acf9f1819466521bf2) 3.5.3 on Linux
=> (import inspect [tests.resources.bin.pdb [*]])
=> (inspect.findsource func2)
(['(defn func1 [x]\n', '  (print "func1")\n', '  (+ 1 x))\n', '(defn func2 [x]\n', '  (print "func2")\n', '  (func1 x))\n'], -1)
=> (inspect.getsource func2)
Traceback (most recent call last):
  File "/home/bwillard/projects/code/python/hy/hy/importer.py", line 140, in hy_eval
    return eval(ast_compile(expr, "<eval>", "eval"), namespace)
  File "<eval>", line 1, in <module>
  File "/home/bwillard/apps/anaconda3/envs/hy-dev-pypy35/lib-python/3/inspect.py", line 947, in getsource
    lines, lnum = getsourcelines(object)
  File "/home/bwillard/apps/anaconda3/envs/hy-dev-pypy35/lib-python/3/inspect.py", line 939, in getsourcelines
    return getblock(lines[lnum:]), lnum + 1
  File "/home/bwillard/apps/anaconda3/envs/hy-dev-pypy35/lib-python/3/inspect.py", line 919, in getblock
    for _token in tokens:
  File "/home/bwillard/apps/anaconda3/envs/hy-dev-pypy35/lib-python/3/tokenize.py", line 597, in _tokenize
    raise TokenError("EOF in multi-line statement", (lnum, 0))
tokenize.TokenError: ('EOF in multi-line statement', (2, 0))

CPython raises no exception, but the result it gives is incomplete:

hy 0.15.0+32.g4af87dc.dirty using CPython(default) 2.7.15 on Linux
=> (import inspect [tests.resources.bin.pdb [*]])
=> (inspect.findsource func2)
(['(defn func1 [x]\n', '  (print "func1")\n', '  (+ 1 x))\n', '(defn func2 [x]\n', '  (print "func2")\n', '  (func1 x))\n'], 0)
=> (inspect.getsource func2)
'(defn func1 [x]\n'

The debugger triggers a call to inspect during code listing, and a -1 start-of-line is returned by PyPy's inspect.findsource. This causes inspect.getsourcelines to send inspect.getblock only the last line and, ultimately, the Python tokenizer to barf.

We might be able to introduce some patches that fix these issues.

Originally posted by @brandonwillard in https://github.com/hylang/hy/pull/1680#issuecomment-420411517

brandonwillard avatar Nov 09 '18 06:11 brandonwillard

Looking at the source of inspect, I think the main problem is a regex inside findsource() and one in formatannotation(). You'd have to load inspect and replace those two functions. If that's what people want I could have a go at it.

atisharma avatar Feb 12 '24 16:02 atisharma

You're welcome to. But I would want a test for this feature, and it's the kind of thing that could be more laborious to write a test for than to implement.

Kodiologist avatar Feb 12 '24 17:02 Kodiologist