def PySequence_Fast(space, w_obj, m): """Returns the sequence o as a tuple, unless it is already a tuple or list, in which case o is returned. Use PySequence_Fast_GET_ITEM() to access the members of the result. Returns NULL on failure. If the object is not a sequence, raises TypeError with m as the message text.""" if (isinstance(w_obj, listobject.W_ListObject) or isinstance(w_obj, tupleobject.W_TupleObject)): return w_obj try: return tupleobject.W_TupleObject(space.fixedview(w_obj)) except OperationError: raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
def PySequence_Fast(space, w_obj, m): """Returns the sequence o as a tuple, unless it is already a tuple or list, in which case o is returned. Use PySequence_Fast_GET_ITEM() to access the members of the result. Returns NULL on failure. If the object is not a sequence, raises TypeError with m as the message text.""" if isinstance(w_obj, listobject.W_ListObject): # make sure we can return a borrowed obj from PySequence_Fast_GET_ITEM # XXX how does this interact with CPyListStrategy? w_obj.ensure_object_strategy() return w_obj if isinstance(w_obj, tupleobject.W_TupleObject): return w_obj try: return tupleobject.W_TupleObject(space.fixedview(w_obj)) except OperationError: raise OperationError(space.w_TypeError, space.wrap(rffi.charp2str(m)))
def PySequence_Fast(space, w_obj, m): """Returns the sequence o as a tuple, unless it is already a tuple or list, in which case o is returned. Use PySequence_Fast_GET_ITEM() to access the members of the result. Returns NULL on failure. If the object cannot be converted to a sequence, and raises a TypeError, raise a new TypeError with m as the message text. If the conversion otherwise, fails, reraise the original exception""" if isinstance(w_obj, tupleobject.W_AbstractTupleObject): return w_obj # CCC avoid the double conversion that occurs here if isinstance(w_obj, W_ListObject): # note: we used to call w_obj.convert_to_cpy_strategy() here, # but we really have to call it from PySequence_Fast_GET_ITEM() # because some people never call PySequence_Fast() if they know # the object is a list. return w_obj try: return tupleobject.W_TupleObject(space.fixedview(w_obj)) except OperationError as e: if e.match(space, space.w_TypeError): raise OperationError(space.w_TypeError, space.newtext(rffi.charp2str(m))) raise e