From bf9ce73e3720bdbf1cc671f35f4f299511d59650 Mon Sep 17 00:00:00 2001 From: Andi Albrecht Date: Sun, 24 Sep 2017 09:14:35 +0200 Subject: Close files during tests. --- sqlparse/cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'sqlparse') diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 0b5c204..ad6bc7a 100755 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -154,14 +154,17 @@ def main(args=None): sys.stdin.buffer, encoding=args.encoding).read() else: try: - data = ''.join(open(args.filename, 'r', args.encoding).readlines()) + with open(args.filename, 'r', args.encoding) as f: + data = ''.join(f.readlines()) except IOError as e: return _error( u'Failed to read {0}: {1}'.format(args.filename, e)) + close_stream = False if args.outfile: try: stream = open(args.outfile, 'w', args.encoding) + close_stream = True except IOError as e: return _error(u'Failed to open {0}: {1}'.format(args.outfile, e)) else: @@ -176,4 +179,6 @@ def main(args=None): s = sqlparse.format(data, **formatter_opts) stream.write(s) stream.flush() + if close_stream: + stream.close() return 0 -- cgit v1.2.1