]> granicus.if.org Git - python/commitdiff
#18853: Fix resource warning in shlex's __main__ section.
authorR David Murray <rdmurray@bitdance.com>
Sat, 18 Oct 2014 00:28:47 +0000 (20:28 -0400)
committerR David Murray <rdmurray@bitdance.com>
Sat, 18 Oct 2014 00:28:47 +0000 (20:28 -0400)
Report and original fix by Vajrasky Kok.

Lib/shlex.py
Misc/NEWS

index 69f3b456af103cc9067c30be5075d0fdf1e7001c..4672553f1cc7c9b6f6dea3668d5f206958e5f5aa 100644 (file)
@@ -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))
index 9c50f3e22abedf7801f67aac242d7bd414ebe160..2738699484a965ec55f8f1e5540ab2d2ecd7ac0a 100644 (file)
--- 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.