diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2012-12-31 22:54:59 +0100 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2012-12-31 22:54:59 +0100 |
| commit | a25569e742b4884d2198c0ea6a6f804e657f6f6d (patch) | |
| tree | 841812458a62648f36d17368e131362c4d8a3239 /sqlparse | |
| parent | f85b696fc10076be3e9fcf34085ef864f2121026 (diff) | |
| download | sqlparse-a25569e742b4884d2198c0ea6a6f804e657f6f6d.tar.gz | |
Python 3 is now fully supported without any patches.
This change makes the extras/py3k stuff obsolete and installing for
Python 3 is as easy as "python3 setup.py install". setup.py uses
distribute's use_2to3 flag to automatically run 2to3 when Python 3 is
used.
\o/ Happy New Year, everyone!
Diffstat (limited to 'sqlparse')
| -rw-r--r-- | sqlparse/lexer.py | 5 | ||||
| -rw-r--r-- | sqlparse/sql.py | 26 |
2 files changed, 26 insertions, 5 deletions
diff --git a/sqlparse/lexer.py b/sqlparse/lexer.py index 3348be8..a92e2b7 100644 --- a/sqlparse/lexer.py +++ b/sqlparse/lexer.py @@ -13,6 +13,7 @@ # and to allow some customizations. import re +import sys from sqlparse import tokens from sqlparse.keywords import KEYWORDS, KEYWORDS_COMMON @@ -220,6 +221,8 @@ class Lexer(object): self.filters.append(filter_) def _decode(self, text): + if sys.version_info[0] == 3: + return text if self.encoding == 'guess': try: text = text.decode('utf-8') @@ -249,7 +252,7 @@ class Lexer(object): elif self.stripnl: text = text.strip('\n') - if isinstance(text, unicode): + if sys.version_info[0] < 3 and isinstance(text, unicode): text = StringIO(text.encode('utf-8')) self.encoding = 'utf-8' else: diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 77448d7..8c8f74c 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -3,6 +3,7 @@ """This module contains classes representing syntactical elements of SQL.""" import re +import sys from sqlparse import tokens as T @@ -25,10 +26,15 @@ class Token(object): self.parent = None def __str__(self): - return unicode(self).encode('utf-8') + if sys.version_info[0] == 3: + return self.value + else: + return unicode(self).encode('utf-8') def __repr__(self): - short = self._get_repr_value().encode('utf-8') + short = self._get_repr_value() + if sys.version_info[0] < 3: + short = short.encode('utf-8') return '<%s \'%s\' at 0x%07x>' % (self._get_repr_name(), short, id(self)) @@ -147,10 +153,22 @@ class TokenList(Token): if tokens is None: tokens = [] self.tokens = tokens - Token.__init__(self, None, unicode(self)) + Token.__init__(self, None, self._to_string()) def __unicode__(self): - return ''.join(unicode(x) for x in self.flatten()) + return self._to_string() + + def __str__(self): + str_ = self._to_string() + if sys.version_info[0] < 2: + str_ = str_.encode('utf-8') + return str_ + + def _to_string(self): + if sys.version_info[0] == 3: + return ''.join(x.value for x in self.flatten()) + else: + return ''.join(unicode(x) for x in self.flatten()) def _get_repr_name(self): return self.__class__.__name__ |
