Python __builtin__ 模块,type() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用__builtin__.type()

项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def init(self, update=None, lang=None):
        '''
        Init pool
        Set update to proceed to update
        lang is a list of language code to be updated
        '''
        with self._lock:
            if not self._started:
                self.start()
        with self._locks[self.database_name]:
            # Don't reset pool if already init and not to update
            if not update and self._pool.get(self.database_name):
                return
            logger.info('init pool for "%s"', self.database_name)
            self._pool.setdefault(self.database_name, {})
            # Clean the _pool before loading modules
            for type in self.classes.keys():
                self._pool[self.database_name][type] = {}
            restart = not load_modules(self.database_name, self, update=update,
                    lang=lang)
            if restart:
                self.init()
项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def get(self, name, type='model'):
        '''
        Get an object from the pool

        :param name: the object name
        :param type: the type
        :return: the instance
        '''
        if type == '*':
            for type in self.classes.keys():
                if name in self._pool[self.database_name][type]:
                    break
        try:
            return self._pool[self.database_name][type][name]
        except KeyError:
            if type == 'report':
                from trytond.report import Report
                # Keyword argument 'type' conflicts with builtin function
                cls = __builtin__.type(str(name), (Report,), {})
                cls.__setup__()
                self.add(cls, type)
                return cls
            raise
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def new(self, name, bits, idaname=None, **kwargs):
        '''Add a register to the architecture's cache.'''

        # older
        if idaapi.__version__ < 7.0:
            dtype_by_size = utils.compose(idaapi.get_dtyp_by_size, ord)
        # newer
        else:
            dtype_by_size = idaapi.get_dtyp_by_size

        dtyp = kwargs.get('dtyp', idaapi.dt_bitfild if bits == 1 else dtype_by_size(bits//8))
        namespace = dict(register_t.__dict__)
        namespace.update({'__name__':name, 'parent':None, 'children':{}, 'dtyp':dtyp, 'offset':0, 'size':bits})
        namespace['realname'] = idaname
        namespace['alias'] = kwargs.get('alias', set())
        res = type(name, (register_t,), namespace)()
        self.__register__.__state__[name] = res
        self.__cache__[idaname or name,dtyp] = name
        return res
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def child(self, parent, name, offset, bits, idaname=None, **kwargs):
        '''Add a child-register to the architecture's cache.'''

        # older
        if idaapi.__version__ < 7.0:
            dtype_by_size = utils.compose(idaapi.get_dtyp_by_size, ord)
        # newer
        else:
            dtype_by_size = idaapi.get_dtyp_by_size

        dtyp = kwargs.get('dtyp', idaapi.dt_bitfild if bits == 1 else dtype_by_size(bits//8))
        namespace = dict(register_t.__dict__)
        namespace.update({'__name__':name, 'parent':parent, 'children':{}, 'dtyp':dtyp, 'offset':offset, 'size':bits})
        namespace['realname'] = idaname
        namespace['alias'] = kwargs.get('alias', set())
        res = type(name, (register_t,), namespace)()
        self.__register__.__state__[name] = res
        self.__cache__[idaname or name,dtyp] = name
        parent.children[offset] = res
        return res
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def __init__(self, maxlen=132, indent=4, method='smart', prefix='&',
                 suffix='&'):
        # Line length should be long enough that contintuation lines can host at
        # east one character apart of indentation and two continuation signs
        minmaxlen = indent + len(prefix) + len(suffix) + 1
        if maxlen < minmaxlen:
            msg = 'Maximal line length less than {0} when using an indentation'\
                  ' of {1}'.format(minmaxlen, indent)
            raise FyppFatalError(msg)
        self._maxlen = maxlen
        self._indent = indent
        self._prefix = ' ' * self._indent + prefix
        self._suffix = suffix
        if method not in ['brute', 'smart', 'simple']:
            raise FyppFatalError('invalid folding type')
        if method == 'brute':
            self._inherit_indent = False
            self._fold_position_finder = self._get_maximal_fold_pos
        elif method == 'simple':
            self._inherit_indent = True
            self._fold_position_finder = self._get_maximal_fold_pos
        elif method == 'smart':
            self._inherit_indent = True
            self._fold_position_finder = self._get_smart_fold_pos
项目:fypp    作者:aradi    | 项目源码 | 文件源码
def _get_callable_argspec_py3(func):
    sig = inspect.signature(func)
    args = []
    defaults = {}
    vararg = None
    for param in sig.parameters.values():
        if param.kind == param.POSITIONAL_OR_KEYWORD:
            args.append(param.name)
            if param.default != param.empty:
                defaults[param.name] = param.default
        elif param.kind == param.VAR_POSITIONAL:
            vararg = param.name
        else:
            msg = "argument '{0}' has invalid argument type".format(param.name)
            raise FyppFatalError(msg)
    return args, defaults, vararg


# Signature objects are available from Python 3.3 (and deprecated from 3.5)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def traverse(self, edges, filter=lambda node:True, **kwds):
        """Will walk the elements returned by the generator ``edges -> node -> ptype.type``

        This will iterate in a top-down approach.
        """
        for self in edges(self, **kwds):
            if not isinstance(self, generic):
                continue

            if filter(self):
                yield self

            for y in self.traverse(edges=edges, filter=filter, **kwds):
                yield y
            continue
        return
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def alloc(self, **attrs):
        """Will zero the ptype.container instance with the provided ``attrs``.

        This can be overloaded in order to allocate physical space for the new ptype.
        """

        # If there's a custom .blocksize changing the way this instance get's loaded
        #   then restore the original blocksize temporarily so that .alloc will actually
        #   allocate the entire object.
        if getattr(self.blocksize, 'im_func', None) is not container.blocksize.im_func:
            instancemethod = type.new.__class__
            func = container.blocksize.im_func
            method = instancemethod(func, self, self.__class__)
            attrs.setdefault('blocksize', method)

        return super(container, self).alloc(**attrs)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def append(self, object):
        """Add ``object`` to the ptype.container ``self``. Return the element's index.

        When adding ``object`` to ``self``, none of the offsets are updated and
        thus will need to be manually updated before committing to a provider.
        """

        # if we're uninitialized, then create an empty value and try again
        if self.value is None:
            self.value = []
            return self.append(object)

        # if object is not an instance, then try to resolve it to one and try again
        if not isinstance(object, generic):
            res = self.new(object)
            return self.append(res)

        # assume that object is now a ptype instance
        assert isinstance(object, generic), "container.append : {:s} : Tried to append unknown type to container : {:s}".format(self.instance(), object.__class__)
        object.parent,object.source = self,None

        current = len(self.value)
        self.value.append(object if object.initializedQ() else object.a)
        return current
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def define(cls, *definition, **attributes):
        """Add a definition to the cache keyed by the .type attribute of the definition. Return the original definition.

        If any ``attributes`` are defined, the definition is duplicated with the specified attributes before being added to the cache.
        """
        def clone(definition):
            res = dict(definition.__dict__)
            res.update(attributes)
            #res = __builtin__.type(res.pop('__name__',definition.__name__), definition.__bases__, res)
            res = __builtin__.type(res.pop('__name__',definition.__name__), (definition,), res)
            cls.add(getattr(res,cls.attribute),res)
            return definition

        if attributes:
            assert len(definition) == 0, 'Unexpected positional arguments'
            return clone
        res, = definition
        cls.add(getattr(res,cls.attribute),res)
        return res
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def reference(self, object, **attrs):
        """Reference ``object`` and encode it into self"""
        object = self.__hook(object)

        # take object, and encode it to an encoded type
        enc = self.encode(object, **attrs)

        # take encoded type and cast it to self's wrapped type in order to preserve length
        res = enc.cast(self._value_, **attrs)

        # now that the length is correct, write it to the wrapper_t
        res.commit(offset=0, source=provider.proxy(self))

        # assign some default attributes to object
        object.__name__ = '*'+self.name()
        self._object_ = object.__class__
        return self
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def test_container_copy():
        class leaf_sr(ptype.type):
            length = 4
        class leaf_jr(ptype.type):
            length = 2

        class branch(ptype.container): pass

        a = branch(source=prov.empty())
        a.set(leaf_sr, leaf_jr, branch().set(leaf_jr,leaf_jr,leaf_jr))
        b = a.copy()
        if b.v[2].v[1].size() == leaf_jr.length:
            raise Success

    # XXX: test casting between block types and stream types (szstring) as this
    #      might've been broken at some point...
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def test_decompression_block():
        from ptypes import dynamic,pint,pstruct,ptype
        class cblock(pstruct.type):
            class _zlibblock(ptype.encoded_t):
                _object_ = ptype.block
                def encode(self, object, **attrs):
                    data = object.serialize().encode('zlib')
                    return super(cblock._zlibblock,self).encode(ptype.block().set(data), length=len(data))
                def decode(self, object, **attrs):
                    data = object.serialize().decode('zlib')
                    return super(cblock._zlibblock,self).decode(ptype.block().set(data), length=len(data))

            def __zlibblock(self):
                return ptype.clone(self._zlibblock, _value_=dynamic.block(self['size'].l.int()))

            _fields_ = [
                (pint.uint32_t, 'size'),
                (__zlibblock, 'data'),
            ]
        message = 'hi there.'
        cmessage = message.encode('zlib')
        data = pint.uint32_t().set(len(cmessage)).serialize()+cmessage
        a = cblock(source=prov.string(data)).l
        if a['data'].d.l.serialize() == message:
            raise Success
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def __init__(self, *args, **kwds):
        super(enum, self).__init__(*args, **kwds)

        # invert ._values_ if they're defined backwards
        if len(self._values_):
            name, value = self._values_[0]
            if isinstance(value, basestring):
                Log.warning("pint.enum : {:s} : {:s}._values_ is defined backwards. Inverting it's values.".format(self.classname(), self.typename()))
                self._values_ = [(k,v) for v,k in self._values_]

        # verify the types are correct for ._values_
        if any(not isinstance(k, basestring) or not isinstance(v, six.integer_types) for k,v in self._values_):
            raise TypeError(self, 'enum.__init__', "{:s}._values_ is of an incorrect format. Should be [({:s}, {:s}), ...]".format(self.classname(), basestring, int))

        # FIXME: fix constants within ._values_ by checking to see if they're out of bounds of our type
        return
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def test_load(cls, integer, expected):
        if cls.length == 4:
            expected, = struct.unpack('f', struct.pack('f', expected))
            i,_ = bitmap.join(bitmap.new(ord(x),8) for x in reversed(struct.pack('f',expected)))
        elif cls.length == 8:
            expected, = struct.unpack('d', struct.pack('d', expected))
            i,_ = bitmap.join(bitmap.new(ord(x),8) for x in reversed(struct.pack('d',expected)))
        else:
            i = 0

        a = cls()
        super(type,a).set(integer)
        n = a.getf()

        if n == expected:
            raise Success
        raise Failure('getf: 0x%x == %s? %s (%s) %x'%( integer, expected, a.num(), n, i))

    ## tests for floating-point
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):

        self.data = {}

        # Initialise from sequence object

        if len(args) == 1 and type(args[0]) == list:
            for item in args[0]:
                self[item[0]] = item[1]

        # Initialise from mapping object

        if len(args) == 1 and type(args[0]) == dict:
            self.update(args[0])

        # Initialise from NocaseDict

        if len(args) == 1 and isinstance(args[0], NocaseDict):
            self.data = args[0].data.copy()

        # Initialise from keyword args

        self.update(kwargs)

    # Basic accessor and settor methods
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def __str__(self):

        s = ''

        if self.host is not None:
            s += '//%s/' % self.host

        if self.namespace is not None:
            s += '%s:' % self.namespace

        s += '%s.' % self.classname

        for key, value in self.keybindings.items():

            s +='%s=' % key

            if type(value) == int or type(value) == long:
                s += str(value)
            else:
                s += '"%s"' % value

            s += ','

        return s[:-1]
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def __setitem__(self, key, value):

        # Don't let anyone set integer or float values.  You must use
        # a subclass from the cim_type module.

        if type(value) == int or type(value) == float or type(value) == long:
            raise TypeError('Must use a CIM type assigning numeric values.')

        if self.property_list is not None and key.lower() not in \
                self.property_list:
            if self.path is not None and key not in self.path.keybindings:
                return
        # Convert value to appropriate PyWBEM type

        if isinstance(value, CIMProperty):
            v = value
        else:
            v = CIMProperty(key, value)

        self.properties[key] = v
        if self.path is not None and key in self.path.keybindings:
            self.path[key] = v.value
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def tocimxml(self):

        props = []

        for key, value in self.properties.items():

            # Value has already been converted into a CIM object
            # property type (e.g for creating null property values).

            if isinstance(value, CIMProperty):
                props.append(value)
                continue

            props.append(CIMProperty(key, value))

        instance_xml = cim_xml.INSTANCE(
            self.classname,
            properties = [p.tocimxml() for p in props],
            qualifiers = [q.tocimxml() for q in self.qualifiers.values()])

        if self.path is None:
            return instance_xml

        return cim_xml.VALUE_NAMEDINSTANCE(self.path.tocimxml(),
                                           instance_xml)
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def tomof(self):

        def _prop2mof(_type, value):
            if value is None:
                val = 'NULL'
            elif isinstance(value, list):
                val = '{'
                for i,x in enumerate(value):
                    if i > 0:
                        val += ', '
                    val += _prop2mof(_type, x)
                val += '}'
            elif _type == 'string':
                val = '"' + value + '"'
            else:
                val = str(value)
            return val

        s = 'instance of %s {\n' % self.classname
        for p in self.properties.values():
            s+= '\t%s = %s;\n' % (p.name, _prop2mof(p.type, p.value))
        s+= '};\n'

        return s
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def tocimxml(self):

        value = None

        if type(self.value) == list:
            value = VALUE_ARRAY([VALUE(v) for v in self.value])
        elif self.value is not None:
            value = VALUE(self.value)

        return QUALIFIER(self.name,
                         self.type,
                         value,
                         propagated = self.propagated,
                         overridable = self.overridable,
                         tosubclass = self.tosubclass,
                         toinstance = self.toinstance,
                         translatable = self.translatable)
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def __cmp__(self, other):

        if self is other:
            return 0
        elif not isinstance(other, CIMQualifierDeclaration):
            return 1

        return (cmpname(self.name, other.name) or
                cmp(self.type, other.type) or
                cmp(self.value, other.value) or
                cmp(self.is_array, other.is_array) or
                cmp(self.array_size, other.array_size) or
                cmp(self.scopes, other.scopes) or
                cmp(self.overridable, other.overridable) or
                cmp(self.tosubclass, other.tosubclass) or
                cmp(self.toinstance, other.toinstance) or
                cmp(self.translatable, other.translatable))
项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def __new__(cls, name, bases, dct):
        new = type.__new__(cls, name, bases, dct)
        if '__name__' in dct:
            try:
                new.__name__ = dct['__name__']
            except TypeError:
                new.__name__ = dct['__name__'].encode('utf-8')
        return new
项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def add(self, cls, type='model'):
        '''
        Add a classe to the pool
        '''
        with self._locks[self.database_name]:
            self._pool[self.database_name][type][cls.__name__] = cls
项目:health-mosconi    作者:GNUHealth-Mosconi    | 项目源码 | 文件源码
def iterobject(self, type='model'):
        '''
        Return an iterator over object name, object

        :param type: the type
        :return: an iterator
        '''
        return self._pool[self.database_name][type].iteritems()
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def type(self):
        '''Returns the IDA dtype of the register.'''
        return self.dtyp
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def define(cls, processor, type):
        def decorator(fn):
            res = processor, type
            return cls.cache.setdefault(res, fn)
        return decorator
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def lookup(cls, type, processor=None):
        try: return cls.cache[processor or idaapi.ph.id, type]
        except KeyError: return cls.cache[0, type]
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def type(cls, op, processor=None):
        res = cls.lookup(op.type, processor=processor)
        return res.__name__
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def operand(ea, none):
    '''Return all the op_t's of the instruction at ``ea``.'''
    insn = at(ea)
    res = itertools.takewhile(utils.compose(operator.attrgetter('type'), functools.partial(operator.ne, idaapi.o_void)), insn.Operands)
    if idaapi.__version__ < 7.0:
        return tuple(op.copy() for op in res)
    res = ((idaapi.op_t(), op) for op in res)
    return tuple([n.assign(op), n][1] for n, op in res)
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def op_type(n):
    '''Returns the string type of the ``n``th operand for the instruction at the current address.'''
    return op_type(ui.current.address(), n)
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def op_type(ea, n):
    """Returns the string type of the ``n``th operand for the instruction at the address ``ea``.
    Some of the types returned are: imm, reg, phrase, or addr
    """
    res = operand(ea, n)
    return __optype__.type(res)
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def op_value(ea, n):
    """Returns the value of the ``n``th operand for the instruction at the address ``ea``.

    The formats are based on the operand type as emitted by the ins.op_type function:
    imm -> integer
    reg -> register name
    addr -> address
    phrase -> (offset, base-register name, index-register name, scale)
    """
    res = operand(ea, n)
    return __optype__.decode(res)
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def op_refs(n):
    '''Returns the (address, opnum, type) of all the instructions that reference the ``n``th operand of the current instruction.'''
    return op_refs(ui.current.address(), n)
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def is_return(ea):
    '''Returns True if the instruction at ``ea`` is a ret-type instruction.'''
    idaapi.decode_insn(ea)
    return database.is_code(ea) and idaapi.is_ret_insn(ea)
#    return feature(ea) & idaapi.CF_STOP == idaapi.CF_STOP
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def register(op):
        '''Return the operand as a register_t.'''
        dtype_by_size = utils.compose(idaapi.get_dtyp_by_size, ord) if idaapi.__version__ < 7.0 else idaapi.get_dtyp_by_size
        if op.type in (idaapi.o_reg,):
            res, dt = op.reg, dtype_by_size(database.config.bits()//8)
            return reg.by_indextype(res, op.dtyp)
        optype = "{:s}({:d})".format('idaapi.o_reg', idaapi.o_reg)
        raise TypeError("{:s}.register(...) : {:s} : Invalid operand type. : {:d}".format('.'.join((__name__, 'operand_types')), s_optype, op.type))
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def immediate(op):
        '''Return the operand as an integer.'''
        if op.type in (idaapi.o_imm,idaapi.o_phrase):
            bits = idaapi.get_dtyp_size(op.dtyp) * 8
            return op.value & (2**bits-1)
        optype = "{:s}({:d})".format('idaapi.o_imm', idaapi.o_imm)
        raise TypeError("{:s}.immediate(...) : {:s} : Invalid operand type. : {:d}".format('.'.join((__name__, 'operand_types')), optype, op.type))
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def memory(op):
        '''Return the operand.addr field from an operand.'''
        if op.type in (idaapi.o_mem,idaapi.o_far,idaapi.o_near,idaapi.o_displ):
            seg,sel = (op.specval & 0xffff0000) >> 16, (op.specval & 0x0000ffff) >> 0
            return op.addr
        optype = map(utils.unbox("{:s}({:d})".format), [('idaapi.o_far', idaapi.o_far), ('idaapi.o_near', idaapi.o_near)])
        raise TypeError("{:s}.address(...) : {:s},{:s} : Invalid operand type. : {:d}".format('.'.join((__name__, 'operand_types')), optype[0], optype[1], op.type))
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def __newprc__(id):
    plfm, m = idaapi.ph.id, __import__('sys').modules[__name__]
    if plfm == idaapi.PLFM_386:     # id == 15
        m.reg = Intel()
        m.register = m.reg.r
    elif plfm == idaapi.PLFM_ARM:   # id == 1
        m.reg = AArch32()
        m.register = m.reg.r
    else:
        logging.warn("{:s} : IDP_Hooks.newprc({:d}) : {:d} : Unknown processor type. instruction module might not work properly.".format(__name__, id, plfm))
    return
项目:idascripts    作者:ctfhacker    | 项目源码 | 文件源码
def __eq__(self, other):
            classname = self.__class__.__name__
            if isinstance(other, basestring):
                if other.endswith(')'):
                    other_name = other.split('(')[0]
                    other_size = other.split('(')[-1].split(')')[0]
                    try: return cmp(classname, other_name) == 0 and self.size == int(other_size)
                    except ValueError: return False
                return cmp(classname, other) == 0
            return isinstance(self, other) if isinstance(other, __builtin__.type) else super(object, self) == other
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def isiterator(t):
    """True if type ``t`` is iterable"""
    # FIXME: also insure that it's not a class with these attributes
    return hasattr(t, '__iter__') and hasattr(t, 'next')
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def iscallable(t):
    """True if type ``t`` is a code object that can be called"""
    return callable(t) and hasattr(t, '__call__')
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def istype(t):
    """True if type ``t`` inherits from ptype.type"""
    return t.__class__ is t.__class__.__class__ and not isresolveable(t) and (isinstance(t, types.ClassType) or hasattr(object, '__bases__')) and issubclass(t, generic)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def iscontainer(t):
    """True if type ``t`` inherits from ptype.container """
    return (istype(t) and issubclass(t, container)) or pbinary.istype(t)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def isresolveable(t):
    """True if type ``t`` can be descended into"""
    return isinstance(t, (types.FunctionType, types.MethodType))    # or isiterator(t)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def force(t, self, chain=None):
    """Resolve type ``t`` into a ptype.type for the provided object ``self``"""
    if chain is None:
        chain = []
    chain.append(t)

    # of type pbinary.type. we insert a partial node into the tree
    if pbinary.istype(t):
        t = clone(pbinary.partial, _object_=t)
        return t

    # of type ptype
    if istype(t) or isinstance(t, base):
        return t

    # functions
    if isinstance(t, types.FunctionType):
        res = t(self)
        return force(res, self, chain)

    # bound methods
    if isinstance(t, types.MethodType):
        return force(t(), self, chain)

    if inspect.isgenerator(t):
        return force(next(t), self, chain)

    if False:
        # and lastly iterators
        if isiterator(t):
            return force(next(t), self, chain)

    path = ','.join(self.backtrace())
    raise error.TypeError(self, 'force<ptype>', message='chain={!r} : Refusing request to resolve {!r} to a type that does not inherit from ptype.type : {{{:s}}}'.format(chain, t, path))
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def instance(self):
        """Returns a minimal string describing the type and it's location"""
        name,ofs = self.classname(),self.getoffset()
        try:
            bs = self.blocksize()
            return '{:s}[{:x}:+{:x}]'.format(name, ofs, bs)
        except:
            pass
        return '{:s}[{:x}:+???]'.format(name, ofs)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def hexdump(self, **options):
        """Return a hexdump of the type using utils.hexdump(**options)

        Options can provide formatting specifiers
        terse -- display the hexdump tersely if larger than a specific threshold
        threshold -- maximum number of rows to display
        """
        options.setdefault('width', Config.display.hexdump.width)
        options.setdefault('offset', self.getoffset())
        return utils.hexdump(self.serialize(), **options)
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def getparent(self, *args, **kwds):
        """Returns the creator of the current type.

        If nothing is specified, return the parent element.

        If an argument is provided, return the element whose parent is the one
        specified.

        If the ``type`` argument is specified, recursively descend into .parent
        elements until encountering an instance that inherits from the one
        provided.
        """
        if len(args) == 0 and 'type' not in kwds:
            return self.parent

        t = args[0] if len(args) > 0 else kwds['type']

        if self.__class__ is t:
            return self

        #if self.__class__ == t or self is t or (isinstance(t,__builtin__.type) and (isinstance(self,t) or issubclass(self.__class__,t))):
        #    return self

        for x in self.traverse(edges=lambda node:(node.parent for x in range(1) if node.parent is not None)):
            if x.parent is t or (isinstance(t,__builtin__.type) and (isinstance(x,t) or issubclass(x.__class__,t))):
                return x
            continue

        # XXX
        chain = ';'.join(utils.repr_instance(x.classname(),x.name()) for x in self.traverse(edges=lambda node:(node.parent for x in range(1) if node.parent is not None)))
        try: bs = hex(self.blocksize())
        except: bs = '???'
        raise error.NotFoundError(self, 'base.getparent', message="match {:s} not found in chain : {:s}[{:x}:+{:s}] : {:s}".format(type.typename(), self.classname(), self.getoffset(), bs, chain))
项目:MacHeap    作者:blankwall    | 项目源码 | 文件源码
def new(self, t, **attrs):
        """Create a new instance of ``ptype`` with the provided ``attrs``

        If any ``attrs`` are provided, this will assign them to the new instance.
        The newly created instance will inherit the current object's .source and
        any .attributes designated by the current instance.
        """

        if 'recurse' in attrs:
            attrs['recurse'].update(self.attributes)
        else:
            attrs['recurse'] = self.attributes

        attrs.setdefault('parent', self)

        # instantiate an instance if we're given a type
        if not(istype(t) or isinstance(t,generic)):
            raise error.TypeError(self, 'base.new', message='{!r} is not a ptype class'.format(t.__class__))

        # if it's a type, then instantiate it
        if istype(t):
            t = t(**attrs)

        # if already instantiated, then update it's attributes
        elif isinstance(t,generic):
            t.__update__(**attrs)

        # give the instance a default name
        if '__name__' in attrs:
            t.__name__ = attrs['__name__']
        return t