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

Function generate_hash_wrapper

mypyc/codegen/emitwrapper.py:546–570  ·  view source on GitHub ↗

Generates a wrapper for native __hash__ methods.

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

Source from the content-addressed store, hash-verified

544
545
546def generate_hash_wrapper(cl: ClassIR, fn: FuncIR, emitter: Emitter) -> str:
547 """Generates a wrapper for native __hash__ methods."""
548 name = f"{DUNDER_PREFIX}{fn.name}{cl.name_prefix(emitter.names)}"
549 emitter.emit_line(f"static Py_ssize_t {name}(PyObject *self) {{")
550 emitter.emit_line(
551 "{}retval = {}{}{}(self);".format(
552 emitter.ctype_spaced(fn.ret_type),
553 emitter.get_group_prefix(fn.decl),
554 NATIVE_PREFIX,
555 fn.cname(emitter.names),
556 )
557 )
558 emitter.emit_error_check("retval", fn.ret_type, "return -1;")
559 if is_int_rprimitive(fn.ret_type):
560 emitter.emit_line("Py_ssize_t val = CPyTagged_AsSsize_t(retval);")
561 else:
562 emitter.emit_line("Py_ssize_t val = PyLong_AsSsize_t(retval);")
563 emitter.emit_dec_ref("retval", fn.ret_type)
564 emitter.emit_line("if (PyErr_Occurred()) return -1;")
565 # We can't return -1 from a hash function..
566 emitter.emit_line("if (val == -1) return -2;")
567 emitter.emit_line("return val;")
568 emitter.emit_line("}")
569
570 return name
571
572
573def generate_len_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…