Skip to content

Commit ebc8448

Browse files
committed
Improve representation of frames.
Specifically this avoids cutting log messages in two lines. Ref #765.
1 parent 0779eb9 commit ebc8448

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/websockets/frames.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __str__(self) -> str:
129129
if self.opcode is OP_TEXT:
130130
# Decoding only the beginning and the end is needlessly hard.
131131
# Decode the entire payload then elide later if necessary.
132-
data = self.data.decode()
132+
data = repr(self.data.decode())
133133
elif self.opcode is OP_BINARY:
134134
# We'll show at most the first 16 bytes and the last 8 bytes.
135135
# Encode just what we need, plus two dummy bytes to elide later.
@@ -145,7 +145,7 @@ def __str__(self) -> str:
145145
# Ping and Pong frames could contain UTF-8. Attempt to decode as
146146
# UTF-8 and display it as text; fallback to binary.
147147
try:
148-
data = self.data.decode()
148+
data = repr(self.data.decode())
149149
coding = "text"
150150
except UnicodeDecodeError:
151151
binary = self.data
@@ -154,7 +154,7 @@ def __str__(self) -> str:
154154
data = " ".join(f"{byte:02x}" for byte in binary)
155155
coding = "binary"
156156
else:
157-
data = ""
157+
data = "''"
158158

159159
if len(data) > 75:
160160
data = data[:48] + "..." + data[-24:]

tests/test_frames.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class StrTests(unittest.TestCase):
198198
def test_cont_text(self):
199199
self.assertEqual(
200200
str(Frame(OP_CONT, b" cr\xc3\xa8me", fin=False)),
201-
"CONT crème [text, 7 bytes, continued]",
201+
"CONT ' crème' [text, 7 bytes, continued]",
202202
)
203203

204204
def test_cont_binary(self):
@@ -210,7 +210,7 @@ def test_cont_binary(self):
210210
def test_cont_final_text(self):
211211
self.assertEqual(
212212
str(Frame(OP_CONT, b" cr\xc3\xa8me")),
213-
"CONT crème [text, 7 bytes]",
213+
"CONT ' crème' [text, 7 bytes]",
214214
)
215215

216216
def test_cont_final_binary(self):
@@ -222,8 +222,8 @@ def test_cont_final_binary(self):
222222
def test_cont_text_truncated(self):
223223
self.assertEqual(
224224
str(Frame(OP_CONT, b"caf\xc3\xa9 " * 16, fin=False)),
225-
"CONT café café café café café café café café café caf..."
226-
"afé café café café café [text, 96 bytes, continued]",
225+
"CONT 'café café café café café café café café café ca..."
226+
" café café café café ' [text, 96 bytes, continued]",
227227
)
228228

229229
def test_cont_binary_truncated(self):
@@ -236,20 +236,26 @@ def test_cont_binary_truncated(self):
236236
def test_text(self):
237237
self.assertEqual(
238238
str(Frame(OP_TEXT, b"caf\xc3\xa9")),
239-
"TEXT café [5 bytes]",
239+
"TEXT 'café' [5 bytes]",
240240
)
241241

242242
def test_text_non_final(self):
243243
self.assertEqual(
244244
str(Frame(OP_TEXT, b"caf\xc3\xa9", fin=False)),
245-
"TEXT café [5 bytes, continued]",
245+
"TEXT 'café' [5 bytes, continued]",
246246
)
247247

248248
def test_text_truncated(self):
249249
self.assertEqual(
250250
str(Frame(OP_TEXT, b"caf\xc3\xa9 " * 16)),
251-
"TEXT café café café café café café café café café caf..."
252-
"afé café café café café [96 bytes]",
251+
"TEXT 'café café café café café café café café café ca..."
252+
"fé café café café café ' [96 bytes]",
253+
)
254+
255+
def test_text_with_newline(self):
256+
self.assertEqual(
257+
str(Frame(OP_TEXT, b"Hello\nworld!")),
258+
"TEXT 'Hello\\nworld!' [12 bytes]",
253259
)
254260

255261
def test_binary(self):
@@ -286,13 +292,19 @@ def test_close_reason(self):
286292
def test_ping(self):
287293
self.assertEqual(
288294
str(Frame(OP_PING, b"")),
289-
"PING [0 bytes]",
295+
"PING '' [0 bytes]",
290296
)
291297

292298
def test_ping_text(self):
293299
self.assertEqual(
294300
str(Frame(OP_PING, b"ping")),
295-
"PING ping [text, 4 bytes]",
301+
"PING 'ping' [text, 4 bytes]",
302+
)
303+
304+
def test_ping_text_with_newline(self):
305+
self.assertEqual(
306+
str(Frame(OP_PING, b"ping\n")),
307+
"PING 'ping\\n' [text, 5 bytes]",
296308
)
297309

298310
def test_ping_binary(self):
@@ -304,13 +316,19 @@ def test_ping_binary(self):
304316
def test_pong(self):
305317
self.assertEqual(
306318
str(Frame(OP_PONG, b"")),
307-
"PONG [0 bytes]",
319+
"PONG '' [0 bytes]",
308320
)
309321

310322
def test_pong_text(self):
311323
self.assertEqual(
312324
str(Frame(OP_PONG, b"pong")),
313-
"PONG pong [text, 4 bytes]",
325+
"PONG 'pong' [text, 4 bytes]",
326+
)
327+
328+
def test_pong_text_with_newline(self):
329+
self.assertEqual(
330+
str(Frame(OP_PONG, b"pong\n")),
331+
"PONG 'pong\\n' [text, 5 bytes]",
314332
)
315333

316334
def test_pong_binary(self):

0 commit comments

Comments
 (0)