Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/requests/cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ def morsel_to_cookie(morsel):
if morsel["max-age"]:
try:
expires = int(time.time() + int(morsel["max-age"]))
except ValueError:
raise TypeError(f"max-age: {morsel['max-age']} must be integer")
except ValueError as exc:
raise TypeError(f"max-age: {morsel['max-age']} must be integer") from exc
elif morsel["expires"]:
time_template = "%a, %d-%b-%Y %H:%M:%S GMT"
expires = calendar.timegm(time.strptime(morsel["expires"], time_template))
Expand Down
16 changes: 16 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,22 @@ def test_max_age_invalid_str(self):
with pytest.raises(TypeError):
morsel_to_cookie(morsel)

def test_max_age_exception_chaining(self):
"""Test that exception chaining is preserved when max-age conversion fails."""

morsel = Morsel()
morsel["max-age"] = "invalid"
with pytest.raises(TypeError) as exc_info:
morsel_to_cookie(morsel)

# Verify the TypeError was raised
assert "max-age: invalid must be integer" in str(exc_info.value)

# Verify the original ValueError is preserved as the cause
assert exc_info.value.__cause__ is not None
assert isinstance(exc_info.value.__cause__, ValueError)
assert "invalid literal for int()" in str(exc_info.value.__cause__)


class TestTimeout:
def test_stream_timeout(self, httpbin):
Expand Down