Beispiel #1
0
def icompile(src, filename='<input>', mode='single'):
    import sys

    if src.strip() == "":
        return compile("None", "", "eval")

    # {{{ compile ipython magic commands as Python code
    if src.startswith("__IP.magic"):
        import codeop
        return codeop.CommandCompiler()(src, filename, mode)
    # }}}

    # {{{ return None if src is unfinished multiline
    if src.count("\n") > 0:
        if not src.endswith("\n"):
            return None
    else:
        if src.strip().endswith(":"):
            return None
    # }}}

    import IPython
    AutoTB = IPython.ultraTB.AutoFormattedTB(mode='Context',
                                             color_scheme='Linux',
                                             call_pdb=1)

    try:
        ulang = _ipy.locals['__currentlang__']

        if not isinstance(ulang, language.Language):
            raise SyntaxError("current language is not valid")
        elif not ulang.__impl__.name.endswith("~"):
            ulang = language.tmpLanguage(ulang, _ipy.locals['__name__'])
            _ipy.locals['__currentlang__'] = ulang

        lang = ulang.__impl__

        expr = lang.parse(src, mode="interactive", execenv=_ipy.locals)

        return interactiveCompile(expr, _ipy.locals, filename, mode)

    # {{{ exception handling
    except SyntaxError:
        raise
    except macros.MacroExpandError, exc:
        macros.formatMacroError(exc)
        exc_info = exc.exc_info
        raise ValueError("macro expand error")
Beispiel #2
0
def icompile(src, filename='<input>', mode='single'):
    import sys

    if src.strip() == "":
        return compile("None", "", "eval")

    # {{{ compile ipython magic commands as Python code
    if src.startswith("__IP.magic"):
        import codeop
        return codeop.CommandCompiler()(src, filename, mode)
    # }}}
    
    # {{{ return None if src is unfinished multiline
    if src.count("\n") > 0:
        if not src.endswith("\n"):
            return None
    else:
        if src.strip().endswith(":"):
            return None
    # }}}

    import IPython
    AutoTB = IPython.ultraTB.AutoFormattedTB(mode='Context', color_scheme='Linux',
                                             call_pdb=1)

    try:
        ulang = _ipy.locals['__currentlang__']

        if not isinstance(ulang, language.Language):
            raise SyntaxError("current language is not valid")
        elif not ulang.__impl__.name.endswith("~"):
            ulang = language.tmpLanguage(ulang, _ipy.locals['__name__'])
            _ipy.locals['__currentlang__'] = ulang
        
        lang = ulang.__impl__
        
        expr = lang.parse(src, mode="interactive", execenv=_ipy.locals)

        return interactiveCompile(expr, _ipy.locals, filename, mode)
    
    # {{{ exception handling
    except SyntaxError:
        raise
    except macros.MacroExpandError, exc:
        macros.formatMacroError(exc)
        exc_info = exc.exc_info
        raise ValueError("macro expand error")
Beispiel #3
0
    def runsource(self, src, filename='<input>', mode='single'):
        import sys

        if src.strip() == "":
            return False

        # {{{ return True if src is unfinished multiline
        if src.count("\n") > 0:
            if not src.endswith("\n"):
                return True
        else:
            if src.strip().endswith(":"):
                return True
        # }}}

        try:
            ulang = self.locals['__currentlang__']

            if not isinstance(ulang, language.Language):
                raise SyntaxError("current language is not valid")
            elif not ulang.__impl__.name.endswith("~"):
                ulang = language.tmpLanguage(ulang, self.locals['__name__'])
                self.locals['__currentlang__'] = ulang
            
            lang = ulang.__impl__
            
            expr = lang.parse(src, mode='interactive', execenv=self.locals)

            import ipy
            code = ipy.interactiveCompile(expr, self.locals, filename, mode)
            self.runcode(code)

            # {{{ update prompt with current language name
            lang = self.locals['__currentlang__']
            name = lang.__impl__.name
            sys.ps1 = "[%s]: " % (name.endswith("~") and name[:-1] or name)
            # }}}
            return False

        # {{{ exception handling
        except SyntaxError:
            self.showsyntaxerror(filename)
        except MacroExpandError, exc:
            formatMacroError(exc)
