An Emacs mode for editing Python code

py-execute-import-or-reload bug with python3

Reported by Sara on 2012-01-30
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-mode.el
Medium
Andreas Roehler

Bug Description

Hi,

 I think there is a bug when running C-c RET using a python3 interpreter. The following are the steps to reproduce the error:

 1. Create a new python file, e.g. xxx.py

 2. C-u C-! and type 'python3'.

 3. Type any valid python expression, e.g. print("hello world"). There is no need to import any modules.

 4. C-c C-c (python3 correctly executes the file).

 5. C-c RET and press RET again. The last RET is bound to the command comint-send-input. To be precise, C-c RET moves me to the *Python3* buffer, where the cursor is preceded by '... ... ... ...'. Typing anything e.g. print("hello") followed by RET would result in the following error:
     File "<stdin>", line 5
       print("helo")
           ^
      SyntaxError: invalid syntax

 6. Results in the following error:

     Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     AttributeError: 'dict' object has no attribute 'has_key'

The following are my systems details:

 Emacs version: GNU Emacs 24.0.92.2 (x86_64-pc-linux-gnu)
 python-mode.el version: 6.0.4
 Python3 interpreter version: Python 3.1.4

The following is the output of M-x report-emacs-bug

 In GNU Emacs 24.0.92.2 (x86_64-pc-linux-gnu)
 of 2012-01-14 on localhost
