Integrate hy.inspect into hy.macros

It's compatibility code, and there's not a lot of it, and having a module with the same name as a standard module can be a bit troublesome.
This commit is contained in:
Kodi Arfer 2018-10-20 15:25:58 -04:00 committed by Brandon T. Willard
parent 701db83ba9
commit 58003389c5
2 changed files with 26 additions and 33 deletions

View File

@ -1,29 +0,0 @@
# Copyright 2018 the authors.
# This file is part of Hy, which is free software licensed under the Expat
# license. See the LICENSE.
from __future__ import absolute_import
import inspect
try:
# Check if we have the newer inspect.signature available.
# Otherwise fallback to the legacy getargspec.
inspect.signature # noqa
except AttributeError:
def has_kwargs(fn):
argspec = inspect.getargspec(fn)
return argspec.keywords is not None
def format_args(fn):
argspec = inspect.getargspec(fn)
return inspect.formatargspec(*argspec)
else:
def has_kwargs(fn):
parameters = inspect.signature(fn).parameters
return any(param.kind == param.VAR_KEYWORD
for param in parameters.values())
def format_args(fn):
return str(inspect.signature(fn))

View File

@ -1,19 +1,41 @@
# Copyright 2018 the authors.
# This file is part of Hy, which is free software licensed under the Expat
# license. See the LICENSE.
import pkgutil
import inspect
import importlib
from collections import defaultdict
from hy._compat import PY3
import hy.inspect
from hy.models import replace_hy_obj, HyExpression, HySymbol, wrap_value
from hy.lex import mangle
from hy._compat import str_type
from hy.errors import HyTypeError, HyMacroExpansionError
try:
# Check if we have the newer inspect.signature available.
# Otherwise fallback to the legacy getargspec.
inspect.signature # noqa
except AttributeError:
def has_kwargs(fn):
argspec = inspect.getargspec(fn)
return argspec.keywords is not None
def format_args(fn):
argspec = inspect.getargspec(fn)
return inspect.formatargspec(*argspec)
else:
def has_kwargs(fn):
parameters = inspect.signature(fn).parameters
return any(param.kind == param.VAR_KEYWORD
for param in parameters.values())
def format_args(fn):
return str(inspect.signature(fn))
CORE_MACROS = [
"hy.core.bootstrap",
]
@ -42,7 +64,7 @@ def macro(name):
def _(fn):
fn.__name__ = '({})'.format(name)
try:
fn._hy_macro_pass_compiler = hy.inspect.has_kwargs(fn)
fn._hy_macro_pass_compiler = has_kwargs(fn)
except Exception:
# An exception might be raised if fn has arguments with
# names that are invalid in Python.
@ -139,7 +161,7 @@ def make_empty_fn_copy(fn):
# can continue running. Unfortunately, the error message that might get
# raised later on while expanding a macro might not make sense at all.
formatted_args = hy.inspect.format_args(fn)
formatted_args = format_args(fn)
fn_str = 'lambda {}: None'.format(
formatted_args.lstrip('(').rstrip(')'))
empty_fn = eval(fn_str)