def test_resolve_bases(): class A: pass class B: pass class C: def __mro_entries__(self, bases): if A in bases: return () return (A, ) c = C() assert types.resolve_bases(()) == () assert types.resolve_bases((c, )) == (A, ) assert types.resolve_bases((C, )) == (C, ) assert types.resolve_bases((A, C)) == (A, C) assert types.resolve_bases((c, A)) == (A, ) assert types.resolve_bases((A, c)) == (A, ) x = (A, ) y = (C, ) z = (A, C) t = (A, C, B) for bases in [x, y, z, t]: assert types.resolve_bases(bases) is bases
def __new__(cls, name, this_bases, d): if sys.version_info[:2] >= (3, 7): # This version introduced PEP 560 that requires a bit # of extra care (we mimic what is done by __build_class__). resolved_bases = types.resolve_bases(bases) if resolved_bases is not bases: d['__orig_bases__'] = bases else: resolved_bases = bases return meta(name, resolved_bases, d)
def __new__(cls, name, this_bases, d): if sys.version_info[:2] >= (3, 7): # whe check for python version # This version introduced PEP 560 that requires a bit # of extra care (we mimic what is done by __build_class__). resolved_bases = types.resolve_bases( bases) # some __build_class__ mimic already promised if resolved_bases is not bases: d["__orig_bases__"] = bases # update base class dictionary else: resolved_bases = bases # just update base class dictionary if python version less than 3.7 return meta_mix( name, resolved_bases, d) # return the metaclass mixing with updated class bases
def update_event(self, inp=-1): self.set_output_val(0, types.resolve_bases(self.input(0)))
def __prepare__(*args): return expected_ns B = types.new_class("B", (object, )) C = types.new_class("C", (object, ), {"metaclass": A}) meta, ns, kwds = types.prepare_class("D", (B, C), {"metaclass": type}) print(meta is A) print(ns is expected_ns) print(len(kwds) == 0) prepare_class() print(types.resolve_bases([Point, object])) print("-------------") print(types.FunctionType) print(isinstance(prepare_class, types.FunctionType)) print(types.CodeType) print(types.LambdaType) print(types.GeneratorType) print(types.CoroutineType) # 对象的类型,由 async def 函数创建。 print(types.AsyncGeneratorType) # asynchronous generator 迭代器对象的类型,由异步生成器函数创建。 print(types.MethodType) print(types.BuiltinFunctionType) print(types.BuiltinMethodType) print(types.WrapperDescriptorType) print(types.MethodWrapperType) print(types.MethodDescriptorType)