configured using `configure '--prefix=/usr' '--build=x86_64-pc-linux-gnu' '--host=x86_64-pc-linux-gnu' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--datadir=/usr/share' '--sysconfdir=/etc' '--localstatedir=/var/lib' '--libdir=/usr/lib64' '--disable-dependency-tracking' '--program-suffix=-emacs-24' '--infodir=/usr/share/info/emacs-24' '--enable-locallisppath=/etc/emacs:/usr/share/emacs/site-lisp' '--with-crt-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/../../../../lib64' '--with-gameuser=games' '--without-compress-info' '--disable-maintainer-mode' '--without-hesiod' '--without-kerberos' '--without-kerberos5' '--with-gpm' '--with-dbus' '--without-gnutls' '--without-xml2' '--without-selinux' '--with-sound' '--without-x' 'build_alias=x86_64-pc-linux-gnu' 'host_alias=x86_64-pc-linux-gnu' 'CFLAGS=-O2 -pipe -march=native -mtune=native' 'LDFLAGS=-Wl,-O1 -Wl,--as-needed''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: nil
  value of $XMODIFIERS: nil
  locale-coding-system: nil
  default enable-multibyte-characters: t

Major mode: Comint

Minor modes in effect:
  ido-everywhere: t
  shell-dirtrack-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
ESC [ > 0 ; 1 1 5 ; 0 c x C-x 1 C-x C-f / t m p RET
s a m p RET C-x C-s C-u C-c ! 3 RET C-x o C-c C-c C-x
o C-c RET RET ESC x r e p o TAB r TAB RET

Recent messages:
("/usr/bin/emacs")
Loading ido...done
OVERVIEW
(No changes need to be saved)
Python3
Wrote /tmp/Python3164736yE.py
(No files need saving)
Making completion list...

Load-path shadows:
/etc/emacs/site-start hides /usr/share/emacs/site-lisp/site-start

Features:
(shadow sort mail-extr message rfc822 mml mml-sec mm-decode mm-bodies
mm-encode mail-parse rfc2231 rfc2047 rfc2045 ietf-drums mailabbrev
mail-utils gmm-utils mailheader emacsbug help-mode view imenu org-indent
org-wl org-w3m org-vm org-rmail org-mhe org-mew org-irc org-jsinfo
org-infojs org-html org-exp ob-exp org-exp-blocks org-info org-gnus
org-docview org-bibtex bibtex org-bbdb tramp-cache tramp tramp-compat
auth-source eieio gnus-util mm-util mail-prsvr password-cache
format-spec tramp-loaddefs org-colview clojure-mode-autoloads
paredit-autoloads package tabulated-list zenburn-theme ido cus-start
cus-load cal-iso org-agenda remember org-remember org-datetree org
byte-opt warnings bytecomp byte-compile cconv macroexp ob-emacs-lisp
ob-tangle ob-ref ob-lob ob-table org-footnote org-src ob-comint ob-keys
ob ob-eval org-pcomplete org-list org-faces org-compat org-entities
org-macs time-date noutline outline easy-mmode cal-menu calendar
cal-loaddefs python-mode rx advice advice-preload derived edmacro kmacro
highlight-indentation help-fns cc-cmds cc-engine ansi-color compile
tool-bar cl site-gentoo ess-toolbar ess-mouse mouseme thingatpt
browse-url ess-menu ess-swv ess-noweb noweb-font-lock-mode ess-bugs-l
essd-els ess-sas-d ess-sas-l ess-sas-a executable shell pcomplete
ess-arc-d ess-vst-d ess-xls-d ess-lsp-l ess-sta-d ess-sta-l cc-vars
cc-defs make-regexp ess-sp6-d ess-sp5-d ess-sp3-d ess-r-d ess-r-args
assoc ess-s-l ess-inf ess-utils comint ring ess-mode noweb-mode ess
ess-custom regexp-opt easymenu ess-compat ess-site ediff-hook vc-hooks
lisp-float-type lisp-mode register page menu-bar rfn-eshadow timer
select mouse jit-lock font-lock syntax facemenu font-core frame cham
georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese hebrew greek romanian slovak czech european ethiopic
indian cyrillic chinese case-table epa-hook jka-cmpr-hook help simple
abbrev minibuffer loaddefs button faces cus-face files text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process dbusbind
multi-tty emacs)

Am 30.01.2012 03:36, schrieb Sara:
> Public bug reported:
>
> Hi,
>
> I think there is a bug when running C-c RET using a python3
> interpreter. The following are the steps to reproduce the error:
>
> 1. Create a new python file, e.g. xxx.py
>
> 2. C-u C-! and type 'python3'.
>
> 3. Type any valid python expression, e.g. print("hello world")
>
> 4. C-c C-c (python3 correctly executes the file).
>
> 5. C-c RET and press RET again.
>
> 6. Results in the following error:
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in<module>
> AttributeError: 'dict' object has no attribute 'has_key'
>
> The following are my systems details:
>
> Emacs version: GNU Emacs 24.0.92.2 (x86_64-pc-linux-gnu)
> python-mode.el version: 6.0.4
> Python3 interpreter version: Python 3.1.4
>

Hi,

can you tell which modules should be imported?
What does the last RET, i.e. which command then is bound to?
Did you return to original buffer, resp. stayed there?

Please send the output from M-x report-emacs-bug.

Thanks,

Andreas

Sara (sarahlish18) wrote :

This is a sample of a python script that causes the reported error.

Sara (sarahlish18) wrote :

This is a sample of a python script that will result in the reported error.

 I am sorry for the double posting. Is it possible to delete a previous comment?

description: updated
Andreas Roehler (a-roehler) wrote :

don't worry. These kind of mistakes are common.

Seems you call a command in the wrong environment.
C-c RET is meant for common input AFAIU, i.e. it should retrieve the previous input in shell.

Executing Python code from a text buffer is not common so far :)

BTW that commands seems broken in a shell too, does nothing here.
Will make a bug report for this.

So far,

Andreas

hmm, wait, now forget my previous post please :)

Seeing the bug,

Andreas

Changed in python-mode:
assignee: nobody → Andreas Roehler (a-roehler)
milestone: none → 6.0.5
importance: Undecided → Medium
status: New → Confirmed
Andreas Roehler (a-roehler) wrote :

Hi,

changed several things, could you try again with current trunk?

Thanks,

Andreas

Changed in python-mode:
status: Confirmed → In Progress
Changed in python-mode:
status: In Progress → Fix Committed
Sara (sarahlish18) wrote :
Download full text (5.3 KiB)

Hi Andreas,

 I checked out the current trunk last night using the following command:

  bzr branch lp:python-mode # This is the correct command, isn't it? My py-version is now 6.0.5

 So, redoing the steps reported earlier leads to a new error:

   or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.

This does the following:
 - look for an interpreter with `py-choose-shell-by-shebang'
 - examine imports using `py-choose-shell-by-import'
 - if not successful, return default value of `py-shell-name'

