MCPcopy Index your code
hub / github.com/python/mypy / add_coroutine_properties

Function add_coroutine_properties

mypyc/irbuild/callable_class.py:94–142  ·  view source on GitHub ↗

Adds properties to the class to make it look like a regular python function. Needed to make introspection functions like inspect.iscoroutinefunction work.

(
    builder: IRBuilder, callable_class_ir: ClassIR, coroutine_name: str
)

Source from the content-addressed store, hash-verified

92
93
94def add_coroutine_properties(
95 builder: IRBuilder, callable_class_ir: ClassIR, coroutine_name: str
96) -> None:
97 """Adds properties to the class to make it look like a regular python function.
98 Needed to make introspection functions like inspect.iscoroutinefunction work.
99 """
100 callable_class_ir.coroutine_name = coroutine_name
101 callable_class_ir.attributes[CPYFUNCTION_NAME] = object_rprimitive
102
103 properties = {
104 "__name__": cpyfunction_get_name,
105 "__code__": cpyfunction_get_code,
106 "__annotations__": cpyfunction_get_annotations,
107 "__defaults__": cpyfunction_get_defaults,
108 "__kwdefaults__": cpyfunction_get_kwdefaults,
109 }
110
111 writable_props = {
112 "__name__": cpyfunction_set_name,
113 "__annotations__": cpyfunction_set_annotations,
114 }
115
116 line = builder.fn_info.fitem.line
117
118 def get_func_wrapper() -> Value:
119 return builder.add(GetAttr(builder.self(), CPYFUNCTION_NAME, line))
120
121 for name, primitive in properties.items():
122 with builder.enter_method(callable_class_ir, name, object_rprimitive, internal=True):
123 func = get_func_wrapper()
124 val = builder.primitive_op(primitive, [func, Integer(0, c_pointer_rprimitive)], line)
125 builder.add(Return(val))
126
127 for name, primitive in writable_props.items():
128 with builder.enter_method(
129 callable_class_ir, f"{PROPSET_PREFIX}{name}", int_rprimitive, internal=True
130 ):
131 value = builder.add_argument("value", object_rprimitive)
132 func = get_func_wrapper()
133 rv = builder.primitive_op(
134 primitive, [func, value, Integer(0, c_pointer_rprimitive)], line
135 )
136 builder.add(Return(rv))
137
138 for name in properties:
139 getter = callable_class_ir.get_method(name)
140 assert getter
141 setter = callable_class_ir.get_method(f"{PROPSET_PREFIX}{name}")
142 callable_class_ir.properties[name] = (getter, setter)
143
144
145def add_call_to_callable_class(

Callers 1

setup_callable_classFunction · 0.85

Calls 9

IntegerClass · 0.90
ReturnClass · 0.90
get_func_wrapperFunction · 0.85
add_argumentMethod · 0.80
itemsMethod · 0.45
enter_methodMethod · 0.45
primitive_opMethod · 0.45
addMethod · 0.45
get_methodMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…