error message "str object not callable" when not calling string

Bug #355837 reported by shaunalynn
2
Affects Status Importance Assigned to Milestone
IPython
Fix Released
Low
Unassigned

Bug Description

There have been a few times where, when writing a for-loop, I will get a "'str' object is not callable" when I expect just to hit RET and begin writing the rest of the loop. The problem fixes itself when I log out and back ino ipython. I thought it might be related to defining a python name like "dict" in the example below, but I don't think so. Below is a transcript of the last occurrence.

I am using ipython 0.8.4 on an IBM Thinkpad x31 running Ubuntu Ibex.

In [31]: dict = {'a':'b','c':'d'}

In [32]: str = 'wqe'

In [33]: for i,j in dict:
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
KeyboardInterrupt

In [33]: dict
Out[34]: {'a': 'b', 'c': 'd'}

In [35]: i
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/shaunalynn/<ipython console> in <module>()

NameError: name 'i' is not defined

In [36]: j
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/shaunalynn/<ipython console> in <module>()

NameError: name 'j' is not defined

In [37]: for i, j in dict:
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
KeyboardInterrupt

In [37]: dictionary = {'a':'b','c','d'}
------------------------------------------------------------
   File "<ipython console>", line 1
     dictionary = {'a':'b','c','d'}
                              ^
SyntaxError: invalid syntax

