MCPcopy Index your code
hub / github.com/python/cpython / MockSocketFile

Class MockSocketFile

Lib/test/test_remote_pdb.py:37–76  ·  view source on GitHub ↗

Mock socket file for testing _PdbServer without actual socket connections.

Source from the content-addressed store, hash-verified

35
36
37class MockSocketFile:
38 """Mock socket file for testing _PdbServer without actual socket connections."""
39
40 def __init__(self):
41 self.input_queue = []
42 self.output_buffer = []
43
44 def write(self, data: bytes) -> None:
45 """Simulate write to socket."""
46 self.output_buffer.append(data)
47
48 def flush(self) -> None:
49 """No-op flush implementation."""
50 pass
51
52 def readline(self) -> bytes:
53 """Read a line from the prepared input queue."""
54 if not self.input_queue:
55 return b""
56 return self.input_queue.pop(0)
57
58 def close(self) -> None:
59 """Close the mock socket file."""
60 pass
61
62 def add_input(self, data: dict) -> None:
63 """Add input that will be returned by readline."""
64 self.input_queue.append(json.dumps(data).encode() + b"\n")
65
66 def get_output(self) -> List[dict]:
67 """Get the output that was written by the object being tested."""
68 results = []
69 for data in self.output_buffer:
70 if isinstance(data, bytes) and data.endswith(b"\n"):
71 try:
72 results.append(json.loads(data.decode().strip()))
73 except json.JSONDecodeError:
74 pass # Ignore non-JSON output
75 self.output_buffer = []
76 return results
77
78
79class PdbClientTestCase(unittest.TestCase):

Callers 1

setUpMethod · 0.85

Calls

no outgoing calls

Tested by 1

setUpMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…