internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: xonsh/xonsh

Issue:
xonsh requires multiple exits after an error
Example:

```
[myprompt]$ balloon
xonsh: subprocess mode: command not found: balloon
[myprompt]$ exit

[myprompt]$ exit
```

_exits_

Every subsequent error increases the `exit` amount one needs to type.



Discussion:
I can verify this issue. Thanks for reporting. Out of curiosity, do you have `$XONSH_STORE_STDOUT = True`?

Yes, I do.

Thanks @masasin - I think that this a stdout storage issue.


Resolving patch:
diff --git a/xonsh/proc.py b/xonsh/proc.py
--- a/xonsh/proc.py
+++ b/xonsh/proc.py
@@ -367,7 +367,11 @@ def __init__(self, args, stdin=None, stdout=None, stderr=None, preexec_fn=None,
         self._stderr = stderr
         self.args = args
         self.universal_newlines = universal_newlines
-        
+
+        if not os.access(args[0], os.F_OK):
+            raise FileNotFoundError('command {0!r} not found'.format(args[0]))
+        elif not os.access(args[0], os.X_OK) or os.path.isdir(args[0]):
+            raise PermissionError('permission denied: {0!r}'.format(args[0]))
         self._tpty = tpty = TeePTY()
         if preexec_fn is not None:
             preexec_fn()
diff --git a/xonsh/teepty.py b/xonsh/teepty.py
--- a/xonsh/teepty.py
+++ b/xonsh/teepty.py
@@ -51,6 +51,19 @@ def _on_main_thread():
     return threading.current_thread() is threading.main_thread()
 
 
+def _find_error_code(e):
+    """Gets the approriate error code for an exception e, see
+    http://tldp.org/LDP/abs/html/exitcodes.html for exit codes.
+    """
+    if isinstance(e, PermissionError):
+        code = 126
+    elif isinstance(e, FileNotFoundError):
+        code = 127
+    else:
+        code = 1
+    return code
+
+
 class TeePTY(object):
     """This class is a pseudo terminal that tees the stdout and stderr into a buffer."""
 
@@ -115,10 +128,13 @@ def spawn(self, argv=None, env=None, stdin=None, delay=None):
             if self._temp_stdin is not None:
                 self._delay_for_pipe(env=env, delay=delay)
             # ok, go
-            if env is None:
-                os.execvp(argv[0], argv)
-            else:
-                os.execvpe(argv[0], argv, env)
+            try:
+                if env is None:
+                    os.execvp(argv[0], argv)
+                else:
+                    os.execvpe(argv[0], argv, env)
+            except OSError as e:
+                os._exit(_find_error_code(e))
         else:
             self._pipe_stdin(stdin)
 


Test patch:
Repository: xonsh/xonsh

Issue:
Add 'edit and execute command` function
bash and zsh (and I'm sure others) have an edit and execute command that is invoked with Ctrl-e + Ctrl-x which opens the current contents of the command prompt in the system editor.  Edits made to the command are then run on save and quit.  

It could be this functionality is already baked in and I'm missing it?  In any case, it's especially handy for longer commands -- and given xonsh's multi-line input capabilities this seems like a good fit.

Thoughts?



Discussion:
This is not currently present, but it sounds like a reasonable idea.

This _should_ be trivial to implement in the `prompt_toolkit` shell, at least, because there's an `enable_open_in_editor` kwarg when setting up the prompt.  But when I enable it, I'm still not able to get the xonsh prompt to respond to the C-X C-E command.  

@jonathanslenders do you have any idea why xonsh might prevent this from working?  

The following patch works for me:

``` diff
diff --git a/xonsh/prompt_toolkit_shell.py b/xonsh/prompt_toolkit_shell.py
index 9d2ee23..0cd88cc 100644
--- a/xonsh/prompt_toolkit_shell.py
+++ b/xonsh/prompt_toolkit_shell.py
@@ -48,6 +48,7 @@ class PromptToolkitShell(BaseShell):
         self.pt_completer = PromptToolkitCompleter(self.completer, self.ctx)
         self.key_bindings_manager = KeyBindingManager(
             enable_auto_suggest_bindings=True,
+            enable_open_in_editor=True,
             enable_search=True, enable_abort_and_exit_bindings=True)
         load_xonsh_bindings(self.key_bindings_manager)
```

Normally, C-X C-E should work after that.

Awesome.  Thanks!  I was sticking it in the call to `prompt()` and not getting much traction.  The phrase "KeyBindingManager" probably should've been a clue.


Resolving patch:
diff --git a/xonsh/prompt_toolkit_shell.py b/xonsh/prompt_toolkit_shell.py
--- a/xonsh/prompt_toolkit_shell.py
+++ b/xonsh/prompt_toolkit_shell.py
@@ -48,6 +48,7 @@ def __init__(self, **kwargs):
         self.pt_completer = PromptToolkitCompleter(self.completer, self.ctx)
         self.key_bindings_manager = KeyBindingManager(
             enable_auto_suggest_bindings=True,
+            enable_open_in_editor=True,
             enable_search=True, enable_abort_and_exit_bindings=True)
         load_xonsh_bindings(self.key_bindings_manager)
 


Test patch:
Repository: xonsh/xonsh

Issue:
make vi mode configurable when using prompt-toolkit
The readline module automatically determines which key-bindings to use by reading `~/.inputrc`. Prompt-toolkit does not appear to have anything analogous. Vi mode can be enabled by passing `vi_mode=True` to the `prompt()` function. But a better option for xonsh is to pass `enable_vi_mode=True` to the `KeyBindingManager()` call.

To be determined is how to tell xonsh to use vi or emacs mode. It could parse `~/.inputrc` for compatibility with readline. But it's probably simpler to use a xonsh specific config option and or CLI argument.



Discussion:
Looking at [the docs on vi-mode](http://python-prompt-toolkit.readthedocs.org/en/latest/pages/building_prompts.html#dynamically-switch-between-emacs-and-vi-mode) there's an example of dynamically switching between emacs mode and vi mode.  So it shouldn't be too difficult to have an `$ENABLE_VI_MODE` variable that can be set in session, in a `.xonshrc` or via CLI argument.


Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -70,6 +70,7 @@ def lc_converter(val):
     'XONSH_ENCODING_ERRORS': (is_string, ensure_string, ensure_string),
     'XONSH_HISTORY_SIZE': (is_history_tuple, to_history_tuple, history_tuple_to_str),
     'XONSH_STORE_STDOUT': (is_bool, to_bool, bool_to_str),
+    'VI_MODE': (is_bool, to_bool, bool_to_str),
 }
 
 #
@@ -153,6 +154,7 @@ def xonshconfig(env):
     'SUGGEST_THRESHOLD': 3,
     'TEEPTY_PIPE_DELAY': 0.01,
     'TITLE': DEFAULT_TITLE,
+    'VI_MODE': False,
     'XDG_CONFIG_HOME': os.path.expanduser(os.path.join('~', '.config')),
     'XDG_DATA_HOME': os.path.expanduser(os.path.join('~', '.local', 'share')),
     'XONSHCONFIG': xonshconfig,
diff --git a/xonsh/prompt_toolkit_shell.py b/xonsh/prompt_toolkit_shell.py
--- a/xonsh/prompt_toolkit_shell.py
+++ b/xonsh/prompt_toolkit_shell.py
@@ -6,6 +6,7 @@
 from prompt_toolkit.shortcuts import prompt
 from prompt_toolkit.key_binding.manager import KeyBindingManager
 from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
+from prompt_toolkit.filters import Condition
 from pygments.token import Token
 from pygments.style import Style
 
@@ -30,6 +31,7 @@ def setup_history():
 
 def teardown_history(history):
     """Tears down the history object."""
+    import builtins
     env = builtins.__xonsh_env__
     hsize = env.get('XONSH_HISTORY_SIZE')[0]
     hfile = env.get('XONSH_HISTORY_FILE')
@@ -48,8 +50,10 @@ def __init__(self, **kwargs):
         self.pt_completer = PromptToolkitCompleter(self.completer, self.ctx)
         self.key_bindings_manager = KeyBindingManager(
             enable_auto_suggest_bindings=True,
-            enable_open_in_editor=True,
-            enable_search=True, enable_abort_and_exit_bindings=True)
+            enable_search=True, 
+            enable_abort_and_exit_bindings=True,
+            enable_vi_mode=Condition(lambda cli: builtins.__xonsh_env__.get('VI_MODE')),
+            enable_open_in_editor=True)
         load_xonsh_bindings(self.key_bindings_manager)
 
     def __del__(self):


Test patch:
Repository: xonsh/xonsh

Issue:
Feature-request: Static configuration file as a command line option
I suggest that an option to the commandline --config_file is added that allows the user to specify a file location for a static configuration file. (http://xonsh.org/xonshconfig.html)

This would allow portable usage of xonsh, e.g. install python and xonsh on a usb and bring the shell on a stick. 



Discussion:
Yup I agree with this.  Only thing to add would be that the option should be called `--config-file`.

or maybe `--config-path` or `--config`


Resolving patch:
diff --git a/xonsh/main.py b/xonsh/main.py
--- a/xonsh/main.py
+++ b/xonsh/main.py
@@ -2,11 +2,8 @@
 """The main xonsh script."""
 import os
 import sys
-import shlex
-import signal
 import builtins
-import subprocess
-from argparse import ArgumentParser, Namespace
+from argparse import ArgumentParser, ArgumentTypeError
 from contextlib import contextmanager
 
 from xonsh import __version__
@@ -14,6 +11,18 @@
 from xonsh.pretty import pprint
 from xonsh.jobs import ignore_sigtstp
 
+def path_argument(s):
+    """Return a path only if the path is actually legal
+
+    This is very similar to argparse.FileType, except that it doesn't return
+    an open file handle, but rather simply validates the path."""
+
+    s = os.path.abspath(os.path.expanduser(s))
+    if not os.path.isfile(s):
+        raise ArgumentTypeError('"%s" must be a valid path to a file' % s)
+    return s
+
+
 parser = ArgumentParser(description='xonsh')
 parser.add_argument('-V', '--version',
                     action='version',
@@ -34,6 +43,10 @@
                     dest='login',
                     action='store_true',
                     default=False)
+parser.add_argument('--config-path',
+                    help='specify a custom static configuration file',
+                    dest='config_path',
+                    type=path_argument)
 parser.add_argument('--no-rc',
                     help="Do not load the .xonshrc file",
                     dest='norc',
@@ -79,6 +92,8 @@ def premain(argv=None):
     shell_kwargs = {'shell_type': args.shell_type}
     if args.norc:
         shell_kwargs['ctx'] = {}
+    if args.config_path:
+        shell_kwargs['ctx']= {'XONSHCONFIG': args.config_path}
     setattr(sys, 'displayhook', _pprint_displayhook)
     shell = builtins.__xonsh_shell__ = Shell(**shell_kwargs)
     from xonsh import imphooks


Test patch:
Repository: xonsh/xonsh

Issue:
Prompt toolkit up and down arrows don't match beginning of str
Currently the up and down arrows don't do reverse look up that matches on the start of the string. Rather, it only scrolls through history. This should have the same behavior a readline.



Discussion:

Resolving patch:
diff --git a/xonsh/prompt_toolkit_shell.py b/xonsh/prompt_toolkit_shell.py
--- a/xonsh/prompt_toolkit_shell.py
+++ b/xonsh/prompt_toolkit_shell.py
@@ -88,6 +88,7 @@ def cmdloop(self, intro=None):
                     completer=completer,
                     lexer=PygmentsLexer(XonshLexer),
                     history=self.history,
+                    enable_history_search=True,
                     key_bindings_registry=self.key_bindings_manager.registry,
                     display_completions_in_columns=multicolumn)
                 if not line:


Test patch:
Repository: xonsh/xonsh

Issue:
"module" object has no attribute '__xonsh_shell__'
0c132528 broke xonsh for me.  

```
xonsh 
Traceback (most recent call last):
  File "/home/gil/miniconda3/bin/xonsh", line 9, in <module>
    load_entry_point('xonsh==0.2.3', 'console_scripts', 'xonsh')()
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/main.py", line 111, in main
    args = premain(argv)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/main.py", line 98, in premain
    shell = builtins.__xonsh_shell__ = Shell(**shell_kwargs)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/shell.py", line 29, in __init__
    self._init_environ(ctx)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/shell.py", line 67, in _init_environ
    self.ctx = xonshrc_context(rcfiles=rc, execer=self.execer)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/environ.py", line 687, in xonshrc_context
    execer.exec(rc, glbs=env)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/execer.py", line 127, in exec
    return exec(code, glbs, locs)
  File "/home/gil/.xonshrc", line 8, in <module>
    evalx("keychain --quiet ~/.ssh/id_ed25519")
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/execer.py", line 113, in eval
    return eval(code, glbs, locs)
  File "/home/gil/.xonshrc", line 1, in <module>
    import os
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/built_ins.py", line 653, in subproc_uncaptured
    return run_subproc(cmds, captured=False)
  File "/home/gil/miniconda3/lib/python3.4/site-packages/xonsh/built_ins.py", line 618, in run_subproc
    builtins.__xonsh_shell__.settitle()
AttributeError: 'module' object has no attribute '__xonsh_shell__'
```

I have an `evalx` in my `xonshrc` to load my ssh keys and that seems to be the problem.  Removing the offending lines in `built_ins.py` lets xonsh load but, predictably, breaks the titling.



Discussion:

Resolving patch:
diff --git a/xonsh/built_ins.py b/xonsh/built_ins.py
--- a/xonsh/built_ins.py
+++ b/xonsh/built_ins.py
@@ -615,7 +615,10 @@ def run_subproc(cmds, captured=True):
         })
     if ENV.get('XONSH_INTERACTIVE') and not ENV.get('XONSH_STORE_STDOUT'):
         # set title here to get current command running
