Repository: xonsh/xonsh

Issue:
time gives a NameError
```
sandboxmaster*%=🐚 $XONSH_SHOW_TRACEBACK = True
sandboxmaster*%=🐚 time
Traceback (most recent call last):
  File "/Users/aaronmeurer/anaconda/lib/python3.5/site-packages/xonsh/base_shell.py", line 143, in default
    self.execer.exec(code, mode='single', glbs=self.ctx)  # no locals
  File "/Users/aaronmeurer/anaconda/lib/python3.5/site-packages/xonsh/execer.py", line 126, in exec
    return exec(code, glbs, locs)
  File "<xonsh-code>", line 1, in <module>
NameError: name 'time' is not defined
sandboxmaster*%=🐚 which time
/usr/bin/time
```

It's not in `globals()` or `__xonsh_ctx__`. 



Discussion:
This used to work, so it is probably a matter of finding what went wrong.

I have the same problem, but I've gone back through a bunch of releases and can't find where this worked.  That said, on master, 

```
> time ls
```

still works.  Also, somewhat weirdly:

``` python
from xonsh.shell import Shell
s = Shell()
s.execer.exec('time', mode='single', glbs=s.ctx)
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--help] command [arg...]
```

I can't reproduce this.  I'm on 9f9cd1894e4bf72286975bc8e54e0b50ff886949

I get:

```
hartz@lappy-514 ~ $ time
<module 'time' (built-in)>
```

Then again, for me, I don't have an executable called `time` on my path:

```
hartz@lappy-514 ~ $ which time
hartz@lappy-514 ~ $ 
```

So maybe some kind of collision there?

But then again, if I do the following, no problems either:

```
hartz@lappy-514 ~ $ vim = 72
hartz@lappy-514 ~ $ vim
72
```

I'm not suggesting this as a solution, because I don't like `import .. as ...` very much and there's probably a clenaer way, but in `base_shell.py` if you `import time as xonshtime` and then change the instances of `time.time()` accordingly, then 

```
> time
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
       [--portability] [--format=format] [--output=file] [--version]
       [--help] command [arg...]
```

`time ls` works for me. 

@adqm maybe you are importing `time` in your xonshrc. Anyway, if you don't have a `time` executable on your PATH you won't be able to reproduce this. 

@asmeurer I guess I don't understand the situation that causes this.  You have: `time` defined in Python, `time` on the path, and you are seeing the issue?

No, `time` is not defined in Python (it isn't in `globals()`). 

Probably has to do with the fact that it's a module

```
sympy(9364102...)%🐚 time
NameError: name 'time' is not defined
sympy(9364102...)%🐚 os
NameError: name 'os' is not defined
sympy(9364102...)%🐚 sys
NameError: name 'sys' is not defined
```

@asmeurer -- it is.  I think, specifically, with the fact that it's a module imported in `base_shell.py`

```
gil@bad_cat ~ $ time
NameError: name 'time' is not defined
gil@bad_cat ~ $ os
NameError: name 'os' is not defined
gil@bad_cat ~ $ sys
NameError: name 'sys' is not defined
gil@bad_cat ~ $ io
NameError: name 'io' is not defined
gil@bad_cat ~ $ builtins
NameError: name 'builtins' is not defined
gil@bad_cat ~ $ shlex
xonsh: subprocess mode: command not found: shlex
Did you mean one of the following?
    lex:    Command (/usr/bin/lex)
    flex:   Command (/usr/bin/flex)
    mplex:  Command (/usr/bin/mplex)
    shred:  Command (/usr/bin/shred)
gil@bad_cat ~ $ subprocess
xonsh: subprocess mode: command not found: subprocess
gil@bad_cat ~ $ argparse
xonsh: subprocess mode: command not found: argparse
```

Is base_shell running an `exec` that isn't passing in a cleared namespace? 


Resolving patch:
diff --git a/xonsh/base_shell.py b/xonsh/base_shell.py
--- a/xonsh/base_shell.py
+++ b/xonsh/base_shell.py
@@ -178,8 +178,8 @@ def push(self, line):
         try:
             code = self.execer.compile(src,
                                        mode='single',
-                                       glbs=None,
-                                       locs=self.ctx)
+                                       glbs=self.ctx,
+                                       locs=None)
             self.reset_buffer()
         except SyntaxError:
             if line == '\n':
diff --git a/xonsh/ptk/shell.py b/xonsh/ptk/shell.py
--- a/xonsh/ptk/shell.py
+++ b/xonsh/ptk/shell.py
@@ -93,8 +93,8 @@ def push(self, line):
         try:
             code = self.execer.compile(src,
                                        mode='single',
-                                       glbs=None,
-                                       locs=self.ctx)
+                                       glbs=self.ctx,
+                                       locs=None)
             self.reset_buffer()
         except Exception:  # pylint: disable=broad-except
             self.reset_buffer()


Test patch: