Opened 13 years ago

Closed 12 years ago

#15754 closed Cleanup/optimization (fixed)

Сalling _media method many times while getting media value

Reported by: sakkada Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords: media_property
Cc: sakkada, marcoberi Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

File django.forms.widgets.py, function media_property, subfunction _media:

if hasattr(super(cls, self), 'media'):
    base = super(cls, self).media
else:
    base = Media()

"media" attribute of class "cls" is created in the metaclass MediaDefiningClass from method "_media" decorated with "property", so when you call hasattr(super(cls, self), 'media'), "_media" also calling (specifics of the "property"). That's why "_media" method (in property) calling many times, and the number of calls is growing exponentially as the number of ancestor classes. To prevent it, exclude 'hasattr' calls from code. Possible solution is

base = getattr(super(cls, self), 'media', None) or Media()

or

try:
    base = super(cls, self).media
except AttributeError:
    base = Media()

IMHO, first solution better than using try/except.

Attachments (1)

widgets.diff (634 bytes ) - added by sakkada 13 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by sakkada, 13 years ago

milestone: 1.3
Version: 1.3SVN

comment:2 by sakkada, 13 years ago

Cc: sakkada added

by sakkada, 13 years ago

Attachment: widgets.diff added

comment:3 by Jacob, 13 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted

comment:4 by Julien Phalip, 13 years ago

Needs tests: set

comment:5 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:6 by marcoberi, 12 years ago

Cc: marcoberi added

comment:7 by Alex Gaynor <alex.gaynor@…>, 12 years ago

Resolution: fixed
Status: newclosed

In [ede49c7ee03dd1519d0c375d953cb73e106837b6]:

Fixed #15754 -- avoid recursively computing the tree of media widgets more times than is necessary for a wiget

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