With \\[universal-argument]) user is prompted to specify a reachable Python version." (interactive "P") (let ((erg (cond ((eq 4 (prefix-numeric-value arg)) (read-from-minibuffer "Python Shell\
: " py-shell-name)) (py-use-local-default (if (not (string= "" py-shell-local-path)) (expand-file-name py-shell-local-path) (message "Abort: `py-use-local-default' is set to `t' but `py-shell\
-local-path' is empty. Maybe call `py-toggle-local-default-use'"))) ((py-choose-shell-by-shebang)) ((py-choose-shell-by-import)) (t (default-value (quote py-shell-name)))))) (when (interactiv\
e-p) (message "%s" erg)) (setq py-shell-name erg) erg)), 3
Using `python-mode' version 6.0.5
You can run the command `py-version' with C-c C-v
or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.

This does the following: ...

Read more...

Changed in python-mode:
status: Fix Committed → In Progress
Andreas Roehler (a-roehler) wrote :

Am 02.02.2012 04:35, schrieb Sara:
> Hi Andreas,
>
Hi,

> I checked out the current trunk last night using the following command:
>
> bzr branch lp:python-mode # This is the correct command, isn't it?

yes, thanks.

> My py-version is now 6.0.5

correct. it should be higher the released version.

>
> So, redoing the steps reported earlier leads to a new error:
>
> or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.

fixed, thanks

>
> This does the following:
> - look for an interpreter with `py-choose-shell-by-shebang'
> - examine imports using `py-choose-shell-by-import'
> - if not successful, return default value of `py-shell-name'
>
> With \\[universal-argument]) user is prompted to specify a reachable Python version." (interactive "P") (let ((erg (cond ((eq 4 (prefix-numeric-value arg)) (read-from-minibuffer "Python Shell\
> : " py-shell-name)) (py-use-local-default (if (not (string= "" py-shell-local-path)) (expand-file-name py-shell-local-path) (message "Abort: `py-use-local-default' is set to `t' but `py-shell\
> -local-path' is empty. Maybe call `py-toggle-local-default-use'"))) ((py-choose-shell-by-shebang)) ((py-choose-shell-by-import)) (t (default-value (quote py-shell-name)))))) (when (interactiv\
> e-p) (message "%s" erg)) (setq py-shell-name erg) erg)), 3
> Using `python-mode' version 6.0.5
> You can run the command `py-version' with C-c C-v
> or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.
>
> This does the following:
> - look for an interpreter with `py-choose-shell-by-shebang'
> - examine imports using `py-choose-shell-by-import'
> - if not successful, return default value of `py-shell-name'
>
> With \\[universal-argument]) user is prompted to specify a reachable Python version." (interactive "P") (let ((erg (cond ((eq 4 (prefix-numeric-value arg)) (read-from-minibuffer "Python Shell\
> : " py-shell-name)) (py-use-local-default (if (not (string= "" py-shell-local-path)) (expand-file-name py-shell-local-path) (message "Abort: `py-use-local-default' is set to `t' but `py-shell\
> -local-path' is empty. Maybe call `py-toggle-local-default-use'"))) ((py-choose-shell-by-shebang)) ((py-choose-shell-by-import)) (t (default-value (quote py-shell-name)))))) (when (interactiv\
> e-p) (message "%s" erg)) (setq py-shell-name erg) erg)), 3
>
> The solution is to amend line 7685 of python-mode.el to:
> (defun py-choose-shell (&rest arg)
>
> But this still results in the same error. I found that the solution is to amend line 5753 of python-mode.el to:
> (format "import sys,imp\nif '%s' in sys.modules:\n imp.reload(%s)\nelse:\n import %s\n"
> m m m))
>
> The reason is that has_key and reload has been removed in python3
> (http://docs.python.org/release/3.0.1/whatsnew/3.0.html)

applied, thanks a lot!

BTW don't hesitate to make a feature request, should you feel
python-mode could deliver something useful for you.

Andreas

>
> Doing the aforementioned changes fixes the bug on my PC.
>

Changed in python-mode:
status: In Progress → Fix Committed
Sara (sarahlish18) wrote :
Download full text (9.2 KiB)

Hi Andreas,

 I have updated my branch and redid the aforementioned steps using
both python 2.7 and python 3.

 I confirm that this bug is fixed.

 Thank you for your help and time.

On Thu, Feb 2, 2012 at 3:29 PM, Andreas Roehler
<email address hidden> wrote:
> Am 02.02.2012 04:35, schrieb Sara:
>> Hi Andreas,
>>
> Hi,
>
>>   I checked out the current trunk last night using the following command:
>>
>>    bzr branch lp:python-mode    # This is the correct command, isn't it?
>
> yes, thanks.
>
>> My py-version is now 6.0.5
>
> correct. it should be higher the released version.
>
>>
>>   So, redoing the steps reported earlier leads to a new error:
>>
>>     or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.
>
> fixed, thanks
>
>>
>> This does the following:
>>   - look for an interpreter with `py-choose-shell-by-shebang'
>>   - examine imports using `py-choose-shell-by-import'
>>   - if not successful, return default value of `py-shell-name'
>>
>> With \\[universal-argument]) user is prompted to specify a reachable Python version." (interactive "P") (let ((erg (cond ((eq 4 (prefix-numeric-value arg)) (read-from-minibuffer "Python Shell\
>> : " py-shell-name)) (py-use-local-default (if (not (string= "" py-shell-local-path)) (expand-file-name py-shell-local-path) (message "Abort: `py-use-local-default' is set to `t' but `py-shell\
>> -local-path' is empty. Maybe call `py-toggle-local-default-use'"))) ((py-choose-shell-by-shebang)) ((py-choose-shell-by-import)) (t (default-value (quote py-shell-name)))))) (when (interactiv\
>> e-p) (message "%s" erg)) (setq py-shell-name erg) erg)), 3
>> Using `python-mode' version 6.0.5
>> You can run the command `py-version' with C-c C-v
>> or: Wrong number of arguments: (lambda (&optional arg) "Looks for an appropriate mode function.
>>
>> This does the following:
>>   - look for an interpreter with `py-choose-shell-by-shebang'
>>   - examine imports using `py-choose-shell-by-import'
>>   - if not successful, return default value of `py-shell-name'
>>
>> With \\[universal-argument]) user is prompted to specify a reachable Python version." (interactive "P") (let ((erg (cond ((eq 4 (prefix-numeric-value arg)) (read-from-minibuffer "Python Shell\
>> : " py-shell-name)) (py-use-local-default (if (not (string= "" py-shell-local-path)) (expand-file-name py-shell-local-path) (message "Abort: `py-use-local-default' is set to `t' but `py-shell\
>> -local-path' is empty. Maybe call `py-toggle-local-default-use'"))) ((py-choose-shell-by-shebang)) ((py-choose-shell-by-import)) (t (default-value (quote py-shell-name)))))) (when (interactiv\
>> e-p) (message "%s" erg)) (setq py-shell-name erg) erg)), 3
>>
>> The solution is to amend line 7685 of python-mode.el to:
>>    (defun py-choose-shell (&rest arg)
>>
>> But this still results in the same error. I found that the solution is to amend line 5753 of python-mode.el to:
>>    (format "import sys,imp\nif '%s' in sys.modules:\n imp.reload(%s)\nelse:\n import %s\n"
>>                                           m m m))
>>
>> The reason is that has_key and reload has been removed in python3
>> (http://docs.python.org/release/3.0.1...

Read more...

Changed in python-mode:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers

Bug attachments