Beispiel #4
0
    def runsource(self, src, filename='<input>', mode='single'):
        import sys

        if src.strip() == "":
            return False

        # {{{ return True if src is unfinished multiline
        if src.count("\n") > 0:
            if not src.endswith("\n"):
                return True
        else:
            if src.strip().endswith(":"):
                return True
        # }}}

        try:
            ulang = self.locals['__currentlang__']

            if not isinstance(ulang, language.Language):
                raise SyntaxError("current language is not valid")
            elif not ulang.__impl__.name.endswith("~"):
                ulang = language.tmpLanguage(ulang, self.locals['__name__'])
                self.locals['__currentlang__'] = ulang

            lang = ulang.__impl__

            expr = lang.parse(src, mode='interactive', execenv=self.locals)

            import ipy
            code = ipy.interactiveCompile(expr, self.locals, filename, mode)
            self.runcode(code)

            # {{{ update prompt with current language name
            lang = self.locals['__currentlang__']
            name = lang.__impl__.name
            sys.ps1 = "[%s]: " % (name.endswith("~") and name[:-1] or name)
            # }}}
            return False

        # {{{ exception handling
        except SyntaxError:
            self.showsyntaxerror(filename)
        except MacroExpandError, exc:
            formatMacroError(exc)
Beispiel #5
0
def _imp(path, modulename, globals):
    import marshal, new, os
    mtime = os.path.getmtime
    exists = os.path.exists

    # HACK: To make limport work in devlogix lmodules
    if __name__ == 'devlogix' and modulename.startswith("logix."):
        modulename = 'dev' + modulename

    # {{{ if already in lmodules, just return it
    current = lmodules.get(modulename)
    if current:
        return current
    # }}}

    basename = modulename.replace(".","/")

    # might need to look for it within a package
    if (not exists(os.path.join(path, basename + ".lx"))
        and '__name__' in globals
        and '__file__' in globals):
        # {{{ modulename = package local name
        importer = globals['__name__']
        if globals['__file__'].endswith("__init__.py"):
            package = importer
        else:
            idx = importer.rfind('.')
            if idx == -1:
                package = importer
            else:
                package = importer[:idx]
        modulename = package + '.' + modulename
        # }}}
        basename = modulename.replace(".","/")
        # {{{ try again - if already in lmodules, just return it
        current = lmodules.get(modulename)
        if current:
            return current
        # }}}

    sfile = os.path.join(path, basename + ".lx")
    cfile = os.path.join(path, basename + ".lxc")

    if exists(sfile):
        # {{{ ensure package is loaded
        doti = modulename.rfind('.')
        if doti != -1:
            __import__(modulename[:doti], globals, None)
        # }}}
        # {{{ mod = create the module
        #print "limport", modulename
        mod = new.module(modulename)
        mod.__name__ = modulename
        mod.__file__ = cfile

        lmodules[modulename] = mod

        try:
            if not exists(cfile) or mtime(sfile) > mtime(cfile):
                execmodule(sfile, vars(mod))
            else:
                f = file(cfile, "rb")
                f.read(8)
                code = marshal.load(f)
                f.close()
                exec code in vars(mod)

                
        except MacroExpandError, e:
            print 'removing module', modulename
            del lmodules[modulename]
            formatMacroError(e, sfile)
            raise
        except:
Beispiel #6
0
def _imp(path, modulename, globals):
    import marshal, new, os
    mtime = os.path.getmtime
    exists = os.path.exists

    # HACK: To make limport work in devlogix lmodules
    if __name__ == 'devlogix' and modulename.startswith("logix."):
        modulename = 'dev' + modulename

    # {{{ if already in lmodules, just return it
    current = lmodules.get(modulename)
    if current:
        return current
    # }}}

    basename = modulename.replace(".", "/")

    # might need to look for it within a package
    if (not exists(os.path.join(path, basename + ".lx"))
            and '__name__' in globals and '__file__' in globals):
        # {{{ modulename = package local name
        importer = globals['__name__']
        if globals['__file__'].endswith("__init__.py"):
            package = importer
        else:
            idx = importer.rfind('.')
            if idx == -1:
                package = importer
            else:
                package = importer[:idx]
        modulename = package + '.' + modulename
        # }}}
        basename = modulename.replace(".", "/")
        # {{{ try again - if already in lmodules, just return it
        current = lmodules.get(modulename)
        if current:
            return current
        # }}}

    sfile = os.path.join(path, basename + ".lx")
    cfile = os.path.join(path, basename + ".lxc")

    if exists(sfile):
        # {{{ ensure package is loaded
        doti = modulename.rfind('.')
        if doti != -1:
            __import__(modulename[:doti], globals, None)
        # }}}
        # {{{ mod = create the module
        #print "limport", modulename
        mod = new.module(modulename)
        mod.__name__ = modulename
        mod.__file__ = cfile

        lmodules[modulename] = mod

        try:
            if not exists(cfile) or mtime(sfile) > mtime(cfile):
                execmodule(sfile, vars(mod))
            else:
                f = file(cfile, "rb")
                f.read(8)
                code = marshal.load(f)
                f.close()
                exec code in vars(mod)

        except MacroExpandError, e:
            print 'removing module', modulename
            del lmodules[modulename]
            formatMacroError(e, sfile)
            raise
        except: