[Django] #35066: Unsupported operand exception in response.py

25 views
Skip to first unread message

Django

unread,
Dec 28, 2023, 6:48:43 AM12/28/23
to django-...@googlegroups.com
#35066: Unsupported operand exception in response.py
-----------------------------------------+------------------------
Reporter: PaddyKe | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-----------------------------------------+------------------------
Hi, when using the [https://github.com/jazzband/django-revproxy Django-
Revproxy Plugin] I stumbled across an error when cookies are being set.

Error message:
{{{
Internal Server Error: /login
Traceback (most recent call last):
File "<projekt path>/venv/lib/python3.11/site-
packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/contrib/auth/mixins.py", line 73, in dispatch
return super().dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/revproxy/views.py", line 247, in dispatch
response = get_django_response(proxy_response,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/revproxy/response.py", line 64, in get_django_response
response.set_cookie(**cookie_dict)
File "<projekt path>/venv/lib/python3.11/site-
packages/django/http/response.py", line 264, in set_cookie
self.cookies[key]["expires"] = http_date(time.time() + max_age)
~~~~~~~~~~~~^~~~~~~~~
TypeError: unsupported operand type(s) for +: 'float' and 'str'
ERROR:django.request:Internal Server Error: /login
Traceback (most recent call last):
File "<projekt path>/venv/lib/python3.11/site-
packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args,
**callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/django/contrib/auth/mixins.py", line 73, in dispatch
return super().dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/revproxy/views.py", line 247, in dispatch
response = get_django_response(proxy_response,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<projekt path>/venv/lib/python3.11/site-
packages/revproxy/response.py", line 64, in get_django_response
response.set_cookie(**cookie_dict)
File "<projekt path>/venv/lib/python3.11/site-
packages/django/http/response.py", line 264, in set_cookie
self.cookies[key]["expires"] = http_date(time.time() + max_age)
~~~~~~~~~~~~^~~~~~~~~
TypeError: unsupported operand type(s) for +: 'float' and 'str'
}}}

----

The error gets thrown in {{{HttpResponseBase}}}-class in the
{{{set_cookie}}}-method and it should be easy to fix.
Currently, the important part of the {{{set_cookie}}}-method looks as
follows:

{{{
if max_age is not None:
if isinstance(max_age, datetime.timedelta):
max_age = max_age.total_seconds()
self.cookies[key]["max-age"] = int(max_age)
# IE requires expires, so set it if hasn't been already.
if not expires:
self.cookies[key]["expires"] = http_date(time.time() +
max_age)
}}}
\\


In the last line of this snipped (line 264 in the response.py)
{{{max_age}}} is a string and should be converted to int before adding it
to {{{time.time()}}}.
The code fix could look like this:
{{{
self.cookies[key]["expires"] = http_date(time.time() + int(max_age))
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/35066>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 28, 2023, 7:13:08 AM12/28/23
to django-...@googlegroups.com
#35066: Unsupported operand exception in response.py
-------------------------------+--------------------------------------
Reporter: PaddyKe | Owner: nobody
Type: Bug | Status: closed

Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

Thanks for this report, however `str` is not supported as `max_age` as
[https://docs.djangoproject.com/en/5.0/ref/request-
response/#django.http.HttpResponse.set_cookie documented]:

> ''`max_age` should be a `timedelta` object, an integer number of
seconds, or None''

--
Ticket URL: <https://code.djangoproject.com/ticket/35066#comment:1>

Reply all
Reply to author
Forward
0 new messages