Opened 9 years ago

Closed 9 years ago

#4607 closed (fixed)

Django is always using Python 2.3 fallback methods

Reported by: Chris Beaven Owned by: Adrian Holovaty
Component: Core (Other) Version: master
Severity: Keywords: performance
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Throughout Django, the following way is used to fall back import the Python 2.3 methods if the Python 2.4+ builtin methods set and reversed do not exist:

if hasattr(__builtins__, 'set'):
    ...

But Django is converting __builtins__ to a dict somehow, when it's usually a module (try >>> type(__builtins__) from a manage.py shell then from a normal Python shell), and a dict doesn't have the attribute set (or reversed).

Attachments (2)

fallback_plus_new.patch (6.6 KB) - added by Chris Beaven 9 years ago.
fallback_try_except.patch (6.2 KB) - added by Chris Beaven 9 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 9 years ago by Chris Beaven

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Thanks to the IRC team, I have a bit more of an understanding now.

`__builtins__` can either be a module or dict -- we shouldn't be relying on it being a module. Instead we should be doing:

import __builtin__
if hasattr(__builtin__, 'set'):
    ...

comment:2 Changed 9 years ago by anonymous

For clarity; it appears to be not Django that makes Python modify __builtins__ to a dict, but code.py used for the standard python interactive prompt.

People using IPython are having a module and not a Dict for __builtins__, still if Python can arbitrarily change the definition of builtins from a module to a Dict depending on circumstances it makes sense to code this in a way that is not prone to arbitrary definitions. As the linked email suggests.

comment:3 Changed 9 years ago by Malcolm Tredinnick

Triage Stage: UnreviewedAccepted

Changed 9 years ago by Chris Beaven

Attachment: fallback_plus_new.patch added

comment:4 Changed 9 years ago by Chris Beaven

Has patch: set
Triage Stage: AcceptedReady for checkin

Patch standardizes all use of this method (and does it for the contrib.auth module which was only using the old 2.3 set method)

comment:5 Changed 9 years ago by James Bennett

Out of curiosity, wouldn't this be cleaner:

try:
    set
except NameError:
    from sets import Set as set

comment:6 Changed 9 years ago by Adrian Holovaty

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

Yeah, ubuernostrum's suggestion seems more elegant. Any downsides to that?

Changed 9 years ago by Chris Beaven

Attachment: fallback_try_except.patch added

comment:7 Changed 9 years ago by Chris Beaven

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [5514]) Fixed #4607 -- Tweaked checks for features missing in Python 2.3 to not assume
things Python does not guarantee. Patch from SmileyChris.

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