diff --git a/hy/_compat.py b/hy/_compat.py index 518434c..1411521 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -17,27 +17,6 @@ def reraise(exc_type, value, traceback=None): traceback = None -code_obj_args = ['argcount', 'kwonlyargcount', 'nlocals', 'stacksize', - 'flags', 'code', 'consts', 'names', 'varnames', - 'filename', 'name', 'firstlineno', 'lnotab', 'freevars', - 'cellvars'] - -def rename_function(func, new_name): - """Creates a copy of a function and [re]sets the name at the code-object - level. - """ - c = func.__code__ - new_code = type(c)(*[getattr(c, 'co_{}'.format(a)) - if a != 'name' else str(new_name) - for a in code_obj_args]) - - _fn = type(func)(new_code, func.__globals__, str(new_name), - func.__defaults__, func.__closure__) - _fn.__dict__.update(func.__dict__) - - return _fn - - def isidentifier(x): if x in ('True', 'False', 'None', 'print'): # `print` is special-cased here because Python 2's diff --git a/hy/macros.py b/hy/macros.py index 32658d6..3c91f11 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -9,7 +9,7 @@ import traceback from contextlib import contextmanager -from hy._compat import reraise, rename_function +from hy._compat import reraise from hy.models import replace_hy_obj, HyExpression, HySymbol, wrap_value from hy.lex import mangle from hy.errors import (HyLanguageError, HyMacroExpansionError, HyTypeError, @@ -381,3 +381,24 @@ def tag_macroexpand(tag, tree, module): expr.module = inspect.getmodule(tag_macro) return replace_hy_obj(expr, tree) + + +def rename_function(func, new_name): + """Creates a copy of a function and [re]sets the name at the code-object + level. + """ + c = func.__code__ + new_code = type(c)(*[getattr(c, 'co_{}'.format(a)) + if a != 'name' else str(new_name) + for a in code_obj_args]) + + _fn = type(func)(new_code, func.__globals__, str(new_name), + func.__defaults__, func.__closure__) + _fn.__dict__.update(func.__dict__) + + return _fn + +code_obj_args = ['argcount', 'kwonlyargcount', 'nlocals', 'stacksize', + 'flags', 'code', 'consts', 'names', 'varnames', + 'filename', 'name', 'firstlineno', 'lnotab', 'freevars', + 'cellvars']