-        builtins.__xonsh_shell__.settitle()
+        try:
+            builtins.__xonsh_shell__.settitle()
+        except AttributeError:
+            pass
     if background:
         return
     if prev_is_proxy:


Test patch:
Repository: xonsh/xonsh

Issue:
Custom $PATH additions not available in os.environ
I added `$PATH.insert(0, '/usr/local/bin')` to .xonshrc. In xonsh, $PATH is `['/usr/local/bin', '/usr/bin', '/bin']`. However, `import os; os.environ['PATH']` yields `'/usr/bin:/bin'`.

Due to this, [hg_dirty_working_directory](http://xonsh.org/_modules/xonsh/environ.html#hg_dirty_working_directory) doesn't work properly showing false negatives. hg is in /usr/local/bin, so it raises FileNotFoundError when called.



Discussion:

Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -790,7 +790,7 @@ def get_git_branch(cwd=None):
 
 def call_hg_command(command, cwd):
     # Override user configurations settings and aliases
-    hg_env = os.environ.copy()
+    hg_env = builtins.__xonsh_env__.detype()
     hg_env['HGRCPATH'] = ""
 
     s = None


Test patch:
Repository: xonsh/xonsh

Issue:
error when tty login
 Hello again, but it happens that when I try logearme a tty with a user whose shell xonsh default throws me this error:

![photo_2016-02-12_20-20-00](https://cloud.githubusercontent.com/assets/3141865/13024824/1607bf46-d1c9-11e5-8cd5-e7bc987d9847.jpg)

Thanks for the help



Discussion:
Hey @arkan -- is the native encoding of your terminal ASCII?  That can cause trouble like this.  

What's the output of `localectl`?

As a quick(ish) check, you can try adding this to your `xonshrc` (assuming you want US english encoding...)

```
$LANG="en_US.UTF-8"
$LC_ALL="en_US.UTF-8"
```

 ~ $ localectl 
   System Locale: LANG=es_EC.UTF-8
       VC Keymap: n/a
      X11 Layout: n/a

This might be a legitimate encoding error on our part, looking at the traceback. Can you please post the contents of your config file please?


Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -936,9 +936,9 @@ def _current_job():
 def env_name(pre_chars='(', post_chars=') '):
     """Extract the current environment name from $VIRTUAL_ENV."""
     env_path = __xonsh_env__.get('VIRTUAL_ENV')
-    
+
     env_name = os.path.basename(env_path)
-    
+
     return pre_chars + env_name + post_chars if env_name else ''
 
 
@@ -1049,7 +1049,14 @@ def load_static_config(ctx, config=None):
         # don't set in ctx in order to maintain default
         config = env['XONSHCONFIG'] = xonshconfig(env)
     if os.path.isfile(config):
-        with open(config, 'r') as f:
+        # Note that an Env instance at __xonsh_env__ has not been started yet,
+        # per se, so we have to use os.environ
+        encoding = os.environ.get('XONSH_ENCODING',
+                                  DEFAULT_VALUES.get('XONSH_ENCODING', 'utf8'))
+        errors = os.environ.get('XONSH_ENCODING_ERRORS',
+                                DEFAULT_VALUES.get('XONSH_ENCODING_ERRORS',
+                                                   'surrogateescape'))
+        with open(config, 'r', encoding=encoding, errors=errors) as f:
             conf = json.load(f)
         ctx['LOADED_CONFIG'] = True
     else:


Test patch:
Repository: xonsh/xonsh

Issue:
Make pygments highlighter work with !() and ![]
Right now they get assigned to `Token.Error` or something.



Discussion:

Resolving patch:
diff --git a/xonsh/pyghooks.py b/xonsh/pyghooks.py
--- a/xonsh/pyghooks.py
+++ b/xonsh/pyghooks.py
@@ -30,8 +30,8 @@ class XonshSubprocLexer(BashLexer):
 ROOT_TOKENS = [(r'\?', Keyword),
                (r'\$\w+', Name.Variable),
                (r'\$\{', Keyword, ('pymode', )),
-               (r'\$\(', Keyword, ('subproc', )),
-               (r'\$\[', Keyword, ('subproc', )),
+               (r'[\!\$]\(', Keyword, ('subproc', )),
+               (r'[\!\$]\[', Keyword, ('subproc', )),
                (r'@\(', Keyword, ('pymode', )),
                inherit, ]
 


Test patch:
Repository: xonsh/xonsh

Issue:
CTRL-C during config wizard results in loss of input to terminal
Running bash in a Terminator window, when I first started xonsh, I was asked if I wanted to run the config wizard.  I chose yes, but then eventually decided I didn't want to finish, so I hit CTRL-C.  This eventually got me back to bash, but when I pressed keys, the keys didn't show up in the terminal windows.  I had to close the window and open a new one.



Discussion:
Thanks for reporting @rsyring.  I thought that I had fixed this issue.  Do you recall which version of xonsh this was one? Does it still occur?  Sorry for taking so long to respond!

I just updated to master and tried it and the bug is still present.

Thanks for letting us know @rsyring.


Resolving patch:
diff --git a/xonsh/main.py b/xonsh/main.py
--- a/xonsh/main.py
+++ b/xonsh/main.py
@@ -236,8 +236,8 @@ def main(argv=None):
         ignore_sigtstp()
         if not env['LOADED_CONFIG'] and not any(env['LOADED_RC_FILES']):
             print('Could not find xonsh configuration or run control files.')
-            code = '$[xonfig wizard --confirm]'
-            shell.execer.exec(code, mode='single', glbs=shell.ctx)
+            from xonsh import xonfig  # lazy import
+            xonfig.main(['wizard', '--confirm'])
         shell.cmdloop()
     postmain(args)
 


Test patch:
Repository: xonsh/xonsh

Issue:
COMPLETIONS_MENU_ROWS not working
Hello

My value for `COMPLETIONS_MENU_ROWS` is 5 (default). But it does not seem to work. My terminal grow until the bottom of the window and when it has reached the last line then I don't get completions anymore.

I use gnome-terminal on Ubuntu 14.04

Thank you for your help !

EDIT: I installed HEAD from the git repo. I still have the problem

EDIT2+3: I also have the problem with xtrem and Terminator (It seems Terminal independant)



Discussion:
I can confirm this issue on Windows.

But I do get a single row of completions when I am on the last line of cmd.exe

```
$ xonfig
+------------------+-------+
| xonsh            | 0.2.7 |
| Python           | 3.5.1 |
| PLY              | 3.7   |
| have readline    | True  |
| prompt toolkit   | 1.0.0 |
| pygments         | 2.2a0 |
| on posix         | False |
| on linux         | False |
| on arch          | False |
| on windows       | True  |
| on mac           | False |
| are root user    | False |
| default encoding | utf-8 |
+------------------+-------+
```

I also get one line of completion from time to time. I tested also with the AUR package on ArchLinux with the same result.

The current workaround is to hit Ctrl-L when I want a completion but it is quite annoying.

I fixed it. It comes from the new version of ptk. I'm making a PR in a minute.


Resolving patch:
diff --git a/xonsh/ptk/completer.py b/xonsh/ptk/completer.py
--- a/xonsh/ptk/completer.py
+++ b/xonsh/ptk/completer.py
@@ -2,6 +2,7 @@
 """Completer implementation to use with prompt_toolkit."""
 import os
 import builtins
+import xonsh.shell
 
 from prompt_toolkit.layout.dimension import LayoutDimension
 from prompt_toolkit.completion import Completer, Completion
@@ -41,13 +42,18 @@ def get_completions(self, document, complete_event):
 
     def reserve_space(self):
         cli = builtins.__xonsh_shell__.shell.prompter.cli
-        #TODO remove after next prompt_toolkit release
-        try:
-            #old layout to be removed at next ptk release
-            window = cli.application.layout.children[1].children[1].content
-        except AttributeError:
-            #new layout to become default
-            window = cli.application.layout.children[1].content
+        if xonsh.shell.prompt_toolkit_version().startswith("1.0"):
+            # This is the layout for ptk 1.0
+            window = cli.application.layout.children[0].content.children[1]
+        else:
+            #TODO remove after next prompt_toolkit release
+            try:
+                #old layout to be removed at next ptk release
+                window = cli.application.layout.children[1].children[1].content
+            except AttributeError:
+                #new layout to become default
+                window = cli.application.layout.children[1].content
+
         if window and window.render_info:
             h = window.render_info.content_height
             r = builtins.__xonsh_env__.get('COMPLETIONS_MENU_ROWS')


Test patch: