Django

Code

Ticket #4607 (closed: fixed)

Opened 1 year ago

Last modified 1 year ago

Django is always using Python 2.3 fallback methods

Reported by: SmileyChris Assigned to: adrian
Milestone: Component: Core framework
Version: SVN Keywords: performance
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

fallback_plus_new.patch (6.6 kB) - added by SmileyChris on 06/17/07 22:06:44.
fallback_try_except.patch (6.2 kB) - added by SmileyChris on 06/20/07 05:16:01.

Change History

06/17/07 19:36:44 changed by SmileyChris

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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'):
    ...

06/17/07 19:44:20 changed 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.

06/17/07 19:52:07 changed by mtredinnick

  • stage changed from Unreviewed to Accepted.

06/17/07 22:06:44 changed by SmileyChris

  • attachment fallback_plus_new.patch added.

06/17/07 22:07:41 changed by SmileyChris

  • has_patch set to 1.
  • 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)

06/18/07 02:00:05 changed by ubernostrum

Out of curiosity, wouldn't this be cleaner:

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

06/20/07 01:06:14 changed by adrian

  • needs_better_patch set to 1.
  • stage changed from Ready for checkin to Accepted.

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

06/20/07 05:16:01 changed by SmileyChris

  • attachment fallback_try_except.patch added.

06/20/07 05:16:15 changed by SmileyChris

  • needs_better_patch deleted.
  • stage changed from Accepted to Ready for checkin.

06/22/07 22:18:23 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #4607 (Django is always using Python 2.3 fallback methods)




Change Properties
Action