@@ -126,47 +126,51 @@ Callers can intercept the condition MODULE-IMPORT-PRE to override default loadin
126
126
(defparameter *compile-python-ast-before-running* nil
127
127
" Whether to compile an AST before running it." )
128
128
129
+ (defparameter *run-module-globals* nil
130
+ " The module namespace in which RUN forms are evaluated." )
131
+
129
132
(defun run-python-ast (ast &key (habitat *habitat* )
130
133
(compile *compile-python-ast-before-running* )
131
- (module-globals (make-eq-hash-table) )
134
+ (module-globals *run-module-globals* )
132
135
time
133
- args)
136
+ ( args nil args-p) )
134
137
" Run Python AST in freshly bound habitat.
135
138
HABITAT is the execution environment; a fresh one will be used otherwie.
136
139
If COMPILE is true, the AST is compiled into a function before running.
137
140
MODULE-RUN-ARGS is a list with options passed on to the module-function; e.g. %module-globals, module-name, src-module-path.
138
- ARGS are the command-line args, available as `sys.argv'; can be a string or a list of strings."
141
+ ARGS are the command-line args, available as `sys.argv'; can be a string (which will be splitted on spaces) or a list of strings."
139
142
; ; At the moment there are only hashtable or package module namespaces:
140
- (check-type module-globals (or hash-table package ))
141
143
(with-compiler-generated-syntax-errors ()
142
144
(handler-bind (#+ sbcl
143
145
(sb-kernel :redefinition-with-defun #' muffle-warning ))
144
- (let* ((*habitat* habitat)
145
- (get-module-f ` (lambda () , ast))
146
+ (let* ((get-module-f ` (lambda () , ast))
146
147
(fc (if compile
147
148
; ; Same context as for importing a module
148
149
(with-proper-compiler-settings
149
150
(compile nil get-module-f))
150
- (coerce get-module-f ' function))))
151
- (unless *habitat* (setf *habitat* (make-habitat)))
152
- (when (or args (null (habitat-cmd-line-args *habitat* )))
153
- (setf (habitat-cmd-line-args *habitat* ) args))
154
- (let (module-init-func module-run-tlv-func result)
155
- (handler-bind ((module-import-pre (lambda (c)
156
- ; ; This handler just saves the relevant functions,
157
- ; ; unwinding the import state with restarts like
158
- ; ; continue-loading, abort-loading, so the user is not
159
- ; ; bothered by these restarts.
160
- (setf module-init-func (mip.init-func c)
161
- module-run-tlv-func (mip.run-tlv-func c))
162
- (invoke-restart ' abort-loading))))
163
- (funcall fc))
164
- (assert (and module-init-func module-run-tlv-func) () " Unexpected module import behaviour" )
151
+ (coerce get-module-f ' function)))
152
+ module-init-func module-run-tlv-func result)
153
+ (handler-bind ((module-import-pre (lambda (c)
154
+ ; ; This handler just saves the relevant functions,
155
+ ; ; unwinding the import state with restarts like
156
+ ; ; continue-loading, abort-loading, so the user is not
157
+ ; ; bothered by these restarts.
158
+ (setf module-init-func (mip.init-func c)
159
+ module-run-tlv-func (mip.run-tlv-func c))
160
+ (invoke-restart ' abort-loading))))
161
+ (funcall fc))
162
+ (assert (and module-init-func module-run-tlv-func) () " Unexpected module import behaviour" )
163
+ (let ((*habitat* (or habitat (make-habitat))))
164
+ (unless module-globals
165
+ (setf module-globals (make-eq-hash-table)))
166
+ (check-type module-globals (or hash-table package ))
167
+ (when args-p
168
+ (setf (habitat-cmd-line-args *habitat* ) args))
165
169
(flet ((run ()
166
170
(funcall module-init-func module-globals) ; ; always set __name__, __debug__
167
171
(setf result (funcall module-run-tlv-func module-globals))))
168
- (if time (time (run)) (run)))
169
- result) ))))
172
+ (if time (time (run)) (run))))
173
+ result))))
170
174
171
175
172
176
; ;; Python source files are compiled to fasl files. A way is needed to mark
0 commit comments