Repository: xonsh/xonsh

Issue:
multiline code block crashes xonsh
```
$ @contextmanager
Traceback (most recent call last):
  File "/home/laerus/.local/bin/xonsh", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/laerus/src/xonsh/scripts/xonsh", line 3, in <module>
    main()
  File "/home/laerus/src/xonsh/xonsh/main.py", line 221, in main
    shell.shell.cmdloop()
  File "/home/laerus/src/xonsh/xonsh/readline_shell.py", line 395, in cmdloop
    self._cmdloop(intro=intro)
  File "/home/laerus/src/xonsh/xonsh/readline_shell.py", line 381, in _cmdloop
    stop = self.onecmd(line)
  File "/usr/lib/python3.5/cmd.py", line 211, in onecmd
    return self.default(line)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 308, in default
    src, code = self.push(line)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 385, in push
    return self.compile(src)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 420, in compile
    return src, code
UnboundLocalError: local variable 'code' referenced before assignment
```

bisect points to 8f5aa9d73df9701a77c0b6f272439f4a4fc76da1



Discussion:
this seems to happen in any multiline code
```
$ def tt():
Traceback (most recent call last):
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 384, in _compile
    locs=None)
  File "/home/laerus/src/xonsh/xonsh/execer.py", line 103, in compile
    transform=transform)
  File "/home/laerus/src/xonsh/xonsh/execer.py", line 73, in parse
    tree, input = self._parse_ctx_free(input, mode=mode, filename=filename)
  File "/home/laerus/src/xonsh/xonsh/execer.py", line 177, in _parse_ctx_free
    raise original_error from None
  File "/home/laerus/src/xonsh/xonsh/execer.py", line 164, in _parse_ctx_free
    debug_level=(self.debug_level > 1))
  File "/home/laerus/src/xonsh/xonsh/parsers/base.py", line 333, in parse
    tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
  File "/home/laerus/src/xonsh/xonsh/ply/ply/yacc.py", line 331, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/home/laerus/src/xonsh/xonsh/ply/ply/yacc.py", line 1199, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/home/laerus/src/xonsh/xonsh/ply/ply/yacc.py", line 193, in call_errorfunc
    r = errorfunc(token)
  File "/home/laerus/src/xonsh/xonsh/parsers/base.py", line 2671, in p_error
    self._parse_error('no further code', None)
  File "/home/laerus/src/xonsh/xonsh/parsers/base.py", line 464, in _parse_error
    raise err
SyntaxError: None: no further code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/laerus/.local/bin/xonsh", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/home/laerus/src/xonsh/scripts/xonsh", line 3, in <module>
    main()
  File "/home/laerus/src/xonsh/xonsh/main.py", line 221, in main
    shell.shell.cmdloop()
  File "/home/laerus/src/xonsh/xonsh/readline_shell.py", line 399, in cmdloop
    self._cmdloop(intro=intro)
  File "/home/laerus/src/xonsh/xonsh/readline_shell.py", line 385, in _cmdloop
    stop = self.onecmd(line)
  File "/usr/lib/python3.5/cmd.py", line 211, in onecmd
    return self.default(line)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 276, in default
    src, code = self.push(line)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 369, in push
    return self._compile(src)
  File "/home/laerus/src/xonsh/xonsh/base_shell.py", line 392, in _compile
    if line == '\n' and not in_partial_string:
NameError: name 'line' is not defined
```
yes, I could reproduce it.
```
$ ls -lh $(history file).strip()
...............
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/bin/xonsh", line 3, in <module>
    main()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/main.py", line 221, in main
    shell.shell.cmdloop()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/ptk/shell.py", line 126, in cmdloop
    self.default(line)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/base_shell.py", line 308, in default
    src, code = self.push(line)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/base_shell.py", line 387, in push
    return self.compile(src)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/xonsh/base_shell.py", line 422, in compile
    return src, code
UnboundLocalError: local variable 'code' referenced before assignment
```
I know, I better use `ls -lh @$(history file)` here.

Resolving patch:
diff --git a/xonsh/base_shell.py b/xonsh/base_shell.py
--- a/xonsh/base_shell.py
+++ b/xonsh/base_shell.py
@@ -413,10 +413,11 @@ def compile(self, src):
                 print_exception()
                 return src, None
             self.need_more_lines = True
+            code = None
         except Exception:  # pylint: disable=broad-except
             self.reset_buffer()
             print_exception()
-            return src, None
+            code = None
         return src, code
 
     def reset_buffer(self):


Test patch: