Repository: hylang/hy

Issue:
Single quote crashes hy2py
## x.hy

```
'
```

## `hy2py x.hy`

```
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "hy/cmdline.py", line 406, in hy2py_main
    if stdin_text is None
  File "hy/cmdline.py", line 186, in pretty_error
    print(e, file=sys.stderr)
  File "hy/lex/exceptions.py", line 43, in __str__
    source = self.source.split("\n")
AttributeError: 'NoneType' object has no attribute 'split'
```

Discussion:

Patch:
diff --git a/NEWS b/NEWS
index 9ae36128..fd3a1e1a 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,7 @@ Changes from 0.12.1
    * `setv` no longer unnecessarily tries to get attributes
    * `loop` no longer replaces string literals equal to "recur"
    * The REPL now prints the correct value of `do` and `try` forms
+   * Fixed a crash when tokenizing a single quote followed by whitespace
 
    [ Misc. Improvements ]
    * New contrib module `hy-repr`
diff --git a/hy/lex/__init__.py b/hy/lex/__init__.py
index f1a74aae..8a73d874 100644
--- a/hy/lex/__init__.py
+++ b/hy/lex/__init__.py
@@ -34,7 +34,7 @@ def tokenize(buf):
     except LexingError as e:
         pos = e.getsourcepos()
         raise LexException("Could not identify the next token.",
-                           pos.lineno, pos.colno)
+                           pos.lineno, pos.colno, buf)
     except LexException as e:
         if e.source is None:
             e.source = buf
diff --git a/hy/lex/exceptions.py b/hy/lex/exceptions.py
index 4c4b7600..2ef5660a 100644
--- a/hy/lex/exceptions.py
+++ b/hy/lex/exceptions.py
@@ -24,12 +24,12 @@ from hy.errors import HyError
 
 class LexException(HyError):
     """Error during the Lexing of a Hython expression."""
-    def __init__(self, message, lineno, colno):
+    def __init__(self, message, lineno, colno, source=None):
         super(LexException, self).__init__(message)
         self.message = message
         self.lineno = lineno
         self.colno = colno
-        self.source = None
+        self.source = source
         self.filename = '<stdin>'
 
     def __str__(self):


Test patch:
diff --git a/tests/test_lex.py b/tests/test_lex.py
index 96918883..514696b8 100644
--- a/tests/test_lex.py
+++ b/tests/test_lex.py
@@ -63,6 +63,17 @@ def test_unbalanced_exception():
         pass
 
 
+def test_lex_single_quote_err():
+    "Ensure tokenizing \"' \" throws a LexException that can be stringified"
+    # https://github.com/hylang/hy/issues/1252
+    try:
+        tokenize("' ")
+    except LexException as e:
+        assert "Could not identify the next token" in str(e)
+    else:
+        assert False
+
+
 def test_lex_expression_symbols():
     """ Make sure that expressions produce symbols """
     objs = tokenize("(foo bar)")