本文整理汇总了Python中types.MemberDescriptorType方法的典型用法代码示例。如果您正苦于以下问题:Python types.MemberDescriptorType方法的具体用法?Python types.MemberDescriptorType怎么用?Python types.MemberDescriptorType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types
的用法示例。
在下文中一共展示了types.MemberDescriptorType方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ismemberdescriptor
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def ismemberdescriptor(object):
"""Return true if the object is a member descriptor.
Member descriptors are specialized descriptors defined in extension
modules."""
return isinstance(object, types.MemberDescriptorType)
示例2: isNonPrimitiveInstance
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def isNonPrimitiveInstance(x):
return not isinstance(x,(float,int,type,tuple,list,dict,str,bytes,complex,bool,slice,_rl_NoneType,
types.FunctionType,types.LambdaType,types.CodeType,
types.MappingProxyType,types.SimpleNamespace,
types.GeneratorType,types.MethodType,types.BuiltinFunctionType,
types.BuiltinMethodType,types.ModuleType,types.TracebackType,
types.FrameType,types.GetSetDescriptorType,types.MemberDescriptorType))
示例3: get_class_slots
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def get_class_slots(cls):
"""
Return a list of all slots registered in class and parent classes.
"""
slots = set()
for sub in cls.mro():
if sub is object:
continue
# Get slots from slots attribute
cls_slots = sub.__dict__.get("__slots__")
if isinstance(cls_slots, str):
slots.add(cls_slots)
continue
elif isinstance(cls_slots, (tuple, list)):
slots.update(slots)
continue
# Explicitly search for slot member objects
else:
members = []
for k, v in sub.__dict__.items():
if isinstance(v, MemberDescriptorType):
members.append(k)
if members:
slots.update(members)
else:
return None
return tuple(slots)
示例4: test_slots_special2
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def test_slots_special2(self):
# Testing __qualname__ and __classcell__ in __slots__
class Meta(type):
def __new__(cls, name, bases, namespace, attr):
self.assertIn(attr, namespace)
return super().__new__(cls, name, bases, namespace)
class C1:
def __init__(self):
self.b = 42
class C2(C1, metaclass=Meta, attr="__classcell__"):
__slots__ = ["__classcell__"]
def __init__(self):
super().__init__()
self.assertIsInstance(C2.__dict__["__classcell__"],
types.MemberDescriptorType)
c = C2()
self.assertEqual(c.b, 42)
self.assertNotHasAttr(c, "__classcell__")
c.__classcell__ = 42
self.assertEqual(c.__classcell__, 42)
with self.assertRaises(TypeError):
class C3:
__classcell__ = 42
__slots__ = ["__classcell__"]
class Q1(metaclass=Meta, attr="__qualname__"):
__slots__ = ["__qualname__"]
self.assertEqual(Q1.__qualname__, C1.__qualname__[:-2] + "Q1")
self.assertIsInstance(Q1.__dict__["__qualname__"],
types.MemberDescriptorType)
q = Q1()
self.assertNotHasAttr(q, "__qualname__")
q.__qualname__ = "q"
self.assertEqual(q.__qualname__, "q")
with self.assertRaises(TypeError):
class Q2:
__qualname__ = object()
__slots__ = ["__qualname__"]
示例5: _is_unsupported_type
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def _is_unsupported_type(obj):
return isinstance(obj, (types.ComplexType, types.TupleType, types.FunctionType, types.LambdaType,
types.GeneratorType, types.MethodType, types.UnboundMethodType, types.BuiltinFunctionType, types.BuiltinMethodType, types.FileType,
types.XRangeType, types.TracebackType, types.FrameType, types.DictProxyType, types.NotImplementedType, types.GetSetDescriptorType,
types.MemberDescriptorType))
示例6: getattr_static
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def getattr_static(obj, attr, default=_sentinel):
"""Retrieve attributes without triggering dynamic lookup via the
descriptor protocol, __getattr__ or __getattribute__.
Note: this function may not be able to retrieve all attributes
that getattr can fetch (like dynamically created attributes)
and may find attributes that getattr can't (like descriptors
that raise AttributeError). It can also return descriptor objects
instead of instance members in some cases. See the
documentation for details.
"""
instance_result = _sentinel
if not _is_type(obj):
klass = type(obj)
dict_attr = _shadowed_dict(klass)
if (dict_attr is _sentinel or
type(dict_attr) is types.MemberDescriptorType):
instance_result = _check_instance(obj, attr)
else:
klass = obj
klass_result = _check_class(klass, attr)
if instance_result is not _sentinel and klass_result is not _sentinel:
if (_check_class(type(klass_result), '__get__') is not _sentinel and
_check_class(type(klass_result), '__set__') is not _sentinel):
return klass_result
if instance_result is not _sentinel:
return instance_result
if klass_result is not _sentinel:
return klass_result
if obj is klass:
# for types we check the metaclass too
for entry in _static_getmro(type(klass)):
if _shadowed_dict(type(entry)) is _sentinel:
try:
return entry.__dict__[attr]
except KeyError:
pass
if default is not _sentinel:
return default
raise AttributeError(attr)
# ------------------------------------------------ generator introspection
示例7: _astroid_bootstrapping
# 需要导入模块: import types [as 别名]
# 或者: from types import MemberDescriptorType [as 别名]
def _astroid_bootstrapping():
"""astroid bootstrapping the builtins module"""
# this boot strapping is necessary since we need the Const nodes to
# inspect_build builtins, and then we can proxy Const
builder = InspectBuilder()
astroid_builtin = builder.inspect_build(builtins)
# pylint: disable=redefined-outer-name
for cls, node_cls in node_classes.CONST_CLS.items():
if cls is type(None):
proxy = build_class("NoneType")
proxy.parent = astroid_builtin
elif cls is type(NotImplemented):
proxy = build_class("NotImplementedType")
proxy.parent = astroid_builtin
else:
proxy = astroid_builtin.getattr(cls.__name__)[0]
if cls in (dict, list, set, tuple):
node_cls._proxied = proxy
else:
_CONST_PROXY[cls] = proxy
# Set the builtin module as parent for some builtins.
nodes.Const._proxied = property(_set_proxied)
_GeneratorType = nodes.ClassDef(
types.GeneratorType.__name__, types.GeneratorType.__doc__
)
_GeneratorType.parent = astroid_builtin
bases.Generator._proxied = _GeneratorType
builder.object_build(bases.Generator._proxied, types.GeneratorType)
if hasattr(types, "AsyncGeneratorType"):
# pylint: disable=no-member; AsyncGeneratorType
_AsyncGeneratorType = nodes.ClassDef(
types.AsyncGeneratorType.__name__, types.AsyncGeneratorType.__doc__
)
_AsyncGeneratorType.parent = astroid_builtin
bases.AsyncGenerator._proxied = _AsyncGeneratorType
builder.object_build(bases.AsyncGenerator._proxied, types.AsyncGeneratorType)
builtin_types = (
types.GetSetDescriptorType,
types.GeneratorType,
types.MemberDescriptorType,
type(None),
type(NotImplemented),
types.FunctionType,
types.MethodType,
types.BuiltinFunctionType,
types.ModuleType,
types.TracebackType,
)
for _type in builtin_types:
if _type.__name__ not in astroid_builtin:
cls = nodes.ClassDef(_type.__name__, _type.__doc__)
cls.parent = astroid_builtin
builder.object_build(cls, _type)
astroid_builtin[_type.__name__] = cls