Re: [Python-mode] [Bug 450552] Re: python-mode breaks for python 3
... 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. [0;32mIn [[1;32m1[0;32m]: [0m12 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.0 +0200 +++ /home/speck/arbeit/python/python-modes/bug-450552-exec/python-mode.el 2009-11-02 23:08:02.0 +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))) +
Re: [Python-mode] [Bug 450552] Re: python-mode breaks for python 3
On Tue, Oct 20, 2009 at 1:59 PM, Andreas Roehler andreas.roeh...@online.dewrote: Rustom wrote: (cmd (format exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n filename filename))) What puzzles me still is a pure python question - do we need this `read()' here, i.e. if a file opened is delivered to exec, will it not being read anyway? Thanks See Guido's 2 to 3 doc http://docs.python.org/dev/3.0/whatsnew/3.0.html#removed-syntax says stream argument not taken ___ Python-mode mailing list Python-mode@python.org http://mail.python.org/mailman/listinfo/python-mode
Re: [Python-mode] [Bug 450552] Re: python-mode breaks for python 3
On Fri, Oct 23, 2009 at 12:50 AM, Andreas Roehler andreas.roeh...@online.de wrote: Sorry. Simply tried to catch the execution of your form with edebug. The ways I tried, it wasn't called. Ok -- Here goes 1. Take some trivial python file (say foo.py) in python-mode A single line x = 1 will do [I assume it opens with python-mode as major mode] 2. Set edebug to break function py-execute-file (in my case its line no 1358) 3. From foo.py do C-c ! (py-shell) to start python 4. Go back to foo.py buffer 5. Load using C-c C-c (py-execute-buffer) Why are 3 and 4 necessary? Dunno -- but they are at least in my case edebug should be at the breakpoint. Please tell me if this does not work. Rustom ___ Python-mode mailing list Python-mode@python.org http://mail.python.org/mailman/listinfo/python-mode
Re: [Python-mode] [Bug 450552] Re: python-mode breaks for python 3
Rustom Mody wrote: On Tue, Oct 20, 2009 at 1:59 PM, Andreas Roehler andreas.roeh...@online.de mailto:andreas.roeh...@online.de wrote: Rustom wrote: (cmd (format exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n filename filename))) For me both of your variants are working, see output of checks below. uname -a python --version cat 2+4.py cat exec-read.py python exec-read.py cat exec-compile-read.py python exec-compile-read.py == Linux ... 2.6.22.19-0.2-default #1 SMP 2008-12-18 10:17:03 +0100 i686 athlon i386 GNU/Linux Python 2.5.1 #! /usr/bin/env python # -*- coding: utf-8 -*- print 2 + 4 ## #! /usr/bin/env python # -*- coding: utf-8 -*- exec(open('2+4.py').read()) ## 6 #! /usr/bin/env python # -*- coding: utf-8 -*- exec(compile(open('2+4.py').read(), '2+4.py', 'exec')) # 6 ; BTW can you tell whats the us of `compile' here for you? Do you have some tests for python-mode.el? What puzzles me still is a pure python question - do we need this `read()' here, i.e. if a file opened is delivered to exec, will it not being read anyway? Thanks See Guido's 2 to 3 doc http://docs.python.org/dev/3.0/whatsnew/3.0.html#removed-syntax says stream argument not taken Ah, thanks Andreas ___ Python-mode mailing list Python-mode@python.org http://mail.python.org/mailman/listinfo/python-mode
Re: [Python-mode] [Bug 450552] Re: python-mode breaks for python 3
Rustom wrote: On Thu, Oct 22, 2009 at 8:32 PM, Andreas Roehler andreas.roeh...@online.dewrote: ... Rustom wrote: (cmd (format exec(compile(open('%s').read(), '%s', 'exec')) # PYTHON-MODE\n filename filename))) For me both of your variants are working, see output of checks below. You probably need to try on windows. See http://bugs.python.org/issue5524 Hmm, AFAIU the use of `compile' here on windows is to signal an error if the file contains \r chars? Right? The way I understood it the exec want Unix-only lineendings and compile avoids the whole issue by supplying exec with a code object and not a compilable text. But I may be wrong Probably you are right - if the `\r'-error doesn't occur with `compile'. Think we should make a comment, saying compile here is introduced for this side-effect. Do you have some tests for python-mode.el? As in automated el/py etc? No As in biological? ... If you may deliver a simple test case, how to call interactively it from inside emacs, it might be helpful - at least for me... :) Not sure what you are asking for. Your emacs startup should contain something like (autoload 'py-shell python-mode Python Inferior Mode. t) (autoload 'python-mode python-mode Python Mode. t) (add-to-list 'auto-mode-alist '(\\.py\\' . python-mode)) (add-to-list 'interpreter-mode-alist '(python . python-mode)) Also assuming that python-mode.el is in your path After that opening a file with .py extension should start in python mode From there C-c ! should start the python interpreter After that (from the file buffer) C-c C-c should read the file into the python interpreter buffer. But as I said I am not sure what you are asking for :-) Sorry. Simply tried to catch the execution of your form with edebug. The ways I tried, it wasn't called. Thanks anyway. Andreas ___ Python-mode mailing list Python-mode@python.org http://mail.python.org/mailman/listinfo/python-mode