Skip to content

Commit c9309d0

Browse files
committed
Fix pickling of httpclient.HTTPError subclasses and web.HTTPError
The `args` member variable is set by `BaseException.__new__` and used by `BaseException.__reduce__` for pickling. To avoid interfering with it, we need to avoid calling `BaseException.__init__` from classes that have subclasses with incompatible constructors, and rename our own `tornado.web.HTTPError.args` member. >>> pickle.loads(pickle.dumps(tornado.simple_httpclient.HTTPTimeoutError("message"))) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: HTTPTimeoutError.__init__() takes 2 positional arguments but 4 were given >>> str(pickle.loads(pickle.dumps(tornado.web.HTTPError(500, "%s", "foo")))) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/anders/python/tornado/tornado/web.py", line 2488, in __str__ return message + " (" + (self.log_message % self.args) + ")" ~~~~~~~~~~~~~~~~~^~~~~~~~~~~ TypeError: not enough arguments for format string Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 2a0e1d1 commit c9309d0

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

Diff for: tornado/httpclient.py

-1
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,6 @@ def __init__(
717717
self.code = code
718718
self.message = message or httputil.responses.get(code, "Unknown")
719719
self.response = response
720-
super().__init__(code, message, response)
721720

722721
def __str__(self) -> str:
723722
return "HTTP %d: %s" % (self.code, self.message)

Diff for: tornado/web.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ def log_exception(
18691869
if isinstance(value, HTTPError):
18701870
if value.log_message:
18711871
format = "%d %s: " + value.log_message
1872-
args = [value.status_code, self._request_summary()] + list(value.args)
1872+
args = [value.status_code, self._request_summary()] + list(value.log_args)
18731873
gen_log.warning(format, *args)
18741874
else:
18751875
app_log.error(
@@ -2474,7 +2474,7 @@ def __init__(
24742474
) -> None:
24752475
self.status_code = status_code
24762476
self.log_message = log_message
2477-
self.args = args
2477+
self.log_args = args
24782478
self.reason = kwargs.get("reason", None)
24792479
if log_message and not args:
24802480
self.log_message = log_message.replace("%", "%%")
@@ -2485,7 +2485,7 @@ def __str__(self) -> str:
24852485
self.reason or httputil.responses.get(self.status_code, "Unknown"),
24862486
)
24872487
if self.log_message:
2488-
return message + " (" + (self.log_message % self.args) + ")"
2488+
return message + " (" + (self.log_message % self.log_args) + ")"
24892489
else:
24902490
return message
24912491

0 commit comments

Comments
 (0)