Bump to 2.3.1 to pick up the missing file.
[python/dscho.git] / Mac / Tools / IDE / PyInteractive.py
blob3ad02c595b3652a9564772b170f060e37ef627bd
1 import string
2 import sys
3 import traceback
6 try:
7 sys.ps1
8 except AttributeError:
9 sys.ps1 = ">>> "
10 try:
11 sys.ps2
12 except AttributeError:
13 sys.ps2 = "... "
16 def print_exc(limit=None, file=None):
17 if not file:
18 file = sys.stderr
19 # we're going to skip the outermost traceback object, we don't
20 # want people to see the line which excecuted their code.
21 tb = sys.exc_traceback
22 if tb:
23 tb = tb.tb_next
24 try:
25 sys.last_type = sys.exc_type
26 sys.last_value = sys.exc_value
27 sys.last_traceback = tb
28 traceback.print_exception(sys.last_type, sys.last_value,
29 sys.last_traceback, limit, file)
30 except:
31 print '--- hola! ---'
32 traceback.print_exception(sys.exc_type, sys.exc_value,
33 sys.exc_traceback, limit, file)
36 class PyInteractive:
38 def __init__(self):
39 import codeop
40 self._pybuf = ""
41 self._compile = codeop.Compile()
43 def executeline(self, stuff, out = None, env = None):
44 if env is None:
45 import __main__
46 env = __main__.__dict__
47 if out:
48 saveerr, saveout = sys.stderr, sys.stdout
49 sys.stderr = sys.stdout = out
50 try:
51 if self._pybuf:
52 self._pybuf = self._pybuf + '\n' + stuff
53 else:
54 self._pybuf = stuff
56 # Compile three times: as is, with \n, and with \n\n appended.
57 # If it compiles as is, it's complete. If it compiles with
58 # one \n appended, we expect more. If it doesn't compile
59 # either way, we compare the error we get when compiling with
60 # \n or \n\n appended. If the errors are the same, the code
61 # is broken. But if the errors are different, we expect more.
62 # Not intuitive; not even guaranteed to hold in future
63 # releases; but this matches the compiler's behavior in Python
64 # 1.4 and 1.5.
65 err = err1 = err2 = None
66 code = code1 = code2 = None
68 # quickly get out of here when the line is 'empty' or is a comment
69 stripped = string.strip(self._pybuf)
70 if not stripped or stripped[0] == '#':
71 self._pybuf = ''
72 sys.stdout.write(sys.ps1)
73 sys.stdout.flush()
74 return
76 try:
77 code = self._compile(self._pybuf, "<input>", "single")
78 except SyntaxError, err:
79 pass
80 except:
81 # OverflowError. More?
82 print_exc()
83 self._pybuf = ""
84 sys.stdout.write(sys.ps1)
85 sys.stdout.flush()
86 return
88 try:
89 code1 = self._compile(self._pybuf + "\n", "<input>", "single")
90 except SyntaxError, err1:
91 pass
93 try:
94 code2 = self._compile(self._pybuf + "\n\n", "<input>", "single")
95 except SyntaxError, err2:
96 pass
98 if code:
99 try:
100 exec code in env
101 except:
102 print_exc()
103 self._pybuf = ""
104 elif code1:
105 pass
106 elif err1 == err2 or (not stuff and self._pybuf):
107 print_exc()
108 self._pybuf = ""
109 if self._pybuf:
110 sys.stdout.write(sys.ps2)
111 sys.stdout.flush()
112 else:
113 sys.stdout.write(sys.ps1)
114 sys.stdout.flush()
115 finally:
116 if out:
117 sys.stderr, sys.stdout = saveerr, saveout