summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2013-02-22 10:10:12 -0800
committerAndi Albrecht <albrecht.andi@gmail.com>2013-02-22 10:10:12 -0800
commite9e0aa77e5ea05080e1bb76e0de066a5d3eaa989 (patch)
tree22e2f0bb5392ae41da528c3ac77fb101c08916f3
parentdba96a147be55be7da657a17a38c21b30ff0e664 (diff)
parent629fd8c7b016aa43719b7112b75f427f67f2123a (diff)
downloadsqlparse-e9e0aa77e5ea05080e1bb76e0de066a5d3eaa989.tar.gz
Merge pull request #87 from pjdelport/bugfix/sqlformat-python3
Fix sqlformat script to be Python 3 compatible.
-rwxr-xr-xbin/sqlformat19
-rw-r--r--tox.ini5
2 files changed, 16 insertions, 8 deletions
diff --git a/bin/sqlformat b/bin/sqlformat
index dd56b2a..fcee452 100755
--- a/bin/sqlformat
+++ b/bin/sqlformat
@@ -50,7 +50,7 @@ _FORMATTING_GROUP = group
def _error(msg, exit_=None):
"""Print msg and optionally exit with return code exit_."""
- print >>sys.stderr, '[ERROR] %s' % msg
+ sys.stderr.write('[ERROR] %s\n' % msg)
if exit_ is not None:
sys.exit(exit_)
@@ -66,7 +66,7 @@ def _build_formatter_opts(options):
def main():
options, args = parser.parse_args()
if options.verbose:
- print >>sys.stderr, 'Verbose mode'
+ sys.stderr.write('Verbose mode\n')
if len(args) != 1:
_error('No input data.')
@@ -78,13 +78,15 @@ def main():
else:
try:
data = '\n'.join(open(args[0]).readlines())
- except OSError, err:
+ except OSError:
+ err = sys.exc_info()[1] # Python 2.5 compatibility
_error('Failed to read %s: %s' % (args[0], err), exit_=1)
if options.outfile:
try:
stream = open(options.outfile, 'w')
- except OSError, err:
+ except OSError:
+ err = sys.exc_info()[1] # Python 2.5 compatibility
_error('Failed to open %s: %s' % (options.outfile, err), exit_=1)
else:
stream = sys.stdout
@@ -92,11 +94,14 @@ def main():
formatter_opts = _build_formatter_opts(options)
try:
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
- except SQLParseError, err:
+ except SQLParseError:
+ err = sys.exc_info()[1] # Python 2.5 compatibility
_error('Invalid options: %s' % err, exit_=1)
- stream.write(sqlparse.format(data, **formatter_opts).encode('utf-8',
- 'replace'))
+ s = sqlparse.format(data, **formatter_opts)
+ if sys.version_info < (3,):
+ s = s.encode('utf-8', 'replace')
+ stream.write(s)
stream.flush()
diff --git a/tox.ini b/tox.ini
index fe5ef17..fe2a7de 100644
--- a/tox.ini
+++ b/tox.ini
@@ -5,11 +5,14 @@ envlist=py25,py26,py27,pypy,py32
deps=
pytest
pytest-cov
-commands=py.test --cov=sqlparse/ tests
+commands=
+ sqlformat --version # Sanity check.
+ py.test --cov=sqlparse/ tests
[testenv:py32]
changedir={envdir}
commands=
+ sqlformat --version # Sanity check.
rm -rf tests/
cp -r {toxinidir}/tests/ tests/
2to3 -w --no-diffs -n tests/