[IPython-dev] minor ipython.el news
Fernando Perez
Fernando.Perez at colorado.edu
Thu May 6 19:11:17 EDT 2004
Alexander Schmolck wrote:
>>Hmm. I poked around ultraTB.py, which is the traceback formatting code, and
>>it looks doable. However, not particularly straightforward at first sight :(
>>That code is rather convoluted, and I'm not super-familiar with it, since I
>>didn't originally write it myself (though I've modified it quite a bit). So
>>don't hold your breath on this one. However, if in your copious spare time
>>you come up with a nice patch for it, I'll gladly include it in ;-)
>
>
> I'll consider it (when I get time to hack python.el one more time, I'll also
> have a look at this; doesn't seem too much code although possibly not terribly
> great quality -- I get negative line numbers every now and so often -- that
> seems to happen if the file is rather short).
Yes, that code isn't great. Poorly commented (if at all) and with fairly
convoluted logic.
>>>>./script.py --title 'some title'
>>
>>This one looks more feasible: it's basically adding more sophisticated quoting
>>to @run than the simple .split() it currently does (I mean for the quoting,
>>globbing is even easier). I'll see if I can get this one in soonish, to at
>>least grant you one of your wishes :)
>
>
> Couldn't one just defer globbing and quoting to the shell (or have an extra
> @srun command that does that?)?
How does this sound?
In [1]: cat argv.py
#!/usr/bin/env python
import sys
print 'argv:',sys.argv
In [2]: run argv ~ *py
argv: ['argv.py', '/home/fperez', 'argv.py', 'die.py', 'div.py', 'error.py',
'exit.py', 'ramptest.py', 'scopes.py', 'strings.py', 't.py']
In [3]: !./argv.py ~ *py
argv: ['./argv.py', '/home/fperez', 'argv.py', 'die.py', 'div.py', 'error.py',
'exit.py', 'ramptest.py', 'scopes.py', 'strings.py', 't.py']
Here's the bit of code which does the magic:
# Try to have the underlying shell do all quoting for us
# This has one eval in it, which I don't see how to avoid, since we
# need to rebuild argv from the _string_ which comes from the shell
# Note that I print argv[1:] because the -c option is left in there as
# the first entry, even though the python man page says it shouldn't
# be there.
out,err = getoutputerror('%s -SEc "import sys;print sys.argv[1:]" %s'
% (sys.executable,arg_str))
# If there is any problem with the shell-based expansions, we punt and
# do a simple arg_str.split()
if err:
warn(err)
argv = arg_str.split()
elif not out.startswith('[') or not out.endswith(']'):
argv = arg_str.split()
else:
try:
argv = eval(out,{},{})
except:
argv = arg_str.split()
Let me know if you have any objections/comments. I'm not crazy about using an
eval, but on the other hand I don't want to build all of the logic which bash
has to parse/glob/quote a command line into an argv vector.
I've just uploaded this to CVS, in case you feel like playing with it.
Cheers,
f
ps: I gave you what you wanted, now you need to fix ultraTB's regexps :)
More information about the IPython-dev
mailing list