Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/py-polars/debug/launch.py
6939 views
1
import os
2
import re
3
import sys
4
import time
5
from pathlib import Path
6
7
"""
8
The following parameter determines the sleep time of the Python process after a signal
9
is sent that attaches the Rust LLDB debugger. If the Rust LLDB debugger attaches to the
10
current session too late, it might miss any set breakpoints. If this happens
11
consistently, it is recommended to increase this value.
12
"""
13
LLDB_DEBUG_WAIT_TIME_SECONDS = 1
14
15
16
def launch_debugging() -> None:
17
"""
18
Debug Rust files via Python.
19
20
Determine the pID for the current debugging session, attach the Rust LLDB launcher,
21
and execute the originally-requested script.
22
"""
23
if len(sys.argv) == 1:
24
msg = (
25
"launch.py is not meant to be executed directly; please use the `Python: "
26
"Debug Rust` debugging configuration to run a python script that uses the "
27
"polars library."
28
)
29
raise RuntimeError(msg)
30
31
# Get the current process ID.
32
pID = os.getpid()
33
34
# Print to the debug console to allow VSCode to pick up on the signal and start the
35
# Rust LLDB configuration automatically.
36
launch_file = Path(__file__).parents[2] / ".vscode/launch.json"
37
if not launch_file.exists():
38
msg = f"Cannot locate {launch_file}"
39
raise RuntimeError(msg)
40
with launch_file.open("r") as f:
41
launch_info = f.read()
42
43
# Overwrite the pid found in launch.json with the pid for the current process.
44
# Match the initial "Rust LLDB" definition with the pid defined immediately after.
45
pattern = re.compile('("Rust LLDB",\\s*"pid":\\s*")\\d+(")')
46
found = pattern.search(launch_info)
47
if not found:
48
msg = (
49
"Cannot locate pid definition in launch.json for Rust LLDB configuration. "
50
"Please follow the instructions in the debugging section of the "
51
"contributing guide (https://docs.pola.rs/development/contributing/ide/#debugging) "
52
"for creating the launch configuration."
53
)
54
raise RuntimeError(msg)
55
56
launch_info_with_new_pid = pattern.sub(rf"\g<1>{pID}\g<2>", launch_info)
57
with launch_file.open("w") as f:
58
f.write(launch_info_with_new_pid)
59
60
# Print pID to the debug console. This auto-triggers the Rust LLDB configurations.
61
print(f"pID = {pID}")
62
63
# Give the LLDB time to connect. Depending on how long it takes for your LLDB
64
# debugging session to initialize, you may have to adjust this setting.
65
time.sleep(LLDB_DEBUG_WAIT_TIME_SECONDS)
66
67
# Update sys.argv so that when exec() is called, the first argument is the script
68
# name itself, and the remaining are the input arguments.
69
sys.argv.pop(0)
70
with Path(sys.argv[0]).open() as fh:
71
script_contents = fh.read()
72
73
# Run the originally requested file by reading in the script, compiling, and
74
# executing the code.
75
file_to_execute = Path(sys.argv[0])
76
exec(
77
compile(script_contents, file_to_execute, mode="exec"), {"__name__": "__main__"}
78
)
79
80
81
if __name__ == "__main__":
82
launch_debugging()
83
84