Skip to content

Make the inspect module compatible with Hy #1696

@brandonwillard

Description

@brandonwillard

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions