Insert a runtime check for argument and unbox if necessary. The object is named PyObject *obj_{}. This is expected to generate a value of name arg_{} (unboxed if necessary). For each primitive a runtime check ensures the correct type.
(
name: str,
typ: RType,
emitter: Emitter,
error: ErrorHandler | None = None,
*,
optional: bool = False,
raise_exception: bool = True,
bitmap_arg_index: int = 0,
)
| 770 | |
| 771 | |
| 772 | def generate_arg_check( |
| 773 | name: str, |
| 774 | typ: RType, |
| 775 | emitter: Emitter, |
| 776 | error: ErrorHandler | None = None, |
| 777 | *, |
| 778 | optional: bool = False, |
| 779 | raise_exception: bool = True, |
| 780 | bitmap_arg_index: int = 0, |
| 781 | ) -> None: |
| 782 | """Insert a runtime check for argument and unbox if necessary. |
| 783 | |
| 784 | The object is named PyObject *obj_{}. This is expected to generate |
| 785 | a value of name arg_{} (unboxed if necessary). For each primitive a runtime |
| 786 | check ensures the correct type. |
| 787 | """ |
| 788 | error = error or AssignHandler() |
| 789 | if typ.is_unboxed: |
| 790 | if typ.error_overlap and optional: |
| 791 | # Update bitmap is value is provided. |
| 792 | init = emitter.c_undefined_value(typ) |
| 793 | emitter.emit_line(f"{emitter.ctype(typ)} arg_{name} = {init};") |
| 794 | emitter.emit_line(f"if (obj_{name} != NULL) {{") |
| 795 | bitmap = bitmap_name(bitmap_arg_index // BITMAP_BITS) |
| 796 | emitter.emit_line(f"{bitmap} |= 1 << {bitmap_arg_index & (BITMAP_BITS - 1)};") |
| 797 | emitter.emit_unbox( |
| 798 | f"obj_{name}", |
| 799 | f"arg_{name}", |
| 800 | typ, |
| 801 | declare_dest=False, |
| 802 | raise_exception=raise_exception, |
| 803 | error=error, |
| 804 | borrow=True, |
| 805 | ) |
| 806 | emitter.emit_line("}") |
| 807 | else: |
| 808 | # Borrow when unboxing to avoid reference count manipulation. |
| 809 | emitter.emit_unbox( |
| 810 | f"obj_{name}", |
| 811 | f"arg_{name}", |
| 812 | typ, |
| 813 | declare_dest=True, |
| 814 | raise_exception=raise_exception, |
| 815 | error=error, |
| 816 | borrow=True, |
| 817 | optional=optional, |
| 818 | ) |
| 819 | elif is_object_rprimitive(typ): |
| 820 | # Object is trivial since any object is valid |
| 821 | if optional: |
| 822 | emitter.emit_line(f"PyObject *arg_{name};") |
| 823 | emitter.emit_line(f"if (obj_{name} == NULL) {{") |
| 824 | emitter.emit_line(f"arg_{name} = {emitter.c_error_value(typ)};") |
| 825 | emitter.emit_lines("} else {", f"arg_{name} = obj_{name}; ", "}") |
| 826 | else: |
| 827 | emitter.emit_line(f"PyObject *arg_{name} = obj_{name};") |
| 828 | else: |
| 829 | emitter.emit_cast( |
searching dependent graphs…