1
1
# Copyright 2018 the authors.
2
2
# This file is part of Hy, which is free software licensed under the Expat
3
3
# license. See the LICENSE.
4
-
5
- from __future__ import print_function
4
+ from __future__ import absolute_import , print_function
6
5
7
6
import argparse
8
7
import code
13
12
import importlib
14
13
import py_compile
15
14
import runpy
15
+ import types
16
16
17
17
import astor .code_gen
18
18
@@ -47,10 +47,12 @@ def __call__(self, code=None):
47
47
builtins .exit = HyQuitter ('exit' )
48
48
49
49
50
- class HyREPL (code .InteractiveConsole ):
50
+ class HyREPL (code .InteractiveConsole , object ):
51
51
def __init__ (self , spy = False , output_fn = None , locals = None ,
52
52
filename = "<input>" ):
53
53
54
+ super (HyREPL , self ).__init__ (locals = locals , filename = filename )
55
+
54
56
self .spy = spy
55
57
56
58
if output_fn is None :
@@ -65,13 +67,24 @@ def __init__(self, spy=False, output_fn=None, locals=None,
65
67
else :
66
68
self .output_fn = __builtins__ [mangle (output_fn )]
67
69
68
- code .InteractiveConsole .__init__ (self , locals = locals ,
69
- filename = filename )
70
-
71
70
# Pre-mangle symbols for repl recent results: *1, *2, *3
72
71
self ._repl_results_symbols = [mangle ("*{}" .format (i + 1 )) for i in range (3 )]
73
72
self .locals .update ({sym : None for sym in self ._repl_results_symbols })
74
73
74
+ # Create a proper module for this REPL so that we can obtain it easily
75
+ # (e.g. using `importlib.import_module`), have it be distinct from
76
+ # `__main__`, and use it with `hy_compile`.
77
+ module_name = self .locals ['__name__' ]
78
+ self .module = types .ModuleType (module_name )
79
+ self .module .__dict__ .update (self .locals )
80
+
81
+ self .locals = self .module .__dict__
82
+
83
+ sys .modules [module_name ] = self .module
84
+
85
+ # Load cmdline-specific macros.
86
+ require ('hy.cmdline' , '__console__' , assignments = 'ALL' )
87
+
75
88
def runsource (self , source , filename = '<input>' , symbol = 'single' ):
76
89
global SIMPLE_TRACEBACKS
77
90
@@ -102,8 +115,7 @@ def ast_callback(main_ast, expr_ast):
102
115
new_ast = ast .Module (main_ast .body +
103
116
[ast .Expr (expr_ast .body )])
104
117
print (astor .to_source (new_ast ))
105
- value = hy_eval (do , self .locals , "__console__" ,
106
- ast_callback )
118
+ value = hy_eval (do , self .locals , self .module , ast_callback )
107
119
except HyTypeError as e :
108
120
if e .source is None :
109
121
e .source = source
@@ -181,7 +193,7 @@ def ideas_macro(ETname):
181
193
182
194
""" )])
183
195
184
- require ( "hy.cmdline" , "__console__" , assignments = "ALL" )
196
+
185
197
require ("hy.cmdline" , "__main__" , assignments = "ALL" )
186
198
187
199
SIMPLE_TRACEBACKS = True
@@ -199,7 +211,8 @@ def pretty_error(func, *args, **kw):
199
211
200
212
def run_command (source ):
201
213
tree = hy_parse (source )
202
- pretty_error (hy_eval , tree , module_name = "__main__" )
214
+ module = importlib .import_module ('__main__' )
215
+ pretty_error (hy_eval , tree , None , module )
203
216
return 0
204
217
205
218
@@ -208,12 +221,12 @@ def run_repl(hr=None, **kwargs):
208
221
sys .ps1 = "=> "
209
222
sys .ps2 = "... "
210
223
211
- namespace = {'__name__' : '__console__' , '__doc__' : '' }
224
+ if not hr :
225
+ hr = HyREPL (** kwargs )
212
226
213
- with completion ( Completer ( namespace )):
227
+ namespace = hr . locals
214
228
215
- if not hr :
216
- hr = HyREPL (locals = namespace , ** kwargs )
229
+ with completion (Completer (namespace )):
217
230
218
231
hr .interact ("{appname} {version} using "
219
232
"{py}({build}) {pyversion} on {os}" .format (
@@ -409,7 +422,6 @@ def hyc_main():
409
422
# entry point for cmd line script "hy2py"
410
423
def hy2py_main ():
411
424
import platform
412
- module_name = "<STDIN>"
413
425
414
426
options = dict (prog = "hy2py" , usage = "%(prog)s [options] [FILE]" ,
415
427
formatter_class = argparse .RawDescriptionHelpFormatter )
@@ -448,7 +460,7 @@ def hy2py_main():
448
460
print ()
449
461
print ()
450
462
451
- _ast = pretty_error (hy_compile , hst , module_name )
463
+ _ast = pretty_error (hy_compile , hst , '__main__' )
452
464
if options .with_ast :
453
465
if PY3 and platform .system () == "Windows" :
454
466
_print_for_windows (astor .dump_tree (_ast ))
0 commit comments