From a25569e742b4884d2198c0ea6a6f804e657f6f6d Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Mon, 31 Dec 2012 22:54:59 +0100 Subject: 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! --- sqlparse/sql.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'sqlparse/sql.py') 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__ -- cgit v1.2.1