summaryrefslogtreecommitdiff
path: root/sqlparse/sql.py
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2012-12-31 22:54:59 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2012-12-31 22:54:59 +0100
commita25569e742b4884d2198c0ea6a6f804e657f6f6d (patch)
tree841812458a62648f36d17368e131362c4d8a3239 /sqlparse/sql.py
parentf85b696fc10076be3e9fcf34085ef864f2121026 (diff)
downloadsqlparse-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/sql.py')
-rw-r--r--sqlparse/sql.py26
1 files changed, 22 insertions, 4 deletions
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__