IPython - Enhanced Interactive Python

Overzealous introspection

Reported by Fernando Perez on 2009-03-17
2
Affects Status Importance Assigned to Milestone
IPython
Confirmed
Undecided
Unassigned

Bug Description

See http://mail.scipy.org/pipermail/ipython-dev/2009-January/004812.html for details.

In a nuthsell:

In [2]: x="""
  ...: Eh?
Object `Eh` not found.
  ...: """

And now, the string is actually empty...

In [3]: x
Out[3]: '\n\n'

The problem is that the introspection magic is kicking in inside of the string declaration.

This isn't 100% trivial to fix, because we do want magics in multiline input (that's been a feature for a long time). But I suppose we do need to blacklist a few ones, like the '?' behavior.

Jon Guyer (guyer) wrote :

A user just brought a related issue to our attention. We have an example that uses Python string substitution to build up a command for an external program (gmsh). See <http://matforge.org/fipy/browser/branches/version-2_0/examples/diffusion/circle.py#L70>.

When these lines are pasted into ipython, although everything seems to work, the string is garbled by %magic, and ultimately a very confusing error is thrown by our gmsh interpreter. For instance:

In [14]: s = """
   ....: %(radius)g;
   ....: """ % locals()

In [15]: s
Out[15]: '\n_ip.magic("1; ")\n'

whereas:

In [12]: s = "%(radius)g" % locals()

In [13]: s
Out[13]: '1'

Is there any way to get Python string substitution syntax to override %magic commands in multiline strings?

Hi Jon,

On Wed, Sep 9, 2009 at 12:15 PM, Jon Guyer <email address hidden> wrote:
> A user just brought a related issue to our attention. We have an example
> that uses Python string substitution to build up a command for an
> external program (gmsh). See
> <http://matforge.org/fipy/browser/branches/version-2_0/examples/diffusion/circle.py#L70>.
>
> When these lines are pasted into ipython, although everything seems to
> work, the string is garbled by %magic, and ultimately a very confusing
> error is thrown by our gmsh interpreter. For instance:
>
> In [14]: s = """
>   ....: %(radius)g;
>   ....: """ % locals()
>
> In [15]: s
> Out[15]: '\n_ip.magic("1; ")\n'
>
> whereas:
>
> In [12]: s = "%(radius)g" % locals()
>
> In [13]: s
> Out[13]: '1'
>
> Is there any way to get Python string substitution syntax to override
> %magic commands in multiline strings?

I think we should make the magic system smarter, but in the meantime,
you can eliminate the problem by disabling magics and other special
syntax in multiline statements. Simply set the config option

multi_line_specials 0

in your ~/.ipython/ipythonrc file. If you are using the
ipy_user_conf.py config file instead, set

ip.options.multiline_specials = 0

Let me know how this works.

Cheers,

f

Jon Guyer (guyer) wrote :

That does the trick, thanks.

I guess it's too late in the game to choose some other magic character that doesn't conflict with Python.

Fernando Perez (fdo.perez) wrote :

On Mon, Sep 14, 2009 at 8:26 AM, Jon Guyer <email address hidden> wrote:
> That does the trick, thanks.
>
> I guess it's too late in the game to choose some other magic character
> that doesn't conflict with Python.

Well, the good thing is that in python 3.x, they are going to
deprecate % formatting in favor of the new style using {} placeholders
and a .format() method for strings. So this issue will fade away as
we transition to 3.x in the long run.

However, I'm keeping this bug open because we may also be able to make
ipython smarter so the magic rewriting does not kick in for multiline
strings. It's still our bug, and we should fix it.

But I'm glad to be able to have a workaround for now.

Cheers,

f

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

Other bug subscribers