Problem with Emacs mode, at start only

Alex alex at somewhere.round.here
Wed Feb 23 20:36:29 CET 2000


The following changes to the output filter and py-execute-file pollute
the interpreter's namespace slightly, but they put a stop to the
premature deletion of the temporary files in which python code is to be
executed by putting the responsibility for the deletion on the
interpreter itself, after the execfile has been called.

;; Python subprocess utilities and filters
(defun py-execute-file (proc filename)
  "Send to Python interpreter process PROC \"execfile('FILENAME')\".
Make that process's buffer visible and force display.  Also make
comint believe the user typed this string so that
`kill-output-from-shell' does The Right Thing."
  (let ((curbuf (current-buffer))
	(procbuf (process-buffer proc))
;	(comint-scroll-to-bottom-on-output t)
	(msg (format "## working on region in file %s...\n" filename))
	(cmd (format (concat "try: \n"
			     "    __os_for_emacs_control__ = "
			                         "__import__('os') \n"
			     "    print \n"
			     "    execfile(r'%s')\n"
			     "finally: \n"
			     "    __os_for_emacs_control__.remove(r'%s')\n"
			     "    del __os_for_emacs_control__\n\n")
		     filename filename)))
    (unwind-protect
	(save-excursion
	  (set-buffer procbuf)
	  (goto-char (point-max))
	  (move-marker (process-mark proc) (point))
	  (funcall (process-filter proc) proc msg))
      (set-buffer curbuf))
    (process-send-string proc cmd)))

(defun py-comint-output-filter-function (string)
  "Watch output for Python prompt and exec next file waiting in queue.
This function is appropriate for `comint-output-filter-functions'."
  ;; TBD: this should probably use split-string
  (when (and (or (string-equal string ">>> ")
		 (and (>= (length string) 5)
		      (string-equal (substring string -5) "\n>>> ")))
	     py-file-queue)
    ;; (py-safe (delete-file (car py-file-queue)))
    (if (not (file-exists-p (car py-file-queue)))
	(progn
	  (setq py-file-queue (cdr py-file-queue))
	  (if py-file-queue
	      (let ((pyproc (get-buffer-process (current-buffer))))
		(py-execute-file pyproc (car py-file-queue))))
	  ))
    ))

After this, you can make a binding like 

(define-key py-mode-map "\C-c\C-c" '(lambda () (interactive)
				      (save-window-excursion (py-shell))
				      (py-execute-buffer)))

And it seems to work without the problems that Francois described.  For
me, it has also been helpful in that it allows me to type in the
interpreter buffer while the interpreter is executing a file with less
risk of confusing the output filter.

Alex.



More information about the Python-list mailing list