#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018cb0414b6d-0e9cece1-48d3-43e7-934e-22580ae9bef2-000000%40eu-central-1.amazonses.com.

Reply via email to