...

Hi Rustom,

seems I have a ipython-bug here.

with
print 3 * 4
in foo.py

I get the correct result only first time in a separate buffer without ipython.

Afterwards always strange output:

IPython 0.8.1 -- An enhanced Interactive Python.
....
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: 12
Last number 12 is the correct output.
Seems some encoding from shell-processes wrong...

;;;;;;;;;;;;;;;;;

However, don't think it's related to our patch - attached.

May you try it? Thanks!

Hi Barry,

so far a first draft how to do it. Comments welcome.

Andreas



--- /home/speck/arbeit/python/python-modes/python-mode/python-mode.el	2009-10-16 18:55:37.000000000 +0200
+++ /home/speck/arbeit/python/python-modes/bug-450552-exec/python-mode.el	2009-11-02 23:08:02.000000000 +0100
@@ -368,6 +368,90 @@
   :type 'string
   :group 'python)
 
+(defcustom py-python-major-version ""
+  "*Different Python versions affect python execution and editing.
+Python-mode will adapt itself to a version specified.
+Otherwise python-mode will try some guess from the installed system.
+You may switch the addressed python-version with M-x py-switch-addressed-version."
+  :type 'string
+  :group 'python)
+(make-variable-buffer-local 'py-python-major-version)
+
+(defcustom py-guess-python-version t
+ "*Specifies whether to guess the py-python-major-version when a buffer is  
+visited. Defaults to t."
+  :type 'string
+  :group 'python)
+(make-variable-buffer-local 'py-guess-python-version)
+
+(defvar py-python-major-version-default-value "2"
+  "Use this value if py-python-major-version is empty and `py-guess-python-version' set to nil")
+
+(defvar py-exec-command nil
+  "Command set by `py-versions-compatibility-function'") 
+
+(defun py-versions-compatibility-function ()
+  "Set values according to python version.
+Addresses incompatibilities beween different versions of python itself. "
+  (interactive)
+  (let ((version
+         (cond ((not (string= "" py-python-major-version))
+                py-python-major-version)
+        ((when py-guess-python-version
+                (py-guess-python-version)))
+               (t py-python-major-version-default-value))))
+    (set-py-exec-command version)))
+
+(defun py-change-python-version (&optional arg) 
+  "Make `set-py-exec-command' ignore values specified by `py-python-major-version', `py-guess-python-version' resp. `py-python-major-version-default-value'.
+Set it herewith resp. get prompted for it." 
+  (interactive)
+  (let ((version (cond (arg arg)
+                       (t (read-from-minibuffer "Python version: ")))))
+    (when (not (string= "" version))
+    (setq py-python-major-version version)
+    (when (interactive-p) (message "py-python-major-version: %s" py-python-major-version)) 
+    py-python-major-version)))
+
+(defun set-py-exec-command (version)
+  "With Python 3.0 `execfile(fn)' has been replaced by `exec(open(fn).read())'.
+http://docs.python.org/dev/3.0/whatsnew/3.0.html 
+For Python 3.0 this function replaces
+\(format \"execfile(r'%s') # PYTHON-MODE\\n\" filename)
+by
+\(cmd (format \"exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\\n\" filename filename)) 
+as suggested by Rustom, https://bugs.launchpad.net/bugs/450552
+"
+  (setq py-exec-command
+        ;; (quote
+        (cond ((not (string= "" py-exec-this-command))
+               (quote py-exec-this-command))
+              ((string-match "^[^0-9]+2" version)
+               '(format "execfile(r'%s') # PYTHON-MODE\n" filename))
+              (t '(format "exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n" filename filename))))
+  (message "py-exec-comand set to %s" py-exec-command))
+
+(defcustom py-exec-this-command ""
+  "*Usually `py-exec-command' is selected from environement by
+setting vars `py-python-major-version' resp. `py-guess-python-version'.
+If not satisfied with these results, py-exec-this-command will overwrite it.  
+"
+  :type 'string
+  :group 'python)
+
+(defun py-guess-python-version ()
+  (interactive)
+  (let ((oldbuf (current-buffer)) 
+        (version 
+         (progn (set-buffer (get-buffer-create "Python-Version"))
+                (erase-buffer)
+                (shell-command "python --version" "Python-Version")
+                (goto-char (point-min)) 
+                (when (re-search-forward "Python.+" nil t 1)
+                  (match-string-no-properties 0)))))   
+    (when (interactive-p) (message "%s" version))
+    version))
+
 (defcustom py-shell-switch-buffers-on-execute t
   "*Controls switching to the Python buffer where commands are
   executed.  When non-nil the buffer switches to the Python buffer, if
@@ -1349,8 +1433,8 @@
         (procbuf (process-buffer proc))
 ;       (comint-scroll-to-bottom-on-output t)
         (msg (format "## working on region in file %s...\n" filename))
-        ;; add some comment, so that we can filter it out of history
-        (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename)))
+        ;; (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename)))
+        (cmd (eval (intern-soft py-exec-command))))
     (unwind-protect
         (save-excursion
           (set-buffer procbuf)
_______________________________________________
Python-mode mailing list
Python-mode@python.org
http://mail.python.org/mailman/listinfo/python-mode

Reply via email to