Skip to content

%run fails when single-quoted filename contains escaped single quote #1123

jdmarch opened this Issue Dec 8, 2011 · 7 comments

5 participants

jdmarch commented Dec 8, 2011

[Updated to reflect that this is only true of single, not double quotes.]

Apparent bug in %run (or perhaps more generally in magic). The following fails:

%run 'a \''
IPython member

More specifically, it fails with a "ValueError: no closing quotation" from shlex:

jdmarch commented Dec 10, 2011

Changed the title to be more accurate. For the reason described by @minrk at #1109 (posix asymmetry between single and double quotes), this only fails when the filename is single-quoted, not double.

Therefore a workaround is to always use double quotes if the filename contains single quotes, whether or not it also contains double quotes (which can be escaped without failure).

jdmarch commented Dec 10, 2011

For the record, PR #1130 removes the shlex ValueError but does not solve the issue (run magic truncates filename at first escaped single quote)

IPython member
minrk commented Dec 10, 2011

Right, and it may even be wrong to prevent the ValueError, because escaping quotes in a single-quoted string is invalid (as long as we are using shlex).


I don't think there is anything to do here. python 'a \'' won't work on the command line either.

Solution is to use double quotes, or the ugly but functional 'a '"'"''.

jdmarch commented Aug 12, 2012

It seems to come down to what mental model we ask users to adopt. At first glance, it would seem more user-friendly to be able to interpret any valid python string here, but perhaps we should just be clear that this is a posix filename string, not a python string, and make sure that the documentation and help are very clear about that. (Though how we get "very clear" about the mush between posix and windows filenames is another issue.)

IPython member
fperez commented Aug 13, 2012

While I agree that the posix/window situation is a bit messy, indeed the interpretation of text in the line of magic calls, and especially the one for %run, leans far closer to the semantics of a posix command-line than that of a python string.

So if anything, I suggest that the way to close this bug would just be an improvement to the %run docstring emphasizing this point, perhaps along with a note in the main magics docs (could just be in %magic itself) indicating that in general, users shouldn't expect python semantics in magic calls, since each magic ultimately decides what semantics to impose on an argument it receives as a pure string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.