code: def __log_thunk_trace(value, handler: io.TextIOWrapper) -> None:
      """
      Log PyTensor's diagnostic stack trace for an exception.
  
      Uses custom attributes that are added to trace objects by raise_with_op.
      """
  
      def write(msg):
          print(f"log_thunk_trace: {msg.strip()}", file=handler)
  
      if hasattr(value, "__thunk_trace__"):
          trace2 = value.__thunk_trace__
          write("There was a problem executing an Op.")
          if trace2 is None:
              write("Could not find where this Op was defined.")
              write(
                  " * You might have instantiated this Op "
                  "directly instead of using a constructor."
              )
              write(
                  " * The Op you constructed might have been"
                  " optimized. Try turning off optimizations."
              )
          elif trace2:
              write("Definition in: ")
              for line in traceback.format_list(trace2):
                  write(line)
              write(
                  "For the full definition stack trace set"
                  " the PyTensor flags `traceback__limit` to -1"
              )
  
imports: import builtins
  import io
  import re
  import sys
  import traceback
  import warnings
  from collections import Counter, defaultdict
  from collections.abc import Iterable, Sequence
  from keyword import iskeyword
  from operator import itemgetter
  from tempfile import NamedTemporaryFile
  from textwrap import dedent, indent
  from typing import TYPE_CHECKING, Any, Callable, NoReturn, Optional, TextIO, TypeVar, Union, cast
  import numpy
  from pytensor import config, utils
  from pytensor.graph.basic import Apply, Constant, Variable
  from pytensor.graph.fg import FunctionGraph
label_desc: Use image node_id 5 for calling a global function with example usage: __log_thunk_trace(value, handler) without return types