class_docstr: Introspect UDF bytecode and determine if we can rewrite as native expression.
class_docstr_tok: ["Introspect","UDF","bytecode","and","determine","if","we","can","rewrite","as","native","expression","."]
docstr: Inject nesting boundaries into expression blocks (as parentheses).
code: def _inject_nesting(
      self,
      expression_blocks: dict[int, str],
      logical_instructions: list[Instruction],
  ) -> list[tuple[int, str]]:
      """Inject nesting boundaries into expression blocks (as parentheses)."""
      if logical_instructions:
          # reconstruct nesting boundaries for mixed and/or ops by associating control
          # flow jump offsets with their target expression blocks and applying parens
          if len({inst.opname for inst in logical_instructions}) > 1:
              block_offsets: list[int] = list(expression_blocks.keys())
              prev_end = -1
              for inst in logical_instructions:
                  start = block_offsets[
                      bisect_left(block_offsets, inst.offset) - 1
                  ]
                  end = block_offsets[
                      bisect_left(block_offsets, inst.argval) - 1
                  ]
                  if not (start == 0 and end == block_offsets[-1]):
                      if prev_end not in (start, end):
                          expression_blocks[start] = (
                              "(" + expression_blocks[start]
                          )
                          expression_blocks[end] += ")"
                          prev_end = end
  
          for (
              inst
          ) in (
              logical_instructions
          ):  # inject connecting "&" and "|" ops
              expression_blocks[inst.offset] = OpNames.CONTROL_FLOW[
                  inst.opname
              ]
  
      return sorted(expression_blocks.items())
  
imports: from __future__ import annotations
  import datetime
  import dis
  import inspect
  import re
  import sys
  import warnings
  from bisect import bisect_left
  from collections import defaultdict
  from dis import get_instructions
  from inspect import signature
  from itertools import count, zip_longest
  from pathlib import Path
  from typing import TYPE_CHECKING, AbstractSet, Any, Callable, ClassVar, Iterator, Literal, NamedTuple, Union
label_desc: Use image node_id 3 for calling the BytecodeParser obj's underlying member method code with example usage: obj._inject_nesting(expression_blocks, logical_instructions) and returns: sorted