Skip to content

Commit 9fc5d6d

Browse files
committed
test: Make tracemalloc optional in httpclient_test
pypy does not provide the tracemalloc module, so importing it at the top of httpclient_test made the whole module fail to import there. Import it conditionally (following the pattern used for pycurl and other optional modules) and skip the three memory-measuring tests when it is missing. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011PBzMWughm8fUFZDTrd8QF
1 parent 555a2ee commit 9fc5d6d

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎tornado/test/httpclient_test.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import subprocess
1313
import sys
1414
import time
15-
import tracemalloc
1615
import typing # noqa: F401
1716
import unicodedata
1817
import unittest
@@ -37,6 +36,16 @@
3736
from tornado.web import Application, RequestHandler, url
3837
from tornado.httputil import format_timestamp, HTTPHeaders
3938

39+
try:
40+
import tracemalloc
41+
except ImportError:
42+
# Not available on pypy.
43+
tracemalloc = None # type: ignore
44+
45+
skipIfNoTracemalloc = unittest.skipIf(
46+
tracemalloc is None, "tracemalloc module not present"
47+
)
48+
4049

4150
class HelloWorldHandler(RequestHandler):
4251
def get(self):
@@ -341,6 +350,7 @@ def test_streaming_callback_large_body(self):
341350
self.assertEqual(b"".join(chunks), large_body())
342351
self.assertFalse(response.body)
343352

353+
@skipIfNoTracemalloc
344354
def test_streaming_decompression_bomb(self):
345355
# A malicious server can turn a small compressed response into an
346356
# arbitrarily large decompressed one. With a streaming_callback it
@@ -366,6 +376,7 @@ def streaming_callback(chunk):
366376
self.assertFalse(response.body)
367377
self.assertLess(peak, 16 * 1024 * 1024)
368378

379+
@skipIfNoTracemalloc
369380
def test_decompression_bomb_without_streaming_callback(self):
370381
# Without a streaming_callback the whole body has to be buffered, so
371382
# a bomb this size cannot be retrieved at all. It has to be refused
@@ -390,6 +401,7 @@ def test_decompression_bomb_without_streaming_callback(self):
390401
# was refused, not accumulated.
391402
self.assertLess(peak, UNBUFFERABLE_BOMB_SIZE // 2)
392403

404+
@skipIfNoTracemalloc
393405
def test_streaming_unsolicited_brotli_bomb(self):
394406
# A client that decodes an encoding it did not request can be
395407
# reached by a codec whose expansion ratio is hundreds of times

0 commit comments

Comments
 (0)