#34484: HttpRequest.__deepcopy__ doesn't deepcopy attributes
---------------------------------+--------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: new
Component: HTTP handling | Version: 4.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Adam Johnson):
I think there are a couple of issues in the original patch, which is why
we have two tickets:
(1) `__getstate__` completely drops attributes, so unpickled request
objects end up missing attributes, hence #34482. Dropping attributes
entirely in `__getstate__` should only be done where the interface of the
class works with a missing private attribute, otherwise unpickling is
broken.
(2) `__deepcopy__` performs a shallow copy of the object, hence this
ticket. It looks like this was added after
[https://github.com/django/django/pull/15937#issuecomment-1217486491 this
comment from Mariusz] during review, in order to fix test failures. I
think in reality the failures were exposing problems in the `__getstate__`
implementation, and adding a `__deepcopy__` only papered over them.
I reproduced the failure Mariusz talked about in that comment, by removing
`__deepcopy__`:
{{{
======================================================================
ERROR: test_password_reset_change_view
(auth_tests.test_templates.AuthTemplateTests.test_password_reset_change_view)
----------------------------------------------------------------------
Traceback (most recent call last):
...
File
"/Users/chainz/Documents/Projects/django/tests/auth_tests/test_templates.py",
line 115, in test_password_reset_change_view
response =
PasswordChangeView.as_view(success_url="dummy/")(self.request)
^^^^^^^^^^^^^^^^^
...
csrf_secret = request.COOKIES[settings.CSRF_COOKIE_NAME]
^^^^^^^^^^^^^^^^^
File
"/Users/chainz/Documents/Projects/django/django/utils/functional.py", line
57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
^^^^^^^^^^^^^^^^^
File
"/Users/chainz/Documents/Projects/django/django/core/handlers/wsgi.py",
line 111, in COOKIES
raw_cookie = get_str_from_wsgi(self.environ, "HTTP_COOKIE", "")
^^^^^^^^^^^^^^^^^
AttributeError: 'WSGIRequest' object has no attribute 'environ'
}}}
A missing attribute is exactly the problem as in (1).
I lean towards reverting the patch, and then working on a new one. This
stuff is hard to get right, and it would be better to work on it calmly
rather than rushing through a fix. I'll also note the patch also missed
tests for `WSGIRequest` and `ASGIRequest`.
--
Ticket URL: <https://code.djangoproject.com/ticket/34484#comment:2>
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/0107018770d20805-b955b5c2-da1c-4fd3-b667-92b5a1543a9c-000000%40eu-central-1.amazonses.com.