From: R David Murray Date: Sat, 18 Oct 2014 00:28:47 +0000 (-0400) Subject: #18853: Fix resource warning in shlex's __main__ section. X-Git-Tag: v3.4.3rc1~451 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=838f2c437db5796d316f106b1496d8d29268c5ca;p=thirdparty%2FPython%2Fcpython.git #18853: Fix resource warning in shlex's __main__ section. Report and original fix by Vajrasky Kok. --- diff --git a/Lib/shlex.py b/Lib/shlex.py index 69f3b456af10..4672553f1cc7 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -290,15 +290,17 @@ def quote(s): return "'" + s.replace("'", "'\"'\"'") + "'" -if __name__ == '__main__': - if len(sys.argv) == 1: - lexer = shlex() - else: - file = sys.argv[1] - lexer = shlex(open(file), file) +def _print_tokens(lexer): while 1: tt = lexer.get_token() - if tt: - print("Token: " + repr(tt)) - else: + if not tt: break + print("Token: " + repr(tt)) + +if __name__ == '__main__': + if len(sys.argv) == 1: + _print_tokens(shlex()) + else: + fn = sys.argv[1] + with open(fn) as f: + _print_tokens(shlex(f, fn)) diff --git a/Misc/NEWS b/Misc/NEWS index 9c50f3e22abe..2738699484a9 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -33,6 +33,8 @@ Core and Builtins Library ------- +- Issue #18853: Fixed ResourceWarning in shlex.__nain__. + - Issue #9351: Defaults set with set_defaults on an argparse subparser are no longer ignored when also set on the parent parser.