Opened 5 years ago
Last modified 7 hours ago
#32969 assigned Cleanup/optimization
Improve pickling of HttpResponse instances
| Reported by: | zatro | Owned by: | H. White |
|---|---|---|---|
| Component: | HTTP handling | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Keryn Knight, Anvesh Mishra, Ülgen Sarıkavak | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When pickling a HTTPResponse, rather than pickling all of its attributes just store what is needed for the creation of a new HttpResponse with the correct content, status code and headers.
See related discussion on ticket #23895 and its PR.
Change History (20)
comment:1 by , 5 years ago
| Component: | Core (Serialization) → HTTP handling |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:5 by , 5 years ago
| Patch needs improvement: | unset |
|---|
comment:6 by , 5 years ago
| Cc: | added |
|---|---|
| Patch needs improvement: | set |
Tentatively setting the flag back; I've left feedback and proposed a first sketch of how adapting SimpleTemplateResponse and TemplateResponse might go.
comment:7 by , 5 years ago
| Needs tests: | set |
|---|
comment:8 by , 4 years ago
| Cc: | added |
|---|---|
| Owner: | changed from to |
comment:9 by , 4 years ago
comment:10 by , 4 years ago
| Needs tests: | unset |
|---|---|
| Patch needs improvement: | unset |
| Triage Stage: | Accepted → Ready for checkin |
comment:12 by , 4 years ago
| Type: | Bug → Cleanup/optimization |
|---|
comment:15 by , 3 years ago
| Has patch: | unset |
|---|---|
| Resolution: | fixed |
| Status: | closed → new |
| Triage Stage: | Ready for checkin → Accepted |
comment:16 by , 2 years ago
| Cc: | added |
|---|
comment:17 by , 3 weeks ago
| Owner: | removed |
|---|---|
| Status: | new → assigned |
comment:18 by , 3 weeks ago
| Status: | assigned → new |
|---|
comment:19 by , 9 days ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:20 by , 7 hours ago
| Has patch: | set |
|---|
PR, https://github.com/django/django/pull/21289
Several classes associated with HTTP handling have attributes that reference objects that can't be pickled. Previous fixes have tried removing the attributes that won't pickle, but since the attributes weren't replaced, copied or unpickled objects were missing them.
This PR adds __getstate__ and __setstate__ methods to these classes, allowing instances to serialize and deserialize properly. I see that this approach was discussed for #23895 (https://github.com/django/django/pull/14664), and rejected because the urlconf could have changed after the object was pickled. However, the unpicklable objects are only present in responses created by the test client, and the reason for their inclusion is documented.
Thanks, it's worth adding that pickling
HttpResponsewith CBV crashes:>>> import pickle >>> response = self.client.get('/cbv_view/') >>> pickle.dumps(response) ... AttributeError: Can't pickle ....