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 Mariusz Felisiak, 5 years ago

Component: Core (Serialization)HTTP handling
Triage Stage: UnreviewedAccepted

Thanks, it's worth adding that pickling HttpResponse with CBV crashes:

>>> import pickle
>>> response = self.client.get('/cbv_view/')
>>> pickle.dumps(response)
...
AttributeError: Can't pickle ....

comment:2 by tigicion, 5 years ago

Owner: changed from nobody to tigicion
Status: newassigned

comment:4 by Mariusz Felisiak, 5 years ago

Patch needs improvement: set

I agree with Keryn's comment.

comment:5 by tigicion, 5 years ago

Patch needs improvement: unset

comment:6 by Keryn Knight, 5 years ago

Cc: Keryn Knight 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 Mariusz Felisiak, 5 years ago

Needs tests: set

comment:8 by Anvesh Mishra, 4 years ago

Cc: Anvesh Mishra added
Owner: changed from tigicion to Anvesh Mishra

comment:9 by Anvesh Mishra, 4 years ago

Submitted the PR with all the suggested changes according to Keryn and Felix's comments on PR-14728

comment:10 by Mariusz Felisiak, 4 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In d7f5bfd:

Fixed #32969 -- Fixed pickling HttpResponse and subclasses.

comment:12 by Mariusz Felisiak, 4 years ago

Type: BugCleanup/optimization

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 173034b0:

Refs #34482 -- Reverted "Fixed #32969 -- Fixed pickling HttpResponse and subclasses."

This reverts commit d7f5bfd241666c0a76e90208da1e9ef81aec44db.

Thanks Márton Salomváry for the report.

comment:14 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 791407f:

[4.2.x] Refs #34482 -- Reverted "Fixed #32969 -- Fixed pickling HttpResponse and subclasses."

This reverts commit d7f5bfd241666c0a76e90208da1e9ef81aec44db.

Thanks Márton Salomváry for the report.

Backport of 173034b00589c083793d495e8b07e35be2cb1cf0 from main

comment:15 by Mariusz Felisiak, 3 years ago

Has patch: unset
Resolution: fixed
Status: closednew
Triage Stage: Ready for checkinAccepted

comment:16 by Ülgen Sarıkavak, 2 years ago

Cc: Ülgen Sarıkavak added

comment:17 by Jacob Walls, 3 weeks ago

Owner: Anvesh Mishra removed
Status: newassigned

comment:18 by Jacob Walls, 3 weeks ago

Status: assignednew

comment:19 by H. White, 9 days ago

Owner: set to H. White
Status: newassigned

comment:20 by H. White, 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.

Note: See TracTickets for help on using tickets.
Back to Top