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

Function generate_len_wrapper

mypyc/codegen/emitwrapper.py:573–595  ·  view source on GitHub ↗

Generates a wrapper for native __len__ methods.

(cl: ClassIR, fn: FuncIR, emitter: Emitter)

Source from the content-addressed store, hash-verified

571
572
573def generate_len_wrapper(cl: ClassIR, fn: FuncIR, emitter: Emitter) -> str:
574 """Generates a wrapper for native __len__ methods."""
575 name = f"{DUNDER_PREFIX}{fn.name}{cl.name_prefix(emitter.names)}"
576 emitter.emit_line(f"static Py_ssize_t {name}(PyObject *self) {{")
577 emitter.emit_line(
578 "{}retval = {}{}{}(self);".format(
579 emitter.ctype_spaced(fn.ret_type),
580 emitter.get_group_prefix(fn.decl),
581 NATIVE_PREFIX,
582 fn.cname(emitter.names),
583 )
584 )
585 emitter.emit_error_check("retval", fn.ret_type, "return -1;")
586 if is_int_rprimitive(fn.ret_type):
587 emitter.emit_line("Py_ssize_t val = CPyTagged_AsSsize_t(retval);")
588 else:
589 emitter.emit_line("Py_ssize_t val = PyLong_AsSsize_t(retval);")
590 emitter.emit_dec_ref("retval", fn.ret_type)
591 emitter.emit_line("if (PyErr_Occurred()) return -1;")
592 emitter.emit_line("return val;")
593 emitter.emit_line("}")
594
595 return name
596
597
598def generate_bool_wrapper(cl: ClassIR, fn: FuncIR, emitter: Emitter) -> str:

Callers

nothing calls this directly

Calls 9

is_int_rprimitiveFunction · 0.90
name_prefixMethod · 0.80
ctype_spacedMethod · 0.80
get_group_prefixMethod · 0.80
emit_error_checkMethod · 0.80
emit_lineMethod · 0.45
formatMethod · 0.45
cnameMethod · 0.45
emit_dec_refMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…