1
+ # -*- coding: utf-8 -*-
2
+ from __future__ import unicode_literals
3
+
4
+ """
5
+ This code is a part of django.utils.six on https://github.com/django/django/blob/stable/2.2.x/django/utils/six.py removed form Django 3.0
6
+ To keep the backward compatibility between python 2 and 3 the decorator need to be used as well, during the time we find a proper way to
7
+ handle MetaClass overwright working on both versions (or dropping python 2 support).
8
+ """
9
+
10
+ def with_metaclass (meta , * bases ):
11
+ """Create a base class with a metaclass."""
12
+ # This requires a bit of explanation: the basic idea is to make a dummy
13
+ # metaclass for one level of class instantiation that replaces itself with
14
+ # the actual metaclass.
15
+ class metaclass (meta ):
16
+
17
+ def __new__ (cls , name , this_bases , d ):
18
+ return meta (name , bases , d )
19
+ return type .__new__ (metaclass , 'temporary_class' , (), {})
20
+
21
+
22
+ def add_metaclass (metaclass ):
23
+ """Class decorator for creating a class with a metaclass."""
24
+ def wrapper (cls ):
25
+ orig_vars = cls .__dict__ .copy ()
26
+ slots = orig_vars .get ('__slots__' )
27
+ if slots is not None :
28
+ if isinstance (slots , str ):
29
+ slots = [slots ]
30
+ for slots_var in slots :
31
+ orig_vars .pop (slots_var )
32
+ orig_vars .pop ('__dict__' , None )
33
+ orig_vars .pop ('__weakref__' , None )
34
+ return metaclass (cls .__name__ , cls .__bases__ , orig_vars )
35
+ return wrapper
0 commit comments