Opened 8 years ago

Closed 8 years ago

#4607 closed (fixed)

Django is always using Python 2.3 fallback methods

Reported by: SmileyChris Owned by: adrian
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 SmileyChris 8 years ago.
fallback_try_except.patch (6.2 KB) - added by SmileyChris 8 years ago.

Download all attachments as: .zip

Change History (10)

comment:1 Changed 8 years ago by SmileyChris

  • 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 8 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 8 years ago by mtredinnick

  • Triage Stage changed from Unreviewed to Accepted

Changed 8 years ago by SmileyChris

comment:4 Changed 8 years ago by SmileyChris

  • Has patch set
  • Triage Stage changed from Accepted to Ready 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 8 years ago by ubernostrum

Out of curiosity, wouldn't this be cleaner:

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

comment:6 Changed 8 years ago by adrian

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

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

Changed 8 years ago by SmileyChris

comment:7 Changed 8 years ago by SmileyChris

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

comment:8 Changed 8 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(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