Skip to content

Commit 10884a8

Browse files
committed
Clean up RUN, HABITAT
1 parent 017cf96 commit 10884a8

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

compiler/compiler.lisp

+27-23
Original file line numberDiff line numberDiff line change
@@ -126,47 +126,51 @@ Callers can intercept the condition MODULE-IMPORT-PRE to override default loadin
126126
(defparameter *compile-python-ast-before-running* nil
127127
"Whether to compile an AST before running it.")
128128

129+
(defparameter *run-module-globals* nil
130+
"The module namespace in which RUN forms are evaluated.")
131+
129132
(defun run-python-ast (ast &key (habitat *habitat*)
130133
(compile *compile-python-ast-before-running*)
131-
(module-globals (make-eq-hash-table))
134+
(module-globals *run-module-globals*)
132135
time
133-
args)
136+
(args nil args-p))
134137
"Run Python AST in freshly bound habitat.
135138
HABITAT is the execution environment; a fresh one will be used otherwie.
136139
If COMPILE is true, the AST is compiled into a function before running.
137140
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."
139142
;; At the moment there are only hashtable or package module namespaces:
140-
(check-type module-globals (or hash-table package))
141143
(with-compiler-generated-syntax-errors ()
142144
(handler-bind (#+sbcl
143145
(sb-kernel:redefinition-with-defun #'muffle-warning))
144-
(let* ((*habitat* habitat)
145-
(get-module-f `(lambda () ,ast))
146+
(let* ((get-module-f `(lambda () ,ast))
146147
(fc (if compile
147148
;; Same context as for importing a module
148149
(with-proper-compiler-settings
149150
(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))
165169
(flet ((run ()
166170
(funcall module-init-func module-globals) ;; always set __name__, __debug__
167171
(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))))
170174

171175

172176
;;; Python source files are compiled to fasl files. A way is needed to mark

runtime/habitat.lisp

+11-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
(stderr :initform nil :initarg :stderr :accessor %habitat-stderr)
1919
(loaded-mods :initform () :initarg :loaded-mods :accessor habitat-loaded-mods)
2020
(cmd-line-args :initform () :initarg :cmd-line-args :accessor %habitat-cmd-line-args)
21-
(search-paths :initform (make-py-list-from-list (list ".")) :accessor habitat-search-paths))
21+
(search-paths :initform (make-py-list-from-list (list ".")) :accessor habitat-search-paths)
22+
(module-globals :initform (make-eq-hash-table) :reader habitat-module-globals))
2223
(:documentation "Python execution context"))
2324

2425
(defmethod print-object ((habitat habitat) stream)
@@ -54,11 +55,15 @@
5455

5556
(defgeneric (setf habitat-cmd-line-args) (val habitat))
5657
(defmethod (setf habitat-cmd-line-args) (val (x habitat))
57-
(setf (%habitat-cmd-line-args x)
58-
(typecase val
59-
(string (py-string.split val " "))
60-
(list (make-py-list-from-list val))
61-
(t val))))
58+
(let ((old-val (%habitat-cmd-line-args x))
59+
(new-val (typecase val
60+
(string (py-string.split val " "))
61+
(list (make-py-list-from-list val))
62+
(t val))))
63+
(unless (equalp old-val new-val)
64+
(warn "Changing habitat command-line-args for ~A~%from:~% ~S~%to:~% ~S"
65+
x old-val new-val))
66+
(setf (%habitat-cmd-line-args x) new-val)))
6267
(defgeneric habitat-cmd-line-args (habitat))
6368
(defmethod habitat-cmd-line-args ((x habitat))
6469
(or (%habitat-cmd-line-args x)

0 commit comments

Comments
 (0)