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)
Change History (10)
comment:1 by , 18 years ago
comment:2 by , 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 , 18 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
by , 18 years ago
| Attachment: | fallback_plus_new.patch added |
|---|
comment:4 by , 18 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Accepted → 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 by , 18 years ago
Out of curiosity, wouldn't this be cleaner:
try:
set
except NameError:
from sets import Set as set
comment:6 by , 18 years ago
| Patch needs improvement: | set |
|---|---|
| Triage Stage: | Ready for checkin → Accepted |
Yeah, ubuernostrum's suggestion seems more elegant. Any downsides to that?
by , 18 years ago
| Attachment: | fallback_try_except.patch added |
|---|
comment:7 by , 18 years ago
| Patch needs improvement: | unset |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 18 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
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: