Repository: xonsh/xonsh

Issue:
execx requires a line separator character at the end of the input
Hello! Thank you for xonsh!

I have a string variable with xonsh code. For example I need to change environment:
```
e = "print(123)"
evalx(e)
123
```
But:
```
e = "$TEST=1"
evalx(e)
```
Result:
```
  File "1.xsh", line 3, in <module>
    evalx(e)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23092, in __call__
    return self.obj.__call__(*args, **kwargs)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23246, in eval
    transform=transform,
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23224, in compile
    tree = self.parse(input, ctx, mode=mode, filename=filename, transform=transform)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23184, in parse
    tree, input = self._parse_ctx_free(input, mode=mode, filename=filename)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23324, in _parse_ctx_free
    raise original_error from None
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/__amalgam__.py", line 23309, in _parse_ctx_free
    debug_level=(self.debug_level > 2),
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/parsers/base.py", line 543, in parse
    tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/ply/ply/yacc.py", line 335, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/ply/ply/yacc.py", line 1203, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/ply/ply/yacc.py", line 194, in call_errorfunc
    r = errorfunc(token)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/parsers/base.py", line 3289, in p_error
    self._parse_error("no further code", None)
  File "/home/pc/miniconda3/lib/python3.7/site-packages/xonsh/parsers/base.py", line 676, in _parse_error
    raise err
SyntaxError: None: no further code
```
The same result for environ and execx:
```
evalx('os.environ["test"]=1')
...
SyntaxError: None: no further code
```
```
$ xonfig                                                                                                          
+------------------+-----------------+
| xonsh            | 0.9.14          |
| Python           | 3.7.6           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 3.0.3           |
| shell type       | prompt_toolkit2 |
| pygments         | 2.5.2           |
| on posix         | True            |
| on linux         | True            |
| distro           | unknown         |
| on darwin        | False           |
| on windows       | False           |
| on cygwin        | False           |
| on msys2         | False           |
| is superuser     | False           |
| default encoding | utf-8           |
| xonsh encoding   | utf-8           |
| encoding errors  | surrogateescape |
+------------------+-----------------+
```
Is there a way to solve this?

-----------

🥈 Workaround - https://github.com/xonsh/xonsh/issues/3481#issuecomment-599864134

Discussion:
scopatz: Thanks @laloch - can you pleas add some tests for these. Thanks!

Patch:
diff --git a/news/execx-evalx-newline.rst b/news/execx-evalx-newline.rst
new file mode 100644
index 00000000..6f74a5bc
--- /dev/null
+++ b/news/execx-evalx-newline.rst
@@ -0,0 +1,24 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* <news item>
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* ``execx`` does not require the input string to be newline-terminated.
+* ``evalx`` accepts newline-terminated input string.
+
+**Security:**
+
+* <news item>
diff --git a/xonsh/execer.py b/xonsh/execer.py
index 2d113ee5..b4daef78 100644
--- a/xonsh/execer.py
+++ b/xonsh/execer.py
@@ -145,6 +145,7 @@ class Execer(object):
         if isinstance(input, types.CodeType):
             code = input
         else:
+            input = input.rstrip("\n")
             if filename is None:
                 filename = self.filename
             code = self.compile(
@@ -174,6 +175,8 @@ class Execer(object):
         if isinstance(input, types.CodeType):
             code = input
         else:
+            if not input.endswith("\n"):
+                input += "\n"
             if filename is None:
                 filename = self.filename
             code = self.compile(


Test patch:
diff --git a/tests/test_execer.py b/tests/test_execer.py
index 22e612a6..d74ccf9a 100644
--- a/tests/test_execer.py
+++ b/tests/test_execer.py
@@ -2,7 +2,13 @@
 """Tests the xonsh lexer."""
 import os
 
-from tools import check_eval, check_parse, skip_if_on_unix, skip_if_on_windows
+from tools import (
+    check_eval,
+    check_exec,
+    check_parse,
+    skip_if_on_unix,
+    skip_if_on_windows,
+)
 
 import pytest
 
@@ -138,3 +144,12 @@ def test_echo_line_cont():
 )
 def test_two_echo_line_cont(code):
     assert check_parse(code)
+
+
+def test_eval_eol():
+    assert check_eval("0") and check_eval("0\n")
+
+
+def test_exec_eol():
+    locs = dict()
+    assert check_exec("a=0", locs=locs) and check_exec("a=0\n", locs=locs)
diff --git a/tests/tools.py b/tests/tools.py
index 0b9de84a..767887f9 100644
--- a/tests/tools.py
+++ b/tests/tools.py
@@ -174,8 +174,6 @@ class DummyEnv(MutableMapping):
 
 
 def check_exec(input, **kwargs):
-    if not input.endswith("\n"):
-        input += "\n"
     builtins.__xonsh__.execer.exec(input, **kwargs)
     return True