From ca123b0760de38b4df50ca321927a365d2d1c796 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 20 Feb 2008 02:00:20 +0000 Subject: [PATCH] queryset-refactor: Fixed a couple of FIXME items. These are mostly code cleanups, although now we also install the right type of class for our exception sublasses on models in Python 2.5. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7137 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 31 +++++++++++++++++++++---------- django/db/models/options.py | 11 +++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index ed4dac6f05..139fc3d8df 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -34,18 +34,15 @@ class ModelBase(type): return super(ModelBase, cls).__new__(cls, name, bases, attrs) # Create the class. - new_class = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')}) + module = attrs.pop('__module__') meta = attrs.pop('Meta', None) - # FIXME: Promote Meta to a newstyle class before attaching it to the - # model. - ## if meta: - ## new_class.Meta = meta + new_class = type.__new__(cls, name, bases, {'__module__': module}) new_class.add_to_class('_meta', Options(meta)) - # FIXME: Need to be smarter here. Exception is an old-style class in - # Python <= 2.4, new-style in Python 2.5+. This construction is only - # really correct for old-style classes. - new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {})) - new_class.add_to_class('MultipleObjectsReturned', types.ClassType('MultipleObjectsReturned', (MultipleObjectsReturned, ), {})) + new_class.add_to_class('DoesNotExist', + subclass_exception('DoesNotExist', ObjectDoesNotExist, module)) + new_class.add_to_class('MultipleObjectsReturned', + subclass_exception('MultipleObjectsReturned', + MultipleObjectsReturned, module)) # Do the appropriate setup for any model parents. abstract_parents = [] @@ -488,3 +485,17 @@ def method_get_order(ordered_obj, self): def get_absolute_url(opts, func, self, *args, **kwargs): return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs) + +######## +# MISC # +######## + +if sys.version_info < (2, 5): + # Prior to Python 2.5, Exception was an old-style class + def subclass_exception(name, parent, unused): + return types.ClassType(name, (parent,), {}) + +else: + def subclass_exception(name, parent, module): + return type(name, (parent,), {'__module__': module}) + diff --git a/django/db/models/options.py b/django/db/models/options.py index 8c369f98a6..05b1d52f8d 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -1,5 +1,9 @@ import re from bisect import bisect +try: + set +except NameError: + from sets import Set as set # Python 2.3 fallback from django.conf import settings from django.db.models.related import RelatedObject @@ -407,11 +411,10 @@ class Options(object): Returns a list of all the ancestor of this model as a list. Useful for determining if something is an ancestor, regardless of lineage. """ - # FIXME: Fix model hashing and then use a Set here. - result = [] + result = set() for parent in self.parents: - result.append(parent) - result.extend(parent._meta.get_parent_list()) + result.add(parent) + result.update(parent._meta.get_parent_list()) return result def get_ordered_objects(self):