Comprehensive Python debugger backend for IDEs with remote debugging, breakpoints, variable inspection, and performance optimizations
—
Interactive debugging console interface that provides REPL-like functionality within debugging sessions. The console enables code evaluation, variable inspection, and interactive debugging capabilities during breakpoints and suspended execution.
Core classes for managing console commands and execution in debugging contexts.
class Command:
"""
Represents a console command for interactive debugging.
Attributes:
- command_id: Unique identifier for the command
- command_text: The actual command/code to execute
- frame_id: Frame identifier for execution context
"""
def __init__(self, command_id, command_text, frame_id): ...Classes for managing standard input during debugging sessions to enable interactive input/output.
class BaseStdIn:
"""
Base class for stdin handling during debugging.
"""
def readline(self, size=-1): ...
def read(self, size=-1): ...
def close(): ...
class StdIn(BaseStdIn):
"""
Standard input handler for debugging sessions.
"""
def __init__(self, original_stdin, encoding): ...
class DebugConsoleStdIn(BaseStdIn):
"""
Debug console specific input handler.
"""
def __init__(self, py_db): ...Classes for handling code fragment execution and evaluation within debugging contexts.
class CodeFragment:
"""
Represents a code fragment for execution in debugging console.
Attributes:
- text: Code text to execute
- is_single_line: Whether this is a single line expression
"""
def __init__(self, text, is_single_line=False): ...
class BaseInterpreterInterface:
"""
Base interface for interpreter interaction during debugging.
"""
def add_exec(self, code_fragment): ...
def get_namespace(self): ...
def close(): ...
class FakeFrame:
"""
Mock frame object for console evaluation when no real frame available.
"""
def __init__(self, locals_dict, globals_dict): ...import pydevconsole
# Create a console command for evaluation
command = pydevconsole.Command(
command_id=1,
command_text="x = 42; print(f'Value is {x}')",
frame_id="frame_1"
)
# Command will be executed in the debugging context
print(f"Executing command: {command.command_text}")import sys
from pydevconsole import DebugConsoleStdIn
# Example of setting up debug console input
# (This would typically be done by the debugger internally)
class MockPyDB:
def __init__(self):
self.cmd_factory = None
mock_db = MockPyDB()
debug_stdin = DebugConsoleStdIn(mock_db)
# Replace standard input during debugging
original_stdin = sys.stdin
sys.stdin = debug_stdin
try:
# Interactive input will now go through debug console
user_input = input("Enter a value: ")
print(f"You entered: {user_input}")
finally:
# Restore original stdin
sys.stdin = original_stdin
debug_stdin.close()from pydevconsole import CodeFragment, FakeFrame
# Create code fragments for evaluation
single_line = CodeFragment("x + y", is_single_line=True)
multi_line = CodeFragment("""
result = []
for i in range(5):
result.append(i * 2)
print(result)
""", is_single_line=False)
# Create execution context with local variables
locals_dict = {"x": 10, "y": 20}
globals_dict = {"__builtins__": __builtins__}
fake_frame = FakeFrame(locals_dict, globals_dict)
print(f"Single line fragment: {single_line.text}")
print(f"Multi-line fragment: {multi_line.text}")
print(f"Is single line: {single_line.is_single_line}")import pydevd
from pydevconsole import BaseInterpreterInterface, CodeFragment
class DebugInterpreter(BaseInterpreterInterface):
def __init__(self, locals_dict, globals_dict):
self.locals = locals_dict
self.globals = globals_dict
def add_exec(self, code_fragment):
"""Execute code fragment in debugging context."""
try:
if code_fragment.is_single_line:
# Evaluate expression
result = eval(code_fragment.text, self.globals, self.locals)
return result
else:
# Execute statements
exec(code_fragment.text, self.globals, self.locals)
return None
except Exception as e:
return f"Error: {e}"
def get_namespace(self):
return self.locals
def close(self):
pass
# Example usage during debugging breakpoint
def debug_function():
x = 42
y = "hello"
z = [1, 2, 3, 4, 5]
# At this point, debugger would create interpreter
locals_dict = locals()
globals_dict = globals()
interpreter = DebugInterpreter(locals_dict, globals_dict)
# Simulate console commands
commands = [
CodeFragment("x", is_single_line=True),
CodeFragment("len(z)", is_single_line=True),
CodeFragment("new_var = x * 2", is_single_line=False),
CodeFragment("print(f'{y} world, x={x}, new_var={new_var}')", is_single_line=False)
]
for cmd in commands:
result = interpreter.add_exec(cmd)
if result is not None:
print(f">>> {cmd.text}")
print(f"Result: {result}")
else:
print(f">>> {cmd.text}")
print("Executed")
interpreter.close()
# Run the example
debug_function()import sys
import threading
from pydevconsole import StdIn, CodeFragment
# Example of integrating with existing application
class DebugConsoleManager:
def __init__(self):
self.original_stdin = sys.stdin
self.debug_stdin = None
self.console_thread = None
self.active = False
def start_console(self, encoding='utf-8'):
"""Start debug console with custom stdin handling."""
self.debug_stdin = StdIn(self.original_stdin, encoding)
sys.stdin = self.debug_stdin
self.active = True
# Start console thread
self.console_thread = threading.Thread(target=self._console_loop)
self.console_thread.daemon = True
self.console_thread.start()
def stop_console(self):
"""Stop debug console and restore stdin."""
self.active = False
if self.console_thread:
self.console_thread.join(timeout=1.0)
if self.debug_stdin:
self.debug_stdin.close()
sys.stdin = self.original_stdin
def _console_loop(self):
"""Console input loop (simplified example)."""
while self.active:
try:
line = sys.stdin.readline()
if not line or line.strip() == 'quit':
break
# Create and process command
fragment = CodeFragment(line.strip(), is_single_line=True)
print(f"Processing: {fragment.text}")
except EOFError:
break
except Exception as e:
print(f"Console error: {e}")
# Usage example
console_manager = DebugConsoleManager()
try:
console_manager.start_console()
# Simulate application running
import time
print("Debug console started. Type commands or 'quit' to exit.")
# In real scenario, application would continue running
time.sleep(10)
finally:
console_manager.stop_console()
print("Debug console stopped.")Install with Tessl CLI
npx tessl i tessl/pypi-pydevd