55from os .path import abspath , join
66
77from sqlparse import sql , tokens as T
8+ from sqlparse .compat import u , text_type
89from sqlparse .engine import FilterStack
910from sqlparse .lexer import tokenize
1011from sqlparse .pipeline import Pipeline
@@ -25,7 +26,7 @@ def __init__(self, case=None):
2526 if case is None :
2627 case = 'upper'
2728 assert case in ['lower' , 'upper' , 'capitalize' ]
28- self .convert = getattr (unicode , case )
29+ self .convert = getattr (text_type , case )
2930
3031 def process (self , stack , stream ):
3132 for ttype , value in stream :
@@ -52,7 +53,7 @@ class TruncateStringFilter:
5253
5354 def __init__ (self , width , char ):
5455 self .width = max (width , 1 )
55- self .char = unicode (char )
56+ self .char = u (char )
5657
5758 def process (self , stack , stream ):
5859 for ttype , value in stream :
@@ -154,7 +155,7 @@ def process(self, stack, stream):
154155 f .close ()
155156
156157 # There was a problem loading the include file
157- except IOError , err :
158+ except IOError as err :
158159 # Raise the exception to the interpreter
159160 if self .raiseexceptions :
160161 raise
@@ -171,7 +172,7 @@ def process(self, stack, stream):
171172 self .raiseexceptions )
172173
173174 # Max recursion limit reached
174- except ValueError , err :
175+ except ValueError as err :
175176 # Raise the exception to the interpreter
176177 if self .raiseexceptions :
177178 raise
@@ -300,7 +301,7 @@ def _flatten_up_to_token(self, token):
300301 raise StopIteration
301302
302303 def _get_offset (self , token ):
303- raw = '' .join (map (unicode , self ._flatten_up_to_token (token )))
304+ raw = '' .join (map (text_type , self ._flatten_up_to_token (token )))
304305 line = raw .splitlines ()[- 1 ]
305306 # Now take current offset into account and return relative offset.
306307 full_offset = len (line ) - len (self .char * (self .width * self .indent ))
@@ -340,7 +341,7 @@ def _next_token(i):
340341 if prev and prev .is_whitespace () and prev not in added :
341342 tlist .tokens .pop (tlist .token_index (prev ))
342343 offset += 1
343- uprev = unicode (prev )
344+ uprev = u (prev )
344345 if (prev and (uprev .endswith ('\n ' ) or uprev .endswith ('\r ' ))):
345346 nl = tlist .token_next (token )
346347 else :
@@ -462,7 +463,7 @@ def process(self, stack, stmt):
462463 self ._process (stmt )
463464 if isinstance (stmt , sql .Statement ):
464465 if self ._last_stmt is not None :
465- if unicode (self ._last_stmt ).endswith ('\n ' ):
466+ if u (self ._last_stmt ).endswith ('\n ' ):
466467 nl = '\n '
467468 else :
468469 nl = '\n \n '
@@ -494,7 +495,7 @@ def _process(self, stack, group, stream):
494495 and not token .__class__ in self .keep_together ):
495496 token .tokens = self ._process (stack , token , token .tokens )
496497 else :
497- val = unicode (token )
498+ val = u (token )
498499 if len (self .line ) + len (val ) > self .width :
499500 match = re .search ('^ +' , self .line )
500501 if match is not None :
@@ -568,7 +569,7 @@ def process(self, stack, stream):
568569class SerializerUnicode :
569570
570571 def process (self , stack , stmt ):
571- raw = unicode (stmt )
572+ raw = u (stmt )
572573 lines = split_unquoted_newlines (raw )
573574 res = '\n ' .join (line .rstrip () for line in lines )
574575 return res
@@ -578,7 +579,7 @@ def Tokens2Unicode(stream):
578579 result = ""
579580
580581 for _ , value in stream :
581- result += unicode (value )
582+ result += u (value )
582583
583584 return result
584585
@@ -600,7 +601,7 @@ def process(self, stack, stmt):
600601 else :
601602 varname = self .varname
602603
603- has_nl = len (unicode (stmt ).strip ().splitlines ()) > 1
604+ has_nl = len (u (stmt ).strip ().splitlines ()) > 1
604605 stmt .tokens = self ._process (stmt .tokens , varname , has_nl )
605606 return stmt
606607
0 commit comments