Skip to content

gh-68443: Replace debug level-related logic in http client with logging #8633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(test_httplib.py): Fix logging in tests
  • Loading branch information
CuriousLearner committed Aug 7, 2018
commit 230784db63752f5112c2894274b0c0c1dfcf808e
12 changes: 8 additions & 4 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ def begin(self):

self.headers = self.msg = parse_headers(self.fp)

if _log.isEnabledFor(logging.DEBUG):
if _log.isEnabledFor(logging.INFO):
for hdr in self.headers:
_log.debug("Received header: ('%s': '%s')", hdr, self.headers[hdr])
_log.info("Received header: ('%s': '%s')", hdr, self.headers[hdr])

# are we using the chunked-style of transfer encoding?
tr_enc = self.headers.get("transfer-encoding")
Expand Down Expand Up @@ -1065,7 +1065,7 @@ def _send_output(self, message_body=None, encode_chunked=False):
for chunk in chunks:
if not chunk:
if self.debuglevel > 0:
print('Zero length chunk ignored')
_log.info('Zero length chunk ignored')
continue

if encode_chunked and self._http_vsn == 11:
Expand Down Expand Up @@ -1334,7 +1334,11 @@ def getresponse(self):
if self.__state != _CS_REQ_SENT or self.__response:
raise ResponseNotReady(self.__state)

response = self.response_class(self.sock, method=self._method)
if self.debuglevel > 0:
response = self.response_class(
self.sock, self.debuglevel, method=self._method)
else:
response = self.response_class(self.sock, method=self._method)

try:
try:
Expand Down
16 changes: 13 additions & 3 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io
import itertools
import os
import sys
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't being used anywhere!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good catch. I think it remained here while I was working on this.

import logging
import array
import socket
import threading
Expand Down Expand Up @@ -352,12 +354,16 @@ def test_headers_debuglevel(self):
)
sock = FakeSocket(body)
resp = client.HTTPResponse(sock, debuglevel=1)
logging.basicConfig(level=logging.INFO)
with support.captured_stdout() as output:
output_handler = logging.StreamHandler(output)
_logger = logging.getLogger('http.client')
_logger.addHandler(output_handler)
resp.begin()
lines = output.getvalue().splitlines()
self.assertEqual(lines[0], "reply: 'HTTP/1.1 200 OK\\r\\n'")
self.assertEqual(lines[1], "header: First: val")
self.assertEqual(lines[2], "header: Second: val")
self.assertEqual(lines[0], "Received response: 200 OK")
self.assertEqual(lines[1], "Received header: ('First': 'val')")
self.assertEqual(lines[2], "Received header: ('Second': 'val')")


class TransferEncodingTest(TestCase):
Expand Down Expand Up @@ -1928,7 +1934,11 @@ def test_tunnel_debuglog(self):
self.conn._create_connection = self._create_connection(response_text)
self.conn.set_tunnel('destination.com')

logging.basicConfig(level=logging.INFO)
with support.captured_stdout() as output:
output_handler = logging.StreamHandler(output)
_logger = logging.getLogger('http.client')
_logger.addHandler(output_handler)
self.conn.request('PUT', '/', '')
lines = output.getvalue().splitlines()
self.assertIn('header: {}'.format(expected_header), lines)
Expand Down