<commit_before>import copy
import sys
from functools import update_wrapper
from itertools import izip

import django.db.models.manager     # Imported to register signal handler.
from django.conf import settings
from django.core.exceptions import (ObjectDoesNotExist,
    MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS)
from django.core import validators
from django.db.models.fields import AutoField, IntegerField, FieldDoesNotExist
from django.db.models.fields.related import (ManyToOneRel,
    OneToOneField, add_lazy_relation)
from django.db import (connections, router, transaction, DatabaseError,
    DEFAULT_DB_ALIAS)
from django.db.models.query import Q
from django.db.models.query_utils import DeferredAttribute
from django.db.models.deletion import Collector
from django.db.models.options import Options
from django.db.models import signals
from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _
from django.utils.functional import curry
from django.utils.encoding import smart_str, force_unicode
from django.utils.text import get_text_list, capfirst


class ModelBase(type):
    """
    Metaclass for all models.
    """
    def __new__(cls, name, bases, attrs):
        super_new = super(ModelBase, cls).__new__
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return super_new(cls, name, bases, attrs)

        # Create the class.
        module = attrs.pop('__module__')
        new_class = super_new(cls, name, bases, {'__module__': module})
        attr_meta = attrs.pop('Meta', None)
        abstract = getattr(attr_meta, 'abstract', False)
        mat_view = getattr(attr_meta, 'materialized_view', False)
        leaf = False
        if mat_view and abstract:
            raise TypeError("A model cannot be 'abstract' and 'materialized_view' at the same time.")
        if not attr_meta:
            meta = getattr(new_class, 'Meta', None)
        else:
            meta = attr_meta
        base_meta = getattr(new_class, '_meta', None)

        if getattr(meta, 'app_label', None) is None:
            # Figure out the app_label by looking one level up.
            # For 'django.contrib.sites.models', this would be 'sites'.
            model_module = sys.modules[new_class.__module__]
            kwargs = {"app_label": model_module.__name__.split('.')[-2]}
        else:
            kwargs = {}

        new_class.add_to_class('_meta', Options(meta, **kwargs))
        if not abstract:
            new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist',
                    tuple(x.DoesNotExist
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (ObjectDoesNotExist,), module))
            new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned',
                    tuple(x.MultipleObjectsReturned
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (MultipleObjectsReturned,), module))
            if base_meta:
                if base_meta.abstract:
                    # Non-abstract child classes inherit some attributes from their
                    # non-abstract parent (unless an ABC comes before it in the
                    # method resolution order).
                    if not hasattr(meta, 'ordering'):
                        new_class._meta.ordering = base_meta.ordering
                    if not hasattr(meta, 'get_latest_by'):
                        new_class._meta.get_latest_by = base_meta.get_latest_by
                if base_meta.materialized_view or base_meta.leaf or (
                        base_meta.db_view and base_meta.intermediate):
                    if mat_view :
                        raise TypeError('A materialized view cannot inherit from another')
                    new_class._meta.leaf = True
                    leaf = True
                    # register the materialized_view base
                    new_class._meta.mat_view_base = base_meta.concrete_model
                if base_meta.leaf:
                    mat_view_base = base_meta.mat_view_base
                    new_class._meta.mat_view_base = mat_view_base<commit_msg>issue #2: regression in abstract inheritance<commit_after>import copy
import sys
from functools import update_wrapper
from itertools import izip

import django.db.models.manager     # Imported to register signal handler.
from django.conf import settings
from django.core.exceptions import (ObjectDoesNotExist,
    MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS)
from django.core import validators
from django.db.models.fields import AutoField, IntegerField, FieldDoesNotExist
from django.db.models.fields.related import (ManyToOneRel,
    OneToOneField, add_lazy_relation)
from django.db import (connections, router, transaction, DatabaseError,
    DEFAULT_DB_ALIAS)
from django.db.models.query import Q
from django.db.models.query_utils import DeferredAttribute
from django.db.models.deletion import Collector
from django.db.models.options import Options
from django.db.models import signals
from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _
from django.utils.functional import curry
from django.utils.encoding import smart_str, force_unicode
from django.utils.text import get_text_list, capfirst


class ModelBase(type):
    """
    Metaclass for all models.
    """
    def __new__(cls, name, bases, attrs):
        super_new = super(ModelBase, cls).__new__
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return super_new(cls, name, bases, attrs)

        # Create the class.
        module = attrs.pop('__module__')
        new_class = super_new(cls, name, bases, {'__module__': module})
        attr_meta = attrs.pop('Meta', None)
        abstract = getattr(attr_meta, 'abstract', False)
        mat_view = getattr(attr_meta, 'materialized_view', False)
        leaf = False
        if mat_view and abstract:
            raise TypeError("A model cannot be 'abstract' and 'materialized_view' at the same time.")
        if not attr_meta:
            meta = getattr(new_class, 'Meta', None)
        else:
            meta = attr_meta
        base_meta = getattr(new_class, '_meta', None)

        if getattr(meta, 'app_label', None) is None:
            # Figure out the app_label by looking one level up.
            # For 'django.contrib.sites.models', this would be 'sites'.
            model_module = sys.modules[new_class.__module__]
            kwargs = {"app_label": model_module.__name__.split('.')[-2]}
        else:
            kwargs = {}

        new_class.add_to_class('_meta', Options(meta, **kwargs))

        if base_meta:
            if base_meta.abstract:
                # Non-abstract child classes inherit some attributes from their
                # non-abstract parent (unless an ABC comes before it in the
                # method resolution order).
                if not hasattr(meta, 'ordering'):
                    new_class._meta.ordering = base_meta.ordering
                if not hasattr(meta, 'get_latest_by'):
                    new_class._meta.get_latest_by = base_meta.get_latest_by

        if not abstract:
            new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist',
                    tuple(x.DoesNotExist
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (ObjectDoesNotExist,), module))
            new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned',
                    tuple(x.MultipleObjectsReturned
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (MultipleObjectsReturned,), module))
            if base_meta:
                if base_meta.materialized_view or base_meta.leaf or (
                        base_meta.db_view and base_meta.intermediate):
                    if mat_view :
                        raise TypeError('A materialized view cannot inherit from another')
                    new_class._meta.leaf = True
                    leaf = True
                    # register the materialized_view base
                    new_class._meta.mat_view_base = base_meta.concrete_model
                if base_meta.leaf:
                    mat_view_base = base_meta.mat_view_base
                    new_class._meta.mat_view_base = mat_view_base
{'fraction_non_alphanumeric': 0.07604178366842637, 'fraction_numerical': 0.000336965067954622, 'mean_word_length': 2.319910514541387, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2192799', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2324, 'n_words': 674}