44PY2 = int (sys .version [0 ]) == 2
55
66if PY2 :
7- def b (s ):
8- return s
9- def u (s ):
10- return unicode (s , "unicode_escape" )
117 from itertools import imap , izip
128 import urllib2 as request
139 from urllib import quote as urlquote
@@ -27,12 +23,7 @@ def implements_to_string(cls):
2723 cls .__unicode__ = cls .__str__
2824 cls .__str__ = lambda x : x .__unicode__ ().encode ('utf-8' )
2925 return cls
30-
3126else : # PY3
32- def b (s ):
33- return s .encode ("latin-1" )
34- def u (s ):
35- return s
3627 from urllib import request
3728 from urllib .parse import quote as urlquote
3829 text_type = str
@@ -47,15 +38,21 @@ def u(s):
4738 implements_to_string = lambda x : x
4839
4940
50- def add_metaclass (metaclass ):
51- """Class decorator for creating a class with a metaclass.
52- From the six library.
53- """
54- def wrapper (cls ):
55- orig_vars = cls .__dict__ .copy ()
56- orig_vars .pop ('__dict__' , None )
57- orig_vars .pop ('__weakref__' , None )
58- for slots_var in orig_vars .get ('__slots__' , ()):
59- orig_vars .pop (slots_var )
60- return metaclass (cls .__name__ , cls .__bases__ , orig_vars )
61- return wrapper
41+ def with_metaclass (meta , * bases ):
42+ '''Defines a metaclass.
43+
44+ Creates a dummy class with a dummy metaclass. When subclassed, the dummy
45+ metaclass is used, which has a constructor that instantiates a
46+ new class from the original parent. This ensures that the dummy class and
47+ dummy metaclass are not in the inheritance tree.
48+
49+ Credit to Armin Ronacher.
50+ '''
51+ class metaclass (meta ):
52+ __call__ = type .__call__
53+ __init__ = type .__init__
54+ def __new__ (cls , name , this_bases , d ):
55+ if this_bases is None :
56+ return type .__new__ (cls , name , (), d )
57+ return meta (name , bases , d )
58+ return metaclass ('temporary_class' , None , {})
0 commit comments