Skip to content

Commit

Permalink
[mypyc] Remove decorator_helper_name (python#11022)
Browse files Browse the repository at this point in the history
Per mypyc/mypyc#718, renaming the original function name in the module
mapping causes the __name__ of decorated functions to have be
something like __mypyc_*_decorator_helper__. When checking if just
using the original name as the module name (and then later the function
will be overriden by the decorated value) no tests fail. And there are
indeed tests that exhibit rather complex decorator behavior so it seems
there may not actually be anything wrong with using the original name.
  • Loading branch information
jhance authored Sep 3, 2021
1 parent a72fab3 commit 26673be
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
4 changes: 0 additions & 4 deletions mypyc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@
JsonDict = Dict[str, Any]


def decorator_helper_name(func_name: str) -> str:
return '__mypyc_{}_decorator_helper__'.format(func_name)


def shared_lib_name(group_name: str) -> str:
"""Given a group name, return the actual name of its extension module.
Expand Down
3 changes: 1 addition & 2 deletions mypyc/irbuild/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from mypyc.ir.ops import Value, BasicBlock
from mypyc.ir.func_ir import INVALID_FUNC_DEF
from mypyc.ir.class_ir import ClassIR
from mypyc.common import decorator_helper_name
from mypyc.irbuild.targets import AssignmentTarget


Expand All @@ -24,7 +23,7 @@ def __init__(self,
is_decorated: bool = False,
in_non_ext: bool = False) -> None:
self.fitem = fitem
self.name = name if not is_decorated else decorator_helper_name(name)
self.name = name
self.class_name = class_name
self.ns = namespace
# Callable classes implement the '__call__' method, and are used to represent functions
Expand Down
11 changes: 4 additions & 7 deletions mypyc/irbuild/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
check_stop_op, yield_from_except_op, coro_op, send_op, register_function
)
from mypyc.primitives.dict_ops import dict_set_item_op, dict_new_op, dict_get_method_with_none
from mypyc.common import SELF_NAME, LAMBDA_NAME, decorator_helper_name
from mypyc.common import SELF_NAME, LAMBDA_NAME
from mypyc.sametype import is_same_method_signature
from mypyc.irbuild.util import is_constant
from mypyc.irbuild.context import FuncInfo, ImplicitClass
Expand Down Expand Up @@ -100,10 +100,9 @@ def transform_decorator(builder: IRBuilder, dec: Decorator) -> None:
elif dec.func in builder.fdefs_to_decorators:
# Obtain the the function name in order to construct the name of the helper function.
name = dec.func.fullname.split('.')[-1]
helper_name = decorator_helper_name(name)

# Load the callable object representing the non-decorated function, and decorate it.
orig_func = builder.load_global_str(helper_name, dec.line)
orig_func = builder.load_global_str(name, dec.line)
decorated_func = load_decorated_func(builder, dec.func, orig_func)

if decorated_func is not None:
Expand Down Expand Up @@ -333,8 +332,7 @@ def c() -> None:
builder.functions.append(func_ir)
# create the dispatch function
assert isinstance(fitem, FuncDef)
dispatch_name = decorator_helper_name(name) if is_decorated else name
return gen_dispatch_func_ir(builder, fitem, fn_info.name, dispatch_name, sig)
return gen_dispatch_func_ir(builder, fitem, fn_info.name, name, sig)

return func_ir, func_reg

Expand Down Expand Up @@ -384,11 +382,10 @@ def handle_ext_method(builder: IRBuilder, cdef: ClassDef, fdef: FuncDef) -> None
if is_decorated(builder, fdef):
# Obtain the the function name in order to construct the name of the helper function.
_, _, name = fdef.fullname.rpartition('.')
helper_name = decorator_helper_name(name)
# Read the PyTypeObject representing the class, get the callable object
# representing the non-decorated method
typ = builder.load_native_type_object(cdef.fullname)
orig_func = builder.py_get_attr(typ, helper_name, fdef.line)
orig_func = builder.py_get_attr(typ, name, fdef.line)

# Decorate the non-decorated method
decorated_func = load_decorated_func(builder, fdef, orig_func)
Expand Down
20 changes: 10 additions & 10 deletions mypyc/test-data/irbuild-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -2930,7 +2930,7 @@ L0:
r0.g = r2; r4 = is_error
r5 = r0.g
return r5
def __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj.__get__(__mypyc_self__, instance, owner):
def d_c_obj.__get__(__mypyc_self__, instance, owner):
__mypyc_self__, instance, owner, r0 :: object
r1 :: bit
r2 :: object
Expand All @@ -2943,9 +2943,9 @@ L1:
L2:
r2 = PyMethod_New(__mypyc_self__, instance)
return r2
def __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj.__call__(__mypyc_self__):
__mypyc_self__ :: __main__.__mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj
r0 :: __main__.__mypyc_c_decorator_helper___env
def d_c_obj.__call__(__mypyc_self__):
__mypyc_self__ :: __main__.d_c_obj
r0 :: __main__.c_env
r1, d :: object
r2 :: str
r3 :: object
Expand All @@ -2961,9 +2961,9 @@ L0:
r5 = CPyObject_GetAttr(r3, r4)
r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
return 1
def __mypyc_c_decorator_helper__():
r0 :: __main__.__mypyc_c_decorator_helper___env
r1 :: __main__.__mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj
def c():
r0 :: __main__.c_env
r1 :: __main__.d_c_obj
r2 :: bool
r3 :: dict
r4 :: str
Expand All @@ -2981,8 +2981,8 @@ def __mypyc_c_decorator_helper__():
r18 :: str
r19, r20, r21, r22 :: object
L0:
r0 = __mypyc_c_decorator_helper___env()
r1 = __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj()
r0 = c_env()
r1 = d_c_obj()
r1.__mypyc_env__ = r0; r2 = is_error
r3 = __main__.globals :: static
r4 = 'b'
Expand Down Expand Up @@ -3062,7 +3062,7 @@ L2:
r17 = CPyDict_SetItem(r5, r16, r15)
r18 = r17 >= 0 :: signed
r19 = __main__.globals :: static
r20 = '__mypyc_c_decorator_helper__'
r20 = 'c'
r21 = CPyDict_GetItem(r19, r20)
r22 = __main__.globals :: static
r23 = 'b'
Expand Down
9 changes: 9 additions & 0 deletions mypyc/test-data/run-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,12 @@ with assertRaises(TypeError, "varargs4() missing required keyword-only argument
varargs4(1, 2, 3)
with assertRaises(TypeError, "varargs4() missing required argument 'a' (pos 1)"):
varargs4(y=20)

[case testDecoratorName]
def dec(f): return f

@dec
def foo(): pass

def test_decorator_name():
assert foo.__name__ == "foo"

0 comments on commit 26673be

Please sign in to comment.