Re: [Bug 450552] Re: python-mode breaks for python 3

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Andreas Röhler-2
Rustom wrote:

> Right now the mc Im on does not have python 2.x and python3
>
> But when I was on a debian box with 2.5 and 3
> changing
>
> (cmd (format "execfile(r'%s') # PYTHON-MODE\n" filename)))
>
> to
>
> (cmd (format "exec(compile(open('%s').read(), '%s', 'exec')) #
> PYTHON-MODE\n" filename filename)))
>
> [ not
>
> (cmd (format "exec(open(r'%s').read()) # PYTHON-MODE\n" filename)))
> as my original post suggested]
>
> seemed to work for both 2.5 and 3
>
> Whether other changes from 2.x to 3 will introduce breakage in python-
> mode I dont know
>
> The more important question remaining about my suggested fix is about windows.
> Evidently there is some stupidity that exec expects its input to have unix line endings even on windows so the original code may not work on windows but the one with compile should.
>
> But Ive not tested on windows
>


Hi Rustom,

looks like good news. Think you are right.

Checked a little bit with
Python 2.5.1
on Linux 2.6.22.19-0.2-default #1 SMP 2008-12-18 10:17:03 +0100 i686 athlon i386 GNU/Linux

works fine AFAIS.

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

Andreas

--
https://code.launchpad.net/s-x-emacs-werkstatt/
http://bazaar.launchpad.net/~a-roehler/python-mode/python-mode.el/



_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Rustom Mody
On Tue, Oct 20, 2009 at 1:59 PM, Andreas Roehler <[hidden email]> wrote:
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
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Andreas Röhler-2
Rustom Mody wrote:
> On Tue, Oct 20, 2009 at 1:59 PM, Andreas Roehler
> <[hidden email] <mailto:[hidden email]>> 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
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Rustom Mody
On Thu, Oct 22, 2009 at 5:43 PM, Andreas Roehler <[hidden email]> wrote:
Rustom Mody wrote:
> On Tue, Oct 20, 2009 at 1:59 PM, Andreas Roehler
> <[hidden email] <mailto:[hidden email]>> 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.

You probably need to try on windows.
See
http://bugs.python.org/issue5524
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?
As in automated el/py etc? No
As in biological? ...
Heres yours truly :-)

_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Andreas Röhler-2
...
>     >     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?


>
>     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?
>
> See
> http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0
>
>  
>
>     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... :)


Andreas

> Heres yours truly :-)

_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Rustom Mody
On Thu, Oct 22, 2009 at 8:32 PM, Andreas Roehler <[hidden email]> 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.
>
> 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

>     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 :-)


_______________________________________________
Python-mode mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Andreas Röhler-2
Rustom wrote:

> On Thu, Oct 22, 2009 at 8:32 PM, Andreas Roehler
> <[hidden email]>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.
>>>
>>> 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
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Rustom Mody
On Fri, Oct 23, 2009 at 12:50 AM, Andreas Roehler
<[hidden email]> 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
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode
Reply | Threaded
Open this post in threaded view
|

Re: [Bug 450552] Re: python-mode breaks for python 3

Andreas Röhler-2
...

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
[hidden email]
http://mail.python.org/mailman/listinfo/python-mode