PythonInterpreter.py :  » IDE » Boa-Constructor » boa-constructor-0.6.1 » ExternalLib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » IDE » Boa Constructor 
Boa Constructor » boa constructor 0.6.1 » ExternalLib » PythonInterpreter.py
#
# Instant Python
# $Id: PythonInterpreter.py,v 1.8 2003/10/13 08:24:06 riaan Exp $
#
# process python commands line by line
#
# written by Fredrik Lundh, November 1996
#
# fredrik@pythonware.com
# http://www.pythonware.com
#


import string, sys, traceback, linecache

sys.ps1 = ">>> "
sys.ps2 = "... "

class PythonInterpreter:

    def __init__(self, name = "<console>"):

        self.name = name
        self.locals = {}

        self.lines = []

    def push(self, line):

        #
        # collect lines

        if self.lines:
            if line:
                self.lines.append(line)
                return 1 # want more!
            else:
                line = string.join(self.lines, "\n") + "\n"
        else:
            if not line:
                return 0
            else:
                line = line.rstrip()+'\n'

        #
        # compile what we've got this far
        try:
            if sys.version_info[:2] >= (2, 2):
                import __future__
                code = compile(line, self.name, "single",
                               __future__.generators.compiler_flag, 1)
            else:
                code = compile(line, self.name, "single")
            self.lines = []

        except SyntaxError, why:
            if why[0] == "unexpected EOF while parsing":
                # start collecting lines
                self.lines.append(line)
                return 1 # want more!
            else:
                self.showtraceback()

        except:
            self.showtraceback()

        else:
            # execute
            try:
                exec code in self.locals
            except:
                self.showtraceback()

        return 0

    def showtraceback(self):

        self.lines = []

        exc_type, exc_value, exc_traceback = sys.exc_info()
        if exc_type == SyntaxError:# and len(sys.exc_value) == 2:
            # emulate interpreter behaviour
            if len(sys.exc_value.args) == 2:
                fn, ln, indent = sys.exc_value[1][:3]
                indent += 3
                pad = " " * indent
                if fn is not None:
                    src = linecache.getline(fn, ln)                    
                    if src:
                        src = src.rstrip()+'\n'
                        sys.stderr.write('  File "%s", line %d\n%s%s'%(
                                         fn, ln, pad, src))
                sys.stderr.write(pad + "^\n")
            sys.stderr.write("''' %s '''\n"%(str(sys.exc_type) + \
                str(sys.exc_value.args and (" : " +sys.exc_value[0]) or '')))
        else:
            traceback.print_tb(sys.exc_traceback.tb_next, None)
            sys.stderr.write("''' %s '''\n" %(str(sys.exc_type) + " : " + \
                                            str(sys.exc_value)))


# --------------------------------------------------------------------
# test stuff

if __name__ == "__main__":

    #
    # simple console interpreter simulation

    print 'Python', sys.version, "(PythonInterpreter)"
    print sys.copyright

    interp = PythonInterpreter()

    try:

        sys.stdout.write(sys.ps1)

        while 1:
            if interp.push(raw_input()):
                sys.stdout.write(sys.ps2)
            else:
                sys.stdout.write(sys.ps1)

    except EOFError:
        pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.