def operator(ctx: cairo.Context, op: cairo.Operator): prev_op = ctx.get_operator() ctx.set_operator(op) try: yield finally: ctx.set_operator(prev_op)