autojump icon indicating copy to clipboard operation
autojump copied to clipboard

Crash on Windows due to improper backslash handling in autojump_match.py

Open agorgl opened this issue 8 years ago • 8 comments

Executing any normal autojump command in Windows leads to this:

Traceback (most recent call last):
  File "C:\Program Files (x86)\CowShell\Vendor\AutoJump\bin\\autojump", line 320, in <module>
    sys.exit(main(parse_arguments()))
  File "C:\Program Files (x86)\CowShell\Vendor\AutoJump\bin\\autojump", line 314, in main
    ['.'])))
  File "C:\Program Files (x86)\CowShell\Vendor\AutoJump\bin\autojump_utils.py", line 42, in first
    return it.next()
  File "C:\Program Files (x86)\CowShell\Vendor\AutoJump\bin\autojump_match.py", line 86, in <lambda>
    flags=regex_flags,
  File "F:\Programs\Python\lib\re.py", line 146, in search
    return _compile(pattern, flags).search(string)
  File "F:\Programs\Python\lib\re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: unexpected end of regular expression
ECHO is off.

The highlighted change here: https://github.com/wting/autojump/commit/7c7865ea7ecfd937284f774fb0818c5cc10c340a#diff-4b97c2fd2ae952c567c1646bc80e5d43L78 lead to the dysfunction in Windows systems.

The proposed solution is basically changing the lines 78-80 in autojump_match.py to:

    sep = '\\\\' if os.sep == '\\' else os.sep
    regex_no_sep = '[^' + sep + ']*'
    regex_no_sep_end = regex_no_sep + '$'
    regex_one_sep = regex_no_sep + sep + regex_no_sep

(Conditionally setting the separator value used in the regex construction)

agorgl avatar Jul 31 '16 23:07 agorgl

Hey thanks for the detailed bug report!

Unfortunately I don't have access to a Windows machine. Can you update these Windows tests such that they reproduce the error you have? That way we can prevent future regression bugs.

wting avatar Aug 01 '16 00:08 wting

Just trying out autojump on Win10 and hit this. The proposed patch seems to work for me.

glucas avatar Aug 25 '16 21:08 glucas

@wting If the tests are not actually executed on Windows, won't the match_consecutive tests pick up the wrong os.sep? The tests won't actually fail unless os.sep is a backslash.

glucas avatar Aug 29 '16 14:08 glucas

You can mock it out for tests like so:

In [1]: import os, mock

In [2]: os.sep
Out[2]: '/'

In [3]: with mock.patch.object(os, 'sep', '\\'):
   ...:     print(os.sep)
   ...:     
\

wting avatar Aug 29 '16 18:08 wting

I had the same problem and the suggested fix solved the problem. I guess that an alternative solution would be to run the tests on a windows CI platform such as appveyor.

willyd avatar Oct 28 '16 13:10 willyd

win 10 64 , the same issue. change autojump_match.py worked. thank you.

com314159 avatar Jan 03 '19 02:01 com314159

Window 10 professional, I met this issue today....and the suggested patch works like a charm! Many thanks. I just wonder why the patch is not merged into the master while four years have been passed?

shukebeta avatar Mar 19 '20 02:03 shukebeta

Window 11 professional, I met this issue today....and the suggested patch works like a charm! Many thanks. I just wonder why the patch is not merged into the master while four years have been passed?

liwenfengg avatar Jul 14 '22 15:07 liwenfengg