Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Identify the cell number
  • Loading branch information
itholic committed Jun 18, 2024
commit 3b628250b3f2e86d16c06c7d0589df0178e036cf
30 changes: 19 additions & 11 deletions python/pyspark/errors/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,37 @@ def _capture_call_site(
in the user code that led to the error.
"""
stack = list(reversed(inspect.stack()))
ipython = None

# We try import here since IPython is not a required dependency
try:
from IPython import get_ipython

if get_ipython():
import pyspark

# Filtering out PySpark code and keeping user code only
pyspark_root = os.path.dirname(pyspark.__file__)
stack = [
frame_info
for frame_info in inspect.stack()
if pyspark_root not in frame_info.filename
]
ipython = get_ipython()
except ImportError:
pass

if ipython:
import pyspark

# Filtering out PySpark code and keeping user code only
pyspark_root = os.path.dirname(pyspark.__file__)
stack = [
frame_info for frame_info in inspect.stack() if pyspark_root not in frame_info.filename
]

depth = int(
spark_session.conf.get("spark.sql.stackTracesInDataFrameContext") # type: ignore[arg-type]
)
selected_frames = stack[:depth]
call_sites = [f"{frame.filename}:{frame.lineno}" for frame in selected_frames]

# Identifying the cell is useful when the error is generated from IPython Notebook
if ipython:
call_sites = [
f"line {frame.lineno} in cell [{ipython.execution_count}]" for frame in selected_frames
]
else:
call_sites = [f"{frame.filename}:{frame.lineno}" for frame in selected_frames]
call_sites_str = "\n".join(call_sites)

pyspark_origin.set(fragment, call_sites_str)
Expand Down