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

Method emit_union_cast

mypyc/codegen/emit.py:902–938  ·  view source on GitHub ↗

Emit cast to a union type. The arguments are similar to emit_cast.

(
        self,
        src: str,
        dest: str,
        typ: RUnion,
        declare_dest: bool,
        error: ErrorHandler,
        optional: bool,
        src_type: RType | None,
        raise_exception: bool,
    )

Source from the content-addressed store, hash-verified

900 self.emit_line("return %s;" % error.value)
901
902 def emit_union_cast(
903 self,
904 src: str,
905 dest: str,
906 typ: RUnion,
907 declare_dest: bool,
908 error: ErrorHandler,
909 optional: bool,
910 src_type: RType | None,
911 raise_exception: bool,
912 ) -> None:
913 """Emit cast to a union type.
914
915 The arguments are similar to emit_cast.
916 """
917 if declare_dest:
918 self.emit_line(f"PyObject *{dest};")
919 good_label = self.new_label()
920 if optional:
921 self.emit_line(f"if ({src} == NULL) {{")
922 self.emit_line(f"{dest} = {self.c_error_value(typ)};")
923 self.emit_line(f"goto {good_label};")
924 self.emit_line("}")
925 for item in typ.items:
926 self.emit_cast(
927 src,
928 dest,
929 item,
930 declare_dest=False,
931 raise_exception=False,
932 optional=False,
933 likely=False,
934 )
935 self.emit_line(f"if ({dest} != NULL) goto {good_label};")
936 # Handle cast failure.
937 self.emit_cast_error_handler(error, src, dest, typ, raise_exception)
938 self.emit_label(good_label)
939
940 def emit_tuple_cast(
941 self,

Callers 1

emit_castMethod · 0.95

Calls 6

emit_lineMethod · 0.95
new_labelMethod · 0.95
c_error_valueMethod · 0.95
emit_castMethod · 0.95
emit_labelMethod · 0.95

Tested by

no test coverage detected