Overzealous introspection #100

Closed
ipython opened this Issue May 10, 2010 · 5 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented May 10, 2010

Original Launchpad bug 343992: https://bugs.launchpad.net/ipython/+bug/343992
Reported by: fdo.perez (Fernando Perez).

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.

@ghost

ghost commented May 10, 2010

[ LP comment 1 by: Jon Guyer, on 2009-09-09 19:15:26.909552+00:00 ]

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?

@ghost

ghost commented May 10, 2010

[ LP comment 2 by: Fernando Perez, on 2009-09-10 06:27:26+00:00 ]

Hi Jon,

On Wed, Sep 9, 2009 at 12:15 PM, Jon Guyer guyer@nist.gov 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

@ghost

ghost commented May 10, 2010

[ LP comment 3 by: Jon Guyer, on 2009-09-14 15:26:16.685303+00:00 ]

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.

@ghost

ghost commented May 10, 2010

[ LP comment 4 by: Fernando Perez, on 2009-09-14 20:59:22+00:00 ]

On Mon, Sep 14, 2009 at 8:26 AM, Jon Guyer guyer@nist.gov 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

Owner

minrk commented Mar 9, 2011

This is not a problem (as far as I can tell) on 0.11

@minrk minrk closed this Mar 23, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment