2525
2626import traceback
2727import string
28+ import ast
2829from inspect import ismodule , getsource , getsourcefile
2930from tmpy .compat import py_ver
3031from tmpy .capture import CaptureStdout
31- from tmpy .postscript import ps_out
32+ from tmpy .postscript import ps_out , PSOutDummy
3233from tmpy .completion import parse_complete_command , complete
3334from tmpy .protocol import *
3435
3738 exit (- 1 )
3839
3940# import logging as log
40- # log.basicConfig(filename='/tmp/tm_python.log',level=log.DEBUG)
41- def compose_output (data ):
41+ # log.basicConfig(filename='/tmp/tm_python.log',level=log.INFO)
42+
43+ def flush_output (data ):
4244 """Do some parsing on the output according to its type.
4345
4446 Non printable characters in unicode strings are escaped
4547 and objects of type None are not printed (so that procedure calls,
4648 as opposed to function calls, don't produce any output)."""
4749
48- if isinstance (data , str ):
49- data2 = r''
50- for c in data :
51- if c not in string .printable :
52- data2 += '\\ x%x' % ord (c )
53- else :
54- data2 += c
55- data = data2
56- if data is None :
57- data = ''
58- return str (data ).strip ()
50+ if (data is None ):
51+ flush_verbatim ("" )
52+ return
53+
54+ if isinstance (data , PSOutDummy ):
55+ flush_ps (data .content )
56+ else :
57+ flush_verbatim (str (data ).strip ())
5958
6059def as_scm_string (text ):
6160 return '"%s"' % text .replace ('\\ ' , '\\ \\ ' ).replace ('"' , '\\ "' )
@@ -85,6 +84,18 @@ def compile_help (text):
8584
8685 return dict (map (lambda k_v : (k_v [0 ], as_scm_string (k_v [1 ])), out .items ()))
8786
87+
88+ def my_eval (code , p_globals ):
89+ '''Execute a script and return the value of the last expression'''
90+
91+ block = ast .parse (code , mode = 'exec' )
92+ if len (block .body ) > 1 and isinstance (block .body [- 1 ], ast .Expr ):
93+ last = ast .Expression (block .body .pop ().value )
94+ exec (compile (block , '<string>' , mode = 'exec' ), p_globals )
95+ return eval (compile (last , '<string>' , mode = 'eval' ), p_globals )
96+ else :
97+ return eval (code , p_globals )
98+
8899__version__ = '3.0'
89100__author__ = 'Ero Carrera, Adrian Soto, Miguel de Benito Delgado, Darcy Shen'
90101my_globals = {}
@@ -115,7 +126,7 @@ def compile_help (text):
115126 "Please see the documentation in Help -> Plugins -> Python" )
116127flush_prompt (">>> " )
117128while True :
118- line = input ()
129+ line = input ()
119130 if not line :
120131 continue
121132 if line [0 ] == DATA_COMMAND :
@@ -125,22 +136,22 @@ def compile_help (text):
125136 continue
126137 elif line .endswith ('?' ) and not line .strip ().startswith ('#' ):
127138 if len (line ) > 1 :
128- out = compile_help (line [:- 1 ])
139+ out = compile_help (line [:- 1 ])
129140 flush_command ('(tmpy-open-help %s %s %s)' %
130141 (out ["help" ], out ["src" ], out ["file" ]))
131142 else :
132143 flush_verbatim ('Type a name before the "?" to see the help' )
133144 continue
134145 else :
135- lines = [line ]
146+ lines = [line ]
136147 while line != "<EOF>" :
137- line = input ()
148+ line = input ()
138149 if line == '' :
139150 continue
140151 lines .append (line )
141152 text = '\n ' .join (lines [:- 1 ])
142153 try : # Is it an expression?
143- result = eval (text , my_globals )
154+ result = my_eval (text , my_globals )
144155 except :
145- result = CaptureStdout .capture (text , my_globals , "tm_python" )
146- flush_verbatim ( compose_output ( result ) )
156+ result = CaptureStdout .capture (text , my_globals , "tm_python" )
157+ flush_output ( result )
0 commit comments