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

Function generate_richcompare_wrapper

mypyc/codegen/emitwrapper.py:501–528  ·  view source on GitHub ↗

Generates a wrapper for richcompare dunder methods.

(cl: ClassIR, emitter: Emitter)

Source from the content-addressed store, hash-verified

499
500
501def generate_richcompare_wrapper(cl: ClassIR, emitter: Emitter) -> str | None:
502 """Generates a wrapper for richcompare dunder methods."""
503 # Sort for determinism on Python 3.5
504 matches = sorted(name for name in RICHCOMPARE_OPS if cl.has_method(name))
505 if not matches:
506 return None
507
508 name = f"{DUNDER_PREFIX}_RichCompare_{cl.name_prefix(emitter.names)}"
509 emitter.emit_line(
510 "static PyObject *{name}(PyObject *obj_lhs, PyObject *obj_rhs, int op) {{".format(
511 name=name
512 )
513 )
514 emitter.emit_line("switch (op) {")
515 for func in matches:
516 emitter.emit_line(f"case {RICHCOMPARE_OPS[func]}: {{")
517 method = cl.get_method(func)
518 assert method is not None
519 generate_wrapper_core(method, emitter, arg_names=["lhs", "rhs"])
520 emitter.emit_line("}")
521 emitter.emit_line("}")
522
523 emitter.emit_line("Py_INCREF(Py_NotImplemented);")
524 emitter.emit_line("return Py_NotImplemented;")
525
526 emitter.emit_line("}")
527
528 return name
529
530
531def generate_get_wrapper(cl: ClassIR, fn: FuncIR, emitter: Emitter) -> str:

Callers 1

generate_classFunction · 0.90

Calls 7

sortedFunction · 0.85
generate_wrapper_coreFunction · 0.85
has_methodMethod · 0.80
name_prefixMethod · 0.80
emit_lineMethod · 0.45
formatMethod · 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…