From d706ed5041f3b8eb90692d72d5e8db1a67cc7200 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Tue, 6 Jan 2015 20:58:33 -0600 Subject: Python: fix python3 support with keywords. --- python/mal_types.py | 18 +++++++++++++++--- python/printer.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/python/mal_types.py b/python/mal_types.py index 5fb9bba..b1b1b3b 100644 --- a/python/mal_types.py +++ b/python/mal_types.py @@ -1,4 +1,15 @@ import sys, copy, types as pytypes + +# python 3.0 differences +if sys.hexversion > 0x3000000: + def u(x): + return x +else: + import codecs + def u(x): + return codecs.unicode_escape_decode(x)[0] + + if sys.version_info[0] >= 3: str_types = [str] else: @@ -61,9 +72,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 -def _keyword_Q(exp): return _string_Q(exp) and exp[0] == u"\u029e" + 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") # Functions def _function(Eval, Env, ast, env, params): diff --git a/python/printer.py b/python/printer.py index 78a2fcf..98e3e90 100644 --- a/python/printer.py +++ b/python/printer.py @@ -12,7 +12,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] == u'\u029e': + if len(obj) > 0 and obj[0] == types.u('\u029e'): return ':' + obj[1:] elif print_readably: return '"' + obj.encode('unicode_escape').decode('latin1').replace('"', '\\"') + '"' -- cgit v1.2.3