In [39]: dictionary = {'a':'b','c'"'d'}
------------------------------------------------------------
   File "<ipython console>", line 1
     dictionary = {'a':'b','c'"'d'}
                                  ^
SyntaxError: EOL while scanning single-quoted string

In [40]: dictionary = {'a':'b','c':'d'}

In [41]: for i, j in dictionary:
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
<ERROR: 'str' object is not callable>
KeyboardInterrupt

In [41]:

In [42]: exit()
Do you really want to exit ([y]/n)?
shaunalynn@duffles:~$ ipython
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.4 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: dict = {'a':'b','c':'d'}

In [2]: for i, j in dict:
   ...: print i,j

Related branches

Revision history for this message
Brian Granger (ellisonbg) wrote : Re: [Bug 355837] [NEW] error message "str object not callable" when not calling string
Download full text (6.8 KiB)

str is a Python built-in. You reassign it to a string. Because
IPython uses the str builtin internally, things go bad in a serious
way. This is probably called by readline as you type. This is not an
IPython bug. Just don't use built-in names for your own variables.

Cheers,

Brian

On Sun, Apr 5, 2009 at 1:51 PM, shaunalynn <email address hidden> wrote:
> Public bug reported:
>
> There have been a few times where, when writing a for-loop, I will get a
> "'str' object is not callable" when I expect just to hit RET and begin
> writing the rest of the loop. The problem fixes itself when I log out
> and back ino ipython. I thought it might be related to defining a python
> name like "dict" in the example below, but I don't think so. Below is a
> transcript of the last occurrence.
>
> I am using ipython 0.8.4 on an IBM Thinkpad x31 running Ubuntu Ibex.
>
>
> In [31]: dict = {'a':'b','c':'d'}
>
> In [32]: str = 'wqe'
>
> In [33]: for i,j in dict:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> KeyboardInterrupt
>
> In [33]: dict
> Out[34]: {'a': 'b', 'c': 'd'}
>
> In [35]: i
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /home/shaunalynn/<ipython console> in <module>()
>
> NameError: name 'i' is not defined
>
> In [36]: j
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /home/shaunalynn/<ipython console> in <module>()
>
> NameError: name 'j' is not defined
>
> In [37]: for i, j in dict:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> KeyboardInterrupt
>
> In [37]: dictionary = {'a':'b','c','d'}
> ------------------------------------------------------------
>   File "<ipython console>", line 1
>     dictionary = {'a':'b','c','d'}
>                              ^
> SyntaxError: invalid syntax
>
>
> In [39]: dictionary = {'a':'b','c'"'d'}
> ------------------------------------------------------------
>   File "<ipython console>", line 1
>     dictionary = {'a':'b','c'"'d'}
>                                  ^
> SyntaxError: EOL while scanning single-quoted string
>
>
> In [40]: dictionary = {'a':'b','c':'d'}
>
> In [41]: for i, j in dictionary:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> KeyboardInterrupt
>
> In [41]:
>
> In [42]: exit()
> Do you really want to exit ([y]/n)?
> shaunalynn@duffles:~$ ipython
> Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.8.4 -- An enhanced Interactive Python.
> ?         -> Introduction and overview of IPython's features.
> %quickref -> Quick referenc...

Read more...

Revision history for this message
Robert Kern (robert-kern) wrote :

On Sun, Apr 5, 2009 at 18:34, Brian Granger <email address hidden> wrote:
> str is a Python built-in.  You reassign it to a string.  Because
> IPython uses the str builtin internally, things go bad in a serious
> way.  This is probably called by readline as you type.  This is not an
> IPython bug.  Just don't use built-in names for your own variables.

It's probably worth looking into, though. Why does IPython evaluate
'str' in the user's namespace? Assigning to 'str' in the user's
namespace certainly doesn't affect the __builtins__ (if it did, that's
definitely a bug!)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

Revision history for this message
Brian Granger (ellisonbg) wrote :
Download full text (3.9 KiB)

> It's probably worth looking into, though. Why does IPython evaluate
> 'str' in the user's namespace? Assigning to 'str' in the user's
> namespace certainly doesn't affect the __builtins__ (if it did, that's
> definitely a bug!)

Yes, that is true. I guess it really depends on how IPython is
calling str and that is worth looking at.

Brian

> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>  -- Umberto Eco
>
> --
> error message "str object not callable" when not calling string
> https://bugs.launchpad.net/bugs/355837
> You received this bug notification because you are a member of IPython
> Developers, which is subscribed to IPython.
>
> Status in IPython - Enhanced Interactive Python: New
>
> Bug description:
> There have been a few times where, when writing a for-loop, I will get a "'str' object is not callable" when I expect just to hit RET and begin writing the rest of the loop. The problem fixes itself when I log out and back ino ipython. I thought it might be related to defining a python name like "dict" in the example below, but I don't think so. Below is a transcript of the last occurrence.
>
> I am using ipython 0.8.4 on an IBM Thinkpad x31 running Ubuntu Ibex.
>
>
> In [31]: dict = {'a':'b','c':'d'}
>
> In [32]: str = 'wqe'
>
> In [33]: for i,j in dict:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> KeyboardInterrupt
>
> In [33]: dict
> Out[34]: {'a': 'b', 'c': 'd'}
>
> In [35]: i
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /home/shaunalynn/<ipython console> in <module>()
>
> NameError: name 'i' is not defined
>
> In [36]: j
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
>
> /home/shaunalynn/<ipython console> in <module>()
>
> NameError: name 'j' is not defined
>
> In [37]: for i, j in dict:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> KeyboardInterrupt
>
> In [37]: dictionary = {'a':'b','c','d'}
> ------------------------------------------------------------
>   File "<ipython console>", line 1
>     dictionary = {'a':'b','c','d'}
>                              ^
> SyntaxError: invalid syntax
>
>
> In [39]: dictionary = {'a':'b','c'"'d'}
> ------------------------------------------------------------
>   File "<ipython console>", line 1
>     dictionary = {'a':'b','c'"'d'}
>                                  ^
> SyntaxError: EOL while scanning single-quoted string
>
>
> In [40]: dictionary = {'a':'b','c':'d'}
>
> In [41]: for i, j in dictionary:
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not callable>
> <ERROR: 'str' object is not...

Read more...

Changed in ipython:
importance: Undecided → Low
status: New → Confirmed
Revision history for this message
Fernando Perez (fdo.perez) wrote :

I should add that simply typing

del str

at the IPython prompt will get you out of this situation if it ever happens.

Revision history for this message
Fernando Perez (fdo.perez) wrote :

The problem is due to how IPython allows for dynamic prompts. The prompt expression must be eval'ed in the user namespace, so you can put what you want in that prompt. But that means that the user namespace can't contain 'str' as a variable, because then doing

str(prompt_expr)

won't work when executed in the user's namespace.

That expression is by default:

<ItplNS '${self.col_p2}.${"."*len(str(self.cache.prompt_count))}.: $self.col_norm' >)

so if you also define 'len' to be something other than the builtin, you get a similar error.

I'm leaving these comments in here for the record, so we understand the problem better. I'm not sure it's easy to make a 100% robust dynamic prompt system, since for it to be truly dynamic you need to eval in the user's namespace arbitrary expressions, hence running into whatever funny redefinitions the user may have applied.

Revision history for this message
Robert Kern (robert-kern) wrote : Re: [Bug 355837] Re: error message "str object not callable" when not calling string

On Tue, Apr 7, 2009 at 19:08, Fernando Perez <email address hidden> wrote:
> The problem is due to how IPython allows for dynamic prompts.  The
> prompt expression must be eval'ed in the user namespace, so you can put
> what you want in that prompt.  But that means that the user namespace
> can't contain 'str' as a variable, because then doing
>
> str(prompt_expr)
>
> won't work when executed in the user's namespace.
>
> That expression is by default:
>
> <ItplNS '${self.col_p2}.${"."*len(str(self.cache.prompt_count))}.:
> $self.col_norm' >)
>
> so if you also define 'len' to be something other than the builtin, you
> get a similar error.

__builtins__.len(__builtins__.str(...)) perhaps?

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

Revision history for this message
Fernando Perez (fdo.perez) wrote :

On Tue, Apr 7, 2009 at 5:51 PM, Robert Kern
<email address hidden> wrote:

> __builtins__.len(__builtins__.str(...)) perhaps?

I'd dismissed this because ultimately people can stick whatever they
want in there, so the number of ways in which it can fail is basically
unbounded.

But on second thought, it's probably not such a bad idea, since at
least it does close the default hole. People can still break things
with a combination of custom dynamic prompts and redefining things,
but that's likely to be much, much less common.

I've put it in my branch in this format....

Cheers,

f

Revision history for this message
Fernando Perez (fdo.perez) wrote :

The fix in http://bazaar.launchpad.net/~fdo.perez/ipython/trunk-dev/revision/1191 is not perfect, since it only covers len and str, as per my previous message. But it's probably good enough for 99% of the users...

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

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.