[IPython-user] Windows XP pyreadline issue with tab completion directory separator character

TP wingusr@gmail....
Sun Feb 21 10:01:12 CST 2010


Using Windows XP, IPython 0.10, Python 2.6.4, pyreadline from
pyreadline-1.5-win32-setup.exe.

Given testreadline.py is:

    import sys
    def main ():
	filename = sys.argv[1]
	print filename

    if __name__ == '__main__' :
	main()

When using tab completion I get:

    In [7]: run testreadline.py subdir\ [press TAB]
    subdir\0001.html subdir\0003.html subdir\0005.html
    subdir\0002.html subdir\0004.html

    In [7]: run testreadline.py subdir\0001 [press TAB]

    In [7]: run testreadline.py subdir\0001.html [press ENTER]
    subdir0001.html

If I manually fix the \ to / I get:

    In [8]: run testreadline.py subdir/0001.html
    subdir/0001.html

If I run testreadline.py from a normal command prompt:

    python26 testreadline.py subdir\0001.html
    subdir\0001.html

Why the different behavior of treating \ in args passed to run?

Anyway, I got around the problem by hacking pyreadline\modes\basemode.py.

At the top I put:

    #Attempt to see if we are running within IPython
    if sys.modules.get("IPython", None) is None:
	dirsep = os.sep
    else:
	dirsep = "/"

And then I changed part of

    def _get_completions(self):

from:

    completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'))
    if self.mark_directories == 'on':
	mc = []
	for f in completions:
	    if os.path.isdir(f):
		mc.append(f + os.sep)
	    else:
		mc.append(f)
	completions = mc

to:

    completions = map(ensure_unicode, glob.glob(os.path.expanduser(text) + '*'))
    if os.sep != dirsep:
	completions = [f.replace(os.sep, dirsep) for f in completions]
    if self.mark_directories == 'on':
	mc = []
	for f in completions:
	    if os.path.isdir(f):
		mc.append(f + dirsep)
	    else:
		mc.append(f)
	completions = mc

This seems to work but I don't know if it's the best way to handle the
problem?


More information about the IPython-user mailing list