From 65c08c8c7df169b7896b59419942598e303cf4db Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Fri, 12 Apr 2013 20:39:45 +0200 Subject: [PATCH] Factor str_type into hy.util --- hy/compiler.py | 7 ++----- hy/models/keyword.py | 12 +++--------- hy/models/string.py | 8 +------- hy/util.py | 8 ++++++++ 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 0262c78..ab635e9 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -33,7 +33,7 @@ from hy.models.list import HyList from hy.models.dict import HyDict from hy.models.keyword import HyKeyword -from hy.util import flatten_literal_list +from hy.util import flatten_literal_list, str_type from collections import defaultdict import codecs @@ -949,10 +949,7 @@ class HyASTCompiler(object): @builds(HyKeyword) def compile_keyword(self, keyword): - _str_type = str - if sys.version_info[0] < 3: - _str_type = unicode - return ast.Str(s=_str_type(keyword), lineno=keyword.start_line, + return ast.Str(s=str_type(keyword), lineno=keyword.start_line, col_offset=keyword.start_column) @builds(HyDict) diff --git a/hy/models/keyword.py b/hy/models/keyword.py index e57d2f1..2d1d48b 100644 --- a/hy/models/keyword.py +++ b/hy/models/keyword.py @@ -20,20 +20,14 @@ from __future__ import unicode_literals from hy.models import HyObject -import sys +from hy.util import str_type -if sys.version_info[0] >= 3: - _str_type = str -else: - _str_type = unicode - - -class HyKeyword(HyObject, _str_type): +class HyKeyword(HyObject, str_type): """Generic Hy Keyword object. It's either a ``str`` or a ``unicode``, depending on the Python version. """ def __new__(cls, value): - obj = _str_type.__new__(cls, "\uFDD0" + value) + obj = str_type.__new__(cls, "\uFDD0" + value) return obj diff --git a/hy/models/string.py b/hy/models/string.py index a700470..ce0a238 100644 --- a/hy/models/string.py +++ b/hy/models/string.py @@ -19,13 +19,7 @@ # DEALINGS IN THE SOFTWARE. from hy.models import HyObject -import sys - - -if sys.version_info[0] >= 3: - str_type = str -else: - str_type = unicode +from hy.util import str_type class HyString(HyObject, str_type): diff --git a/hy/util.py b/hy/util.py index f2ffdf1..a600b5e 100644 --- a/hy/util.py +++ b/hy/util.py @@ -18,6 +18,14 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. +import sys + + +if sys.version_info[0] >= 3: + str_type = str +else: + str_type = unicode + def flatten_literal_list(entry): for e in entry: