python: fix string/unicode comparison.
authorJoel Martin <github@martintribe.org>
Thu, 17 Dec 2015 15:20:16 +0000 (09:20 -0600)
committerJoel Martin <github@martintribe.org>
Thu, 17 Dec 2015 15:21:06 +0000 (09:21 -0600)
Fixes #135.

python/mal_types.py
python/printer.py
python/reader.py

index 8726f04..e02f87b 100644 (file)
@@ -2,13 +2,12 @@ import sys, copy, types as pytypes
 
 # python 3.0 differences
 if sys.hexversion > 0x3000000:
-    def u(x):
-        return x
+    _u = lambda x: x
+    _s2u = lambda x: x
 else:
     import codecs
-    def u(x):
-        return codecs.unicode_escape_decode(x)[0]
-
+    _u = lambda x: codecs.unicode_escape_decode(x)[0]
+    _s2u = lambda x: unicode(x)
 
 if sys.version_info[0] >= 3:
     str_types = [str]
@@ -72,10 +71,10 @@ def _symbol_Q(exp): return type(exp) == Symbol
 # Keywords
 # A specially prefixed string
 def _keyword(str):
-    if str[0] == u("\u029e"): return str
-    else:                     return u("\u029e") + str
+    if str[0] == _u("\u029e"): return str
+    else:                     return _u("\u029e") + str
 def _keyword_Q(exp):
-    return _string_Q(exp) and exp[0] == u("\u029e")
+    return _string_Q(exp) and exp[0] == _u("\u029e")
 
 # Functions
 def _function(Eval, Env, ast, env, params):
index e36d0ea..a2837bd 100644 (file)
@@ -15,7 +15,7 @@ def _pr_str(obj, print_readably=True):
             ret.extend((_pr_str(k), _pr_str(obj[k],_r)))
         return "{" + " ".join(ret) + "}"
     elif types._string_Q(obj):
-        if len(obj) > 0 and obj[0] == types.u('\u029e'):
+        if len(obj) > 0 and obj[0] == types._u('\u029e'):
             return ':' + obj[1:]
         elif print_readably:
             return '"' + _escape(obj) + '"'
index d1c4d27..44c9d74 100644 (file)
@@ -1,5 +1,5 @@
 import re
-from mal_types import (_symbol, _keyword, _list, _vector, _hash_map)
+from mal_types import (_symbol, _keyword, _list, _vector, _hash_map, _s2u)
 
 class Blank(Exception): pass
 
@@ -32,7 +32,7 @@ def read_atom(reader):
     if re.match(int_re, token):     return int(token)
     elif re.match(float_re, token): return int(token)
     elif token[0] == '"':
-        if token[-1] == '"':        return _unescape(token[1:-1])
+        if token[-1] == '"':        return _s2u(_unescape(token[1:-1]))
         else:                       raise Exception("expected '\"', got EOF")
     elif token[0] == ':':           return _keyword(token[1:])
     elif token == "nil":            return None