12 except AttributeError:
16 def print_exc(limit
=None, file=None):
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
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)
32 traceback
.print_exception(sys
.exc_type
, sys
.exc_value
,
33 sys
.exc_traceback
, limit
, file)
41 self
._compile
= codeop
.Compile()
43 def executeline(self
, stuff
, out
= None, env
= None):
46 env
= __main__
.__dict
__
48 saveerr
, saveout
= sys
.stderr
, sys
.stdout
49 sys
.stderr
= sys
.stdout
= out
52 self
._pybuf
= self
._pybuf
+ '\n' + 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
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] == '#':
72 sys
.stdout
.write(sys
.ps1
)
77 code
= self
._compile
(self
._pybuf
, "<input>", "single")
78 except SyntaxError, err
:
81 # OverflowError. More?
84 sys
.stdout
.write(sys
.ps1
)
89 code1
= self
._compile
(self
._pybuf
+ "\n", "<input>", "single")
90 except SyntaxError, err1
:
94 code2
= self
._compile
(self
._pybuf
+ "\n\n", "<input>", "single")
95 except SyntaxError, err2
:
106 elif err1
== err2
or (not stuff
and self
._pybuf
):
110 sys
.stdout
.write(sys
.ps2
)
113 sys
.stdout
.write(sys
.ps1
)
117 sys
.stderr
, sys
.stdout
= saveerr
, saveout