pinterface / burgled-batteries

A bridge between Python and Lisp (FFI bindings, etc.)
Other
113 stars 21 forks source link

one way to rediredct stdout of python #9

Open jingtaozf opened 9 years ago

jingtaozf commented 9 years ago
  1. write a python bridge module
import io,sys
_lisp_python_stdout = None
_orig_lisp_python_stdout = None
_lisp_python_stderr = None
_orig_lisp_python_stderr = None
def begin_eval_in_python():
    global _lisp_python_stdout
    global _lisp_python_stderr
    global _orig_lisp_python_stdout
    global _orig_lisp_python_stderr
    _lisp_python_stdout = io.StringIO()
    _lisp_python_stderr = io.StringIO()
    _orig_lisp_python_stdout = sys.stdout
    _orig_lisp_python_stderr = sys.stderr
    sys.stdout = _lisp_python_stdout
    sys.stderr = _lisp_python_stderr
def end_eval_in_python():
    global _lisp_python_stdout
    global _lisp_python_stderr
    global _orig_lisp_python_stdout
    global _orig_lisp_python_stderr
    sys.stdout = _orig_lisp_python_stdout
    sys.stderr = _orig_lisp_python_stderr
    return [_lisp_python_stdout.getvalue(),_lisp_python_stderr.getvalue()]
  1. then in lisp side,first load this module
(burgled-batteries:startup-python)
(burgled-batteries:run "import imp")
(burgled-batteries:run
 (format nil "imp.load_source('python_lisp_brige','~/python-bridge.py')"))
(burgled-batteries:run "import python_lisp_brige")
  1. when run a python cmd, run it like this
(defun python-cmd (cmd)
  (burgled-batteries:run "python_lisp_brige.begin_eval_in_python()")
  (let* ((cmd-result (burgled-batteries:run cmd))
         (out-err (burgled-batteries:run "python_lisp_brige.end_eval_in_python()")))
    (when (> (length (aref out-err 0)) 0)
      (format t (aref out-err 0)))
    (when (> (length (aref out-err 1)) 0)
      (format t (aref out-err 1)))
    cmd-result))
pinterface commented 9 years ago

Neat! If I'm understanding this correctly, it essentially stuffs Python's stdout and stderr into strings, much like the CL:WITH-OUTPUT-TO-STRING macro, yes?

There are certainly cases where that's good enough (and this is a great example for those cases!), but it's not very general. E.g., it wouldn't work very well for an interactive program, or a long-running daemon that produces a lot of output. But it does seem like it should be available to people who would find it useful, so let me mull over what exactly to do with it. (Enable Github's wiki feature and include it there? Mention it in the documentation? I'm not sure what the best place for it is.)

On 2015.12.01 08:32, Xu Jingtao wrote:

  1. write a python bridge module
import io,sys
_lisp_python_stdout = None
_orig_lisp_python_stdout = None
_lisp_python_stderr = None
_orig_lisp_python_stderr = None
def begin_eval_in_python():
    global _lisp_python_stdout
    global _lisp_python_stderr
    global _orig_lisp_python_stdout
    global _orig_lisp_python_stderr
    _lisp_python_stdout = io.StringIO()
    _lisp_python_stderr = io.StringIO()
    _orig_lisp_python_stdout = sys.stdout
    _orig_lisp_python_stderr = sys.stderr
    sys.stdout = _lisp_python_stdout
    sys.stderr = _lisp_python_stderr
def end_eval_in_python():
    global _lisp_python_stdout
    global _lisp_python_stderr
    global _orig_lisp_python_stdout
    global _orig_lisp_python_stderr
    sys.stdout = _orig_lisp_python_stdout
    sys.stderr = _orig_lisp_python_stderr
    return [_lisp_python_stdout.getvalue(),_lisp_python_stderr.getvalue()]
  1. then in lisp side,first load this module
(burgled-batteries:startup-python)
(burgled-batteries:run "import imp")
(burgled-batteries:run
 (format nil "imp.load_source('python_lisp_brige','~/python-bridge.py')"))
(burgled-batteries:run "import python_lisp_brige")
  1. when run a python cmd, run it like this
(defun python-cmd (cmd)
  (burgled-batteries:run "python_lisp_brige.begin_eval_in_python()")
  (let* ((cmd-result (burgled-batteries:run cmd))
         (out-err (burgled-batteries:run "python_lisp_brige.end_eval_in_python()")))
    (when (> (length (aref out-err 0)) 0)
      (format t (aref out-err 0)))
    (when (> (length (aref out-err 1)) 0)
      (format t (aref out-err 1)))
    cmd-result))

Reply to this email directly or view it on GitHub: https://github.com/pinterface/burgled-batteries/issues/9