Opened 18 years ago

Closed 18 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: dev
Severity: Keywords: performance
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 18 years ago.
fallback_try_except.patch (6.2 KB ) - added by Chris Beaven 18 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 by Chris Beaven, 18 years ago

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 by anonymous, 18 years ago

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 by Malcolm Tredinnick, 18 years ago

Triage Stage: UnreviewedAccepted

by Chris Beaven, 18 years ago

Attachment: fallback_plus_new.patch added

comment:4 by Chris Beaven, 18 years ago

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 by James Bennett, 18 years ago

Out of curiosity, wouldn't this be cleaner:

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

comment:6 by Adrian Holovaty, 18 years ago

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

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

by Chris Beaven, 18 years ago

Attachment: fallback_try_except.patch added

comment:7 by Chris Beaven, 18 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 by Malcolm Tredinnick, 18 years ago

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