88"""This module contains classes representing syntactical elements of SQL."""
99
1010import re
11- import sys
1211
1312from sqlparse import tokens as T
14- from sqlparse .compat import string_types , u
13+ from sqlparse .compat import u , string_types , unicode_compatible
1514from sqlparse .utils import imt , remove_quotes
1615
1716
17+ @unicode_compatible
1818class Token (object ):
1919 """Base class for all other classes in this module.
2020
@@ -26,6 +26,7 @@ class Token(object):
2626 __slots__ = ('value' , 'ttype' , 'parent' , 'normalized' , 'is_keyword' )
2727
2828 def __init__ (self , ttype , value ):
29+ value = u (value )
2930 self .value = value
3031 if ttype in T .Keyword :
3132 self .normalized = value .upper ()
@@ -36,30 +37,21 @@ def __init__(self, ttype, value):
3637 self .parent = None
3738
3839 def __str__ (self ):
39- if sys .version_info [0 ] == 3 :
40- return self .value
41- else :
42- return u (self ).encode ('utf-8' )
40+ return self .value
4341
4442 def __repr__ (self ):
4543 short = self ._get_repr_value ()
46- if sys .version_info [0 ] < 3 :
47- short = short .encode ('utf-8' )
4844 return '<%s \' %s\' at 0x%07x>' % (self ._get_repr_name (),
4945 short , id (self ))
5046
51- def __unicode__ (self ):
52- """Returns a unicode representation of this object."""
53- return self .value or ''
54-
5547 def _get_repr_name (self ):
5648 return str (self .ttype ).split ('.' )[- 1 ]
5749
5850 def _get_repr_value (self ):
59- raw = u ( self )
51+ raw = self . value
6052 if len (raw ) > 7 :
61- raw = raw [:6 ] + u '...'
62- return re .sub ('\s+' , ' ' , raw )
53+ raw = raw [:6 ] + '...'
54+ return re .sub (r '\s+' , ' ' , raw )
6355
6456 def flatten (self ):
6557 """Resolve subgroups."""
@@ -143,6 +135,7 @@ def has_ancestor(self, other):
143135 return False
144136
145137
138+ @unicode_compatible
146139class TokenList (Token ):
147140 """A group of tokens.
148141
@@ -158,20 +151,8 @@ def __init__(self, tokens=None):
158151 self .tokens = tokens
159152 super (TokenList , self ).__init__ (None , self .__str__ ())
160153
161- def __unicode__ (self ):
162- return self ._to_string ()
163-
164154 def __str__ (self ):
165- str_ = self ._to_string ()
166- if sys .version_info [0 ] <= 2 :
167- str_ = str_ .encode ('utf-8' )
168- return str_
169-
170- def _to_string (self ):
171- if sys .version_info [0 ] == 3 :
172- return '' .join (x .value for x in self .flatten ())
173- else :
174- return '' .join (u (x ) for x in self .flatten ())
155+ return '' .join (token .value for token in self .flatten ())
175156
176157 def _get_repr_name (self ):
177158 return self .__class__ .__name__
0 commit comments