Skip to content

Commit da914ac

Browse files
committed
Add unicode-str compatible cls decorator
1 parent be62c7a commit da914ac

2 files changed

Lines changed: 19 additions & 28 deletions

File tree

sqlparse/compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def u(s, encoding=None):
2525
return str(s)
2626

2727

28+
def unicode_compatible(cls):
29+
return cls
30+
31+
2832
text_type = str
2933
string_types = (str,)
3034
from io import StringIO
@@ -39,6 +43,12 @@ def u(s, encoding=None):
3943
return unicode(s, encoding)
4044

4145

46+
def unicode_compatible(cls):
47+
cls.__unicode__ = cls.__str__
48+
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
49+
return cls
50+
51+
4252
text_type = unicode
4353
string_types = (basestring,)
4454
from StringIO import StringIO

sqlparse/sql.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"""This module contains classes representing syntactical elements of SQL."""
99

1010
import re
11-
import sys
1211

1312
from sqlparse import tokens as T
14-
from sqlparse.compat import string_types, u
13+
from sqlparse.compat import u, string_types, unicode_compatible
1514
from sqlparse.utils import imt, remove_quotes
1615

1716

17+
@unicode_compatible
1818
class 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
146139
class 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

Comments
 (0)