Skip to content

Commit 26673be

Browse files
authored
[mypyc] Remove decorator_helper_name (#11022)
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.
1 parent a72fab3 commit 26673be

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

mypyc/common.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@
7070
JsonDict = Dict[str, Any]
7171

7272

73-
def decorator_helper_name(func_name: str) -> str:
74-
return '__mypyc_{}_decorator_helper__'.format(func_name)
75-
76-
7773
def shared_lib_name(group_name: str) -> str:
7874
"""Given a group name, return the actual name of its extension module.
7975

mypyc/irbuild/context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from mypyc.ir.ops import Value, BasicBlock
88
from mypyc.ir.func_ir import INVALID_FUNC_DEF
99
from mypyc.ir.class_ir import ClassIR
10-
from mypyc.common import decorator_helper_name
1110
from mypyc.irbuild.targets import AssignmentTarget
1211

1312

@@ -24,7 +23,7 @@ def __init__(self,
2423
is_decorated: bool = False,
2524
in_non_ext: bool = False) -> None:
2625
self.fitem = fitem
27-
self.name = name if not is_decorated else decorator_helper_name(name)
26+
self.name = name
2827
self.class_name = class_name
2928
self.ns = namespace
3029
# Callable classes implement the '__call__' method, and are used to represent functions

mypyc/irbuild/function.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
check_stop_op, yield_from_except_op, coro_op, send_op, register_function
3838
)
3939
from mypyc.primitives.dict_ops import dict_set_item_op, dict_new_op, dict_get_method_with_none
40-
from mypyc.common import SELF_NAME, LAMBDA_NAME, decorator_helper_name
40+
from mypyc.common import SELF_NAME, LAMBDA_NAME
4141
from mypyc.sametype import is_same_method_signature
4242
from mypyc.irbuild.util import is_constant
4343
from mypyc.irbuild.context import FuncInfo, ImplicitClass
@@ -100,10 +100,9 @@ def transform_decorator(builder: IRBuilder, dec: Decorator) -> None:
100100
elif dec.func in builder.fdefs_to_decorators:
101101
# Obtain the the function name in order to construct the name of the helper function.
102102
name = dec.func.fullname.split('.')[-1]
103-
helper_name = decorator_helper_name(name)
104103

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

109108
if decorated_func is not None:
@@ -333,8 +332,7 @@ def c() -> None:
333332
builder.functions.append(func_ir)
334333
# create the dispatch function
335334
assert isinstance(fitem, FuncDef)
336-
dispatch_name = decorator_helper_name(name) if is_decorated else name
337-
return gen_dispatch_func_ir(builder, fitem, fn_info.name, dispatch_name, sig)
335+
return gen_dispatch_func_ir(builder, fitem, fn_info.name, name, sig)
338336

339337
return func_ir, func_reg
340338

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

393390
# Decorate the non-decorated method
394391
decorated_func = load_decorated_func(builder, fdef, orig_func)

mypyc/test-data/irbuild-basic.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,7 +2930,7 @@ L0:
29302930
r0.g = r2; r4 = is_error
29312931
r5 = r0.g
29322932
return r5
2933-
def __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj.__get__(__mypyc_self__, instance, owner):
2933+
def d_c_obj.__get__(__mypyc_self__, instance, owner):
29342934
__mypyc_self__, instance, owner, r0 :: object
29352935
r1 :: bit
29362936
r2 :: object
@@ -2943,9 +2943,9 @@ L1:
29432943
L2:
29442944
r2 = PyMethod_New(__mypyc_self__, instance)
29452945
return r2
2946-
def __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj.__call__(__mypyc_self__):
2947-
__mypyc_self__ :: __main__.__mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj
2948-
r0 :: __main__.__mypyc_c_decorator_helper___env
2946+
def d_c_obj.__call__(__mypyc_self__):
2947+
__mypyc_self__ :: __main__.d_c_obj
2948+
r0 :: __main__.c_env
29492949
r1, d :: object
29502950
r2 :: str
29512951
r3 :: object
@@ -2961,9 +2961,9 @@ L0:
29612961
r5 = CPyObject_GetAttr(r3, r4)
29622962
r6 = PyObject_CallFunctionObjArgs(r5, r2, 0)
29632963
return 1
2964-
def __mypyc_c_decorator_helper__():
2965-
r0 :: __main__.__mypyc_c_decorator_helper___env
2966-
r1 :: __main__.__mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj
2964+
def c():
2965+
r0 :: __main__.c_env
2966+
r1 :: __main__.d_c_obj
29672967
r2 :: bool
29682968
r3 :: dict
29692969
r4 :: str
@@ -2981,8 +2981,8 @@ def __mypyc_c_decorator_helper__():
29812981
r18 :: str
29822982
r19, r20, r21, r22 :: object
29832983
L0:
2984-
r0 = __mypyc_c_decorator_helper___env()
2985-
r1 = __mypyc_d_decorator_helper_____mypyc_c_decorator_helper___obj()
2984+
r0 = c_env()
2985+
r1 = d_c_obj()
29862986
r1.__mypyc_env__ = r0; r2 = is_error
29872987
r3 = __main__.globals :: static
29882988
r4 = 'b'
@@ -3062,7 +3062,7 @@ L2:
30623062
r17 = CPyDict_SetItem(r5, r16, r15)
30633063
r18 = r17 >= 0 :: signed
30643064
r19 = __main__.globals :: static
3065-
r20 = '__mypyc_c_decorator_helper__'
3065+
r20 = 'c'
30663066
r21 = CPyDict_GetItem(r19, r20)
30673067
r22 = __main__.globals :: static
30683068
r23 = 'b'

mypyc/test-data/run-functions.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,3 +1183,12 @@ with assertRaises(TypeError, "varargs4() missing required keyword-only argument
11831183
varargs4(1, 2, 3)
11841184
with assertRaises(TypeError, "varargs4() missing required argument 'a' (pos 1)"):
11851185
varargs4(y=20)
1186+
1187+
[case testDecoratorName]
1188+
def dec(f): return f
1189+
1190+
@dec
1191+
def foo(): pass
1192+
1193+
def test_decorator_name():
1194+
assert foo.__name__ == "foo"

0 commit comments

Comments
 (0)