Skip to content

Commit b05bc5a

Browse files
committed
Convert string literals to unicode for Py27
Working with non-ascii in Python require all-unicode approach, but str literals in Python 2.7 are bytes. The patch makes them unicode. Syntax u'' is supported in Python 2.7 and 3.3+.
1 parent 791a331 commit b05bc5a

File tree

5 files changed

+14
-13
lines changed

5 files changed

+14
-13
lines changed

sqlparse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def format(sql, encoding=None, **options):
5757
options = formatter.validate_options(options)
5858
stack = formatter.build_filter_stack(stack, options)
5959
stack.postprocess.append(filters.SerializerUnicode())
60-
return ''.join(stack.run(sql, encoding))
60+
return u''.join(stack.run(sql, encoding))
6161

6262

6363
def split(sql, encoding=None):

sqlparse/cli.py

100644100755
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def create_parser():
123123

124124
def _error(msg):
125125
"""Print msg and optionally exit with return code exit_."""
126-
sys.stderr.write('[ERROR] {0}\n'.format(msg))
126+
sys.stderr.write(u'[ERROR] {0}\n'.format(msg))
127127
return 1
128128

129129

@@ -138,21 +138,22 @@ def main(args=None):
138138
# TODO: Needs to deal with encoding
139139
data = ''.join(open(args.filename).readlines())
140140
except IOError as e:
141-
return _error('Failed to read {0}: {1}'.format(args.filename, e))
141+
return _error(
142+
u'Failed to read {0}: {1}'.format(args.filename, e))
142143

143144
if args.outfile:
144145
try:
145146
stream = open(args.outfile, 'w')
146147
except IOError as e:
147-
return _error('Failed to open {0}: {1}'.format(args.outfile, e))
148+
return _error(u'Failed to open {0}: {1}'.format(args.outfile, e))
148149
else:
149150
stream = sys.stdout
150151

151152
formatter_opts = vars(args)
152153
try:
153154
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
154155
except SQLParseError as e:
155-
return _error('Invalid options: {0}'.format(e))
156+
return _error(u'Invalid options: {0}'.format(e))
156157

157158
s = sqlparse.format(data, **formatter_opts)
158159
if PY2:

sqlparse/filters/output.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _process(self, stream, varname, has_nl):
2222
def process(self, stmt):
2323
self.count += 1
2424
if self.count > 1:
25-
varname = '{f.varname}{f.count}'.format(f=self)
25+
varname = u'{f.varname}{f.count}'.format(f=self)
2626
else:
2727
varname = self.varname
2828

sqlparse/filters/reindent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def leading_ws(self):
3636
return self.offset + self.indent * self.width
3737

3838
def _get_offset(self, token):
39-
raw = ''.join(map(text_type, self._flatten_up_to_token(token)))
39+
raw = u''.join(map(text_type, self._flatten_up_to_token(token)))
4040
line = (raw or '\n').splitlines()[-1]
4141
# Now take current offset into account and return relative offset.
4242
return len(line) - len(self.char * self.leading_ws)

sqlparse/sql.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ def __repr__(self):
4848
cls = self._get_repr_name()
4949
value = self._get_repr_value()
5050

51-
q = '"' if value.startswith("'") and value.endswith("'") else "'"
52-
return "<{cls} {q}{value}{q} at 0x{id:2X}>".format(
51+
q = u'"' if value.startswith("'") and value.endswith("'") else u"'"
52+
return u"<{cls} {q}{value}{q} at 0x{id:2X}>".format(
5353
id=id(self), **locals())
5454

5555
def _get_repr_name(self):
@@ -143,7 +143,7 @@ def __init__(self, tokens=None):
143143
self.is_group = True
144144

145145
def __str__(self):
146-
return ''.join(token.value for token in self.flatten())
146+
return u''.join(token.value for token in self.flatten())
147147

148148
# weird bug
149149
# def __len__(self):
@@ -160,13 +160,13 @@ def _get_repr_name(self):
160160

161161
def _pprint_tree(self, max_depth=None, depth=0, f=None):
162162
"""Pretty-print the object tree."""
163-
indent = ' | ' * depth
163+
indent = u' | ' * depth
164164
for idx, token in enumerate(self.tokens):
165165
cls = token._get_repr_name()
166166
value = token._get_repr_value()
167167

168-
q = '"' if value.startswith("'") and value.endswith("'") else "'"
169-
print("{indent}{idx:2d} {cls} {q}{value}{q}"
168+
q = u'"' if value.startswith("'") and value.endswith("'") else u"'"
169+
print(u"{indent}{idx:2d} {cls} {q}{value}{q}"
170170
.format(**locals()), file=f)
171171

172172
if token.is_group and (max_depth is None or depth < max_depth):

0 commit comments

Comments
 (0)