Skip to content

Commit

Permalink
Merge pull request #471 from snoack/metaclass-improvement
Browse files Browse the repository at this point in the history
Improve with_metaclass()
  • Loading branch information
untitaker committed Aug 6, 2015
2 parents 6bf78bc + 743598d commit 57c288c
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions jinja2/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,14 @@ def encode_filename(filename):


def with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
# This requires a bit of explanation: the basic idea is to make a
# dummy metaclass for one level of class instanciation that replaces
# itself with the actual metaclass. Because of internal type checks
# we also need to make sure that we downgrade the custom metaclass
# for one level to something closer to type (that's why __call__ and
# __init__ comes back from type etc.).
#
# This has the advantage over six.with_metaclass in that it does not
# introduce dummy classes into the final MRO.
class metaclass(meta):
__call__ = type.__call__
__init__ = type.__init__
# dummy metaclass for one level of class instantiation that replaces
# itself with the actual metaclass.
class metaclass(type):
def __new__(cls, name, this_bases, d):
if this_bases is None:
return type.__new__(cls, name, (), d)
return meta(name, bases, d)
return metaclass('temporary_class', None, {})
return type.__new__(metaclass, 'temporary_class', (), {})


try:
Expand Down

0 comments on commit 57c288c

Please sign in to comment.