

def _eval(text=None, filename=None, _larch=None, new_module=None, interactive=False, printall=False):
    'evaluate a string of larch text\n    '
    if (_larch is None):
        raise Warning('cannot eval string. larch broken?')
    if (text is None):
        return None
    symtable = _larch.symtable
    lineno = 0
    output = None
    fname = filename
    inptext = inputText.InputText(interactive=interactive, _larch=_larch)
    is_complete = inptext.put(text, filename=filename)
    if (not is_complete):
        inptext.input_buff.reverse()
        (lline, lineno) = ('unknown line', 0)
        for (tline, complete, eos, fname, lineno) in inptext.input_buff:
            if complete:
                break
            lline = tline
        _larch.raise_exception(None, expr=lline, fname=fname, lineno=(lineno + 1), exc=SyntaxError, msg='input is incomplete')
    if ((len(inptext.keys) > 0) and (filename is not None)):
        msg = "file ends with un-terminated '%s' block"
        _larch.raise_exception(None, expr=("run('%s')" % filename), fname=filename, lineno=inptext.lineno, exc=IOError, msg=(msg % inptext.keys[0]))
    if (new_module is not None):
        symtable.save_frame()
        thismod = symtable.create_group(name=new_module)
        symtable._sys.modules[new_module] = thismod
        symtable.set_frame((thismod, thismod))
    output = []
    if (len(_larch.error) > 0):
        inptext.clear()
        return output
    while (len(inptext) > 0):
        (block, fname, lineno) = inptext.get()
        b = block.strip()
        if (len(b) <= 0):
            continue
        ret = _larch.eval(block, fname=fname, lineno=lineno)
        if (hasattr(ret, '__call__') and (not isinstance(ret, type))):
            try:
                if (1 == len(block.split())):
                    ret = ret()
            except:
                pass
        if (len(_larch.error) > 0):
            break
    if (len(_larch.error) > 0):
        inptext.clear()
    elif (printall and (ret is not None)):
        output.append(('%s' % ret))
    if (new_module is not None):
        symtable.restore_frame()
        output = thismod
    elif (len(output) > 0):
        output = '\n'.join(output)
    else:
        output = None
    return output

dataset/ETHPy150Open xraypy/xraylarch/lib/builtins.py _eval/original