Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the inspect module compatible with Hy #1696

Open
brandonwillard opened this issue Nov 9, 2018 · 2 comments
Open

Make the inspect module compatible with Hy #1696

brandonwillard opened this issue Nov 9, 2018 · 2 comments
Labels

Comments

@brandonwillard
Copy link
Member

brandonwillard commented Nov 9, 2018

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 #1680 (comment)

@atisharma
Copy link

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.

@Kodiologist
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants