MCPcopy Index your code
hub / github.com/kubernetes-client/python / iter_resp_lines

Function iter_resp_lines

kubernetes/base/watch/watch.py:47–76  ·  view source on GitHub ↗
(resp)

Source from the content-addressed store, hash-verified

45
46
47def iter_resp_lines(resp):
48 buffer = bytearray()
49 for segment in resp.stream(amt=None, decode_content=False):
50
51 # Append the segment (chunk) to the buffer
52 #
53 # Performance note: depending on contents of buffer and the type+value of segment,
54 # encoding segment into the buffer could be a wasteful step. The approach used here
55 # simplifies the logic farther down, but in the future it may be reasonable to
56 # sacrifice readability for performance.
57 if isinstance(segment, bytes):
58 buffer.extend(segment)
59 elif isinstance(segment, str):
60 buffer.extend(segment.encode("utf-8"))
61 else:
62 raise TypeError(
63 f"Received invalid segment type, {type(segment)}, from stream. Accepts only 'str' or 'bytes'.")
64
65 # Split by newline (safe for utf-8 because multi-byte sequences cannot contain the newline byte)
66 next_newline = buffer.find(b'\n')
67 while next_newline != -1:
68 # Convert bytes to a valid utf-8 string, replacing any invalid utf-8 with the '�' character
69 line = buffer[:next_newline].decode(
70 "utf-8", errors="replace")
71 buffer = buffer[next_newline+1:]
72 if line:
73 yield line
74 else:
75 yield '' # Only print one empty line
76 next_newline = buffer.find(b'\n')
77
78
79class Watch:

Callers 1

streamMethod · 0.85

Calls 1

streamMethod · 0.80

Tested by

no test coverage detected