def convert(x, new_type, old_type=None, converter=None):
    ' Convert x to the new type if possible.\n    old_type -- the type of x. If not given, it is computed. '
    if (not isinstance(new_type, Type)):
        raise ValueError(('%s is not a type in the type hierarchy' % repr(new_type)))
    if (new_type == TYPE_OBJECT):
        return x
    if (not isinstance(old_type, Type)):
        (old_type, is_an_ast) = get_type(x)
    else:
        is_an_ast = isinstance(x, ast.AST)
    if (old_type == new_type):
        return x
    if (is_an_ast and (old_type == TYPE_BOX)):
        new_type_ast = ast.Name(id=new_type.constant_name)
        return get_call_ast('convert', [x, new_type_ast], return_type=new_type)
    if (converter is None):
        converter = get_converter(old_type, new_type)
        if (converter is None):
            raise TATypeError(bad_value=x, bad_type=old_type, req_type=new_type, message='found no converter for this type combination')

    def _apply_converter(converter, y):
        try:
            if is_an_ast:
                if (converter == identity):
                    return y
                elif is_instancemethod(converter):
                    func = ast.Attribute(value=y, attr=converter.im_func.__name__, ctx=ast.Load)
                    return get_call_ast(func)
                else:
                    func_name = converter.__name__
                    return get_call_ast(func_name, [y])
            else:
                return converter(y)
        except BaseException:
            raise TATypeError(bad_value=x, bad_type=old_type, req_type=new_type, message='error during conversion')
    if isinstance(converter, (list, tuple)):
        result = x
        for conv in converter:
            result = _apply_converter(conv, result)
        return result
    elif (converter is not None):
        return _apply_converter(converter, x)
Convert x to the new type if possible.
old_type -- the type of x. If not given, it is computed.
def convert(x, new_type, old_type=None, converter=None):
    ' Convert x to the new type if possible.\n    old_type -- the type of x. If not given, it is computed. '
    if (not isinstance(new_type, Type)):
        raise ValueError(('%s is not a type in the type hierarchy' % repr(new_type)))
    if (new_type == TYPE_OBJECT):
        return x
    if (not isinstance(old_type, Type)):
        (old_type, is_an_ast) = get_type(x)
    else:
        is_an_ast = isinstance(x, ast.AST)
    if (old_type == new_type):
        return x
    if (is_an_ast and (old_type == TYPE_BOX)):
        new_type_ast = ast.Name(id=new_type.constant_name)
        return get_call_ast('convert', [x, new_type_ast], return_type=new_type)
    if (converter is None):
        converter = get_converter(old_type, new_type)
        if (converter is None):
            raise TATypeError(bad_value=x, bad_type=old_type, req_type=new_type, message='found no converter for this type combination')

    def _apply_converter(converter, y):
        try:
            if is_an_ast:
                if (converter == identity):
                    return y
                elif is_instancemethod(converter):
                    func = ast.Attribute(value=y, attr=converter.im_func.__name__, ctx=ast.Load)
                    return get_call_ast(func)
                else:
                    func_name = converter.__name__
                    return get_call_ast(func_name, [y])
            else:
                return converter(y)
        except BaseException:
            raise TATypeError(bad_value=x, bad_type=old_type, req_type=new_type, message='error during conversion')
    if isinstance(converter, (list, tuple)):
        result = x
        for conv in converter:
            result = _apply_converter(conv, result)
        return result
    elif (converter is not None):
        return _apply_converter(converter, x)