From 58003389c55c05eb15dbe630b3c2b9597be6ebeb Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sat, 20 Oct 2018 15:25:58 -0400 Subject: [PATCH] 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. --- hy/inspect.py | 29 ----------------------------- hy/macros.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 33 deletions(-) delete mode 100644 hy/inspect.py diff --git a/hy/inspect.py b/hy/inspect.py deleted file mode 100644 index f6672f4..0000000 --- a/hy/inspect.py +++ /dev/null @@ -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)) diff --git a/hy/macros.py b/hy/macros.py index a86f043..787fef3 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -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)