Skip to content

Commit f345a0d

Browse files
pi-anlclaude
andcommitted
debugpy: Add MicroPython socket compatibility and VS Code test.
- Fix socket.bind() to use getaddrinfo() for MicroPython compatibility - Handle optional SO_REUSEADDR socket option gracefully - Remove unsupported getsockname() call - Fix sys.exit imports in test files - Add test_vscode.py for VS Code integration testing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 650eb9a commit f345a0d

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

python-ecosys/debugpy/debugpy/public_api.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ def listen(port=DEFAULT_PORT, host=DEFAULT_HOST):
2525

2626
# Create listening socket
2727
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28-
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
29-
listener.bind((host, port))
28+
try:
29+
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
30+
except:
31+
pass # Not supported in MicroPython
32+
33+
# Use getaddrinfo for MicroPython compatibility
34+
addr_info = socket.getaddrinfo(host, port)
35+
addr = addr_info[0][-1] # Get the sockaddr
36+
listener.bind(addr)
3037
listener.listen(1)
3138

32-
actual_host, actual_port = listener.getsockname()
33-
print(f"Debugpy listening on {actual_host}:{actual_port}")
39+
# getsockname not available in MicroPython, use original values
40+
print(f"Debugpy listening on {host}:{port}")
3441

3542
# Wait for connection
3643
try:

python-ecosys/debugpy/test_integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,5 @@ def main():
166166
return 0
167167

168168
if __name__ == "__main__":
169-
exit(main())
169+
import sys
170+
sys.exit(main())

python-ecosys/debugpy/test_simple.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,5 @@ def main():
117117
return 0 if success else 1
118118

119119
if __name__ == "__main__":
120-
exit(main())
120+
import sys
121+
sys.exit(main())

python-ecosys/debugpy/test_vscode.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
"""Test script for VS Code debugging with MicroPython debugpy."""
3+
4+
import sys
5+
sys.path.insert(0, '.')
6+
7+
import debugpy
8+
9+
def fibonacci(n):
10+
"""Calculate fibonacci number."""
11+
if n <= 1:
12+
return n
13+
return fibonacci(n-1) + fibonacci(n-2)
14+
15+
def main():
16+
print("MicroPython VS Code Debugging Test")
17+
print("==================================")
18+
19+
# Start debug server
20+
try:
21+
debugpy.listen()
22+
print("Debug server started on 127.0.0.1:5678")
23+
print("Connect VS Code debugger now...")
24+
print("Set a breakpoint on the 'result = fibonacci(num)' line")
25+
print("Press Enter to continue after connecting debugger...")
26+
try:
27+
input()
28+
except:
29+
pass
30+
31+
# Enable debugging for this thread
32+
debugpy.debug_this_thread()
33+
34+
print("\nStarting debuggable code...")
35+
36+
# Test data - set breakpoint here
37+
numbers = [3, 5, 7]
38+
for i, num in enumerate(numbers):
39+
print(f"Calculating fibonacci({num})...")
40+
result = fibonacci(num) # <-- SET BREAKPOINT HERE
41+
print(f"fibonacci({num}) = {result}")
42+
43+
# Test manual breakpoint
44+
print("\nTriggering manual breakpoint...")
45+
debugpy.breakpoint()
46+
print("Manual breakpoint triggered!")
47+
48+
print("Test completed successfully!")
49+
50+
except KeyboardInterrupt:
51+
print("\nTest interrupted by user")
52+
except Exception as e:
53+
print(f"Error: {e}")
54+
55+
if __name__ == "__main__":
56+
main()

0 commit comments

Comments
 (0)