code: def try_simplify_traceback(
      tb: TracebackType,
  ) -> Optional[TracebackType]:
      """
      Simplify the traceback. It removes the tracebacks in the current package, and only
      shows the traceback that is related to the thirdparty and user-specified codes.
  
      Returns
      -------
      TracebackType or None
        Simplified traceback instance. It returns None if it fails to simplify.
  
      Notes
      -----
      This keeps the tracebacks once it sees they are from a different file even
      though the following tracebacks are from the current package.
  
      Examples
      --------
      >>> import importlib
      >>> import sys
      >>> import traceback
      >>> import tempfile
      >>> with tempfile.TemporaryDirectory() as tmp_dir:
      ...     with open("%s/dummy_module.py" % tmp_dir, "w") as f:
      ...         _ = f.write(
      ...             'def raise_stop_iteration():\\n'
      ...             '    raise StopIteration()\\n\\n'
      ...             'def simple_wrapper(f):\\n'
      ...             '    def wrapper(*a, **k):\\n'
      ...             '        return f(*a, **k)\\n'
      ...             '    return wrapper\\n')
      ...         f.flush()
      ...         spec = importlib.util.spec_from_file_location(
      ...             "dummy_module", "%s/dummy_module.py" % tmp_dir)
      ...         dummy_module = importlib.util.module_from_spec(spec)
      ...         spec.loader.exec_module(dummy_module)
      >>> def skip_doctest_traceback(tb):
      ...     import pyspark
      ...     root = os.path.dirname(pyspark.__file__)
      ...     pairs = zip(walk_tb(tb), traceback.extract_tb(tb))
      ...     for cur_tb, cur_frame in pairs:
      ...         if cur_frame.filename.startswith(root):
      ...             return cur_tb
  
      Regular exceptions should show the file name of the current package as below.
  
      >>> exc_info = None
      >>> try:
      ...     fail_on_stopiteration(dummy_module.raise_stop_iteration)()
      ... except Exception as e:
      ...     tb = sys.exc_info()[-1]
      ...     e.__cause__ = None
      ...     exc_info = "".join(
      ...         traceback.format_exception(type(e), e, tb))
      >>> print(exc_info)  # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
      Traceback (most recent call last):
        File ...
          ...
        File "/.../pyspark/util.py", line ...
          ...
      pyspark.errors.exceptions.base.PySparkRuntimeError: ...
      >>> "pyspark/util.py" in exc_info
      True
  
      If the traceback is simplified with this method, it hides the current package file name:
  
      >>> exc_info = None
      >>> try:
      ...     fail_on_stopiteration(dummy_module.raise_stop_iteration)()
      ... except Exception as e:
      ...     tb = try_simplify_traceback(sys.exc_info()[-1])
      ...     e.__cause__ = None
      ...     exc_info = "".join(
      ...         traceback.format_exception(
      ...             type(e), e, try_simplify_traceback(skip_doctest_traceback(tb))))
      >>> print(exc_info)  # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
      pyspark.errors.exceptions.base.PySparkRuntimeError: ...
      >>> "pyspark/util.py" in exc_info
      False
  
      In the case below, the traceback contains the current package in the middle.
      In this case, it just hides the top occurrence only.
  
      >>> exc_info = None
      >>> try:
      ...     fail_on_stopiteration(dummy_module.simple_wrapper(
      ...         fail_on_stopiteration(dummy_module.raise_stop_iteration)))()
      ... except Exception as e:
      ...     tb = sys.exc_info()[-1]
      ...     e.__cause__ = None
      ...     exc_info_a = "".join(
      ...         traceback.format_exception(type(e), e, tb))
      ...     exc_info_b = "".join(
      ...         traceback.format_exception(
      ...             type(e), e, try_simplify_traceback(skip_doctest_traceback(tb))))
      >>> exc_info_a.count("pyspark/util.py")
      2
      >>> exc_info_b.count("pyspark/util.py")
      1
      """
      if "pypy" in platform.python_implementation().lower():
          # Traceback modification is not supported with PyPy in PySpark.
          return None
      if sys.version_info[:2] < (3, 7):
          # Traceback creation is not supported Python < 3.7.
          # See https://bugs.python.org/issue30579.
          return None
  
      import pyspark
  
      root = os.path.dirname(pyspark.__file__)
      tb_next = None
      new_tb = None
      pairs = zip(walk_tb(tb), traceback.extract_tb(tb))
      last_seen = []
  
      for cur_tb, cur_frame in pairs:
          if not cur_frame.filename.startswith(root):
              # Filter the stacktrace from the PySpark source itself.
              last_seen = [(cur_tb, cur_frame)]
              break
  
      for cur_tb, cur_frame in reversed(
          list(itertools.chain(last_seen, pairs))
      ):
          # Once we have seen the file names outside, don't skip.
          new_tb = TracebackType(
              tb_next=tb_next,
              tb_frame=cur_tb.tb_frame,
              tb_lasti=cur_tb.tb_frame.f_lasti,
              tb_lineno=cur_tb.tb_frame.f_lineno
              if cur_tb.tb_frame.f_lineno is not None
              else -1,
          )
          tb_next = new_tb
      return new_tb
  
imports: import copy
  import functools
  import itertools
  import os
  import platform
  import re
  import sys
  import threading
  import traceback
  import typing
  from types import TracebackType
  from typing import Any, Callable, IO, Iterator, List, Optional, TextIO, Tuple, Union
  from py4j.clientserver import ClientServer
  from pyspark.errors import PySparkRuntimeError
  from py4j.java_gateway import JavaObject
label_desc: Use image node_id 4 for calling a global function with example usage: try_simplify_traceback(tb) and returns: new_tb, None, None