[Python-checkins] cpython (3.4): #18853: Fix resource warning in shlex's __main__ section.

r.david.murray python-checkins at python.org
Sat Oct 18 02:30:31 CEST 2014


https://hg.python.org/cpython/rev/4c2b77d0680b
changeset:   93123:4c2b77d0680b
branch:      3.4
parent:      93120:e9cb45ccf42b
user:        R David Murray <rdmurray at bitdance.com>
date:        Fri Oct 17 20:28:47 2014 -0400
summary:
  #18853: Fix resource warning in shlex's __main__ section.

Report and original fix by Vajrasky Kok.

files:
  Lib/shlex.py |  20 +++++++++++---------
  Misc/NEWS    |   2 ++
  2 files changed, 13 insertions(+), 9 deletions(-)


diff --git a/Lib/shlex.py b/Lib/shlex.py
--- a/Lib/shlex.py
+++ b/Lib/shlex.py
@@ -290,15 +290,17 @@
     return "'" + s.replace("'", "'\"'\"'") + "'"
 
 
+def _print_tokens(lexer):
+    while 1:
+        tt = lexer.get_token()
+        if not tt:
+            break
+        print("Token: " + repr(tt))
+
 if __name__ == '__main__':
     if len(sys.argv) == 1:
-        lexer = shlex()
+        _print_tokens(shlex())
     else:
-        file = sys.argv[1]
-        lexer = shlex(open(file), file)
-    while 1:
-        tt = lexer.get_token()
-        if tt:
-            print("Token: " + repr(tt))
-        else:
-            break
+        fn = sys.argv[1]
+        with open(fn) as f:
+            _print_tokens(shlex(f, fn))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,8 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list