Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 838f2c4

Browse files
committed
#18853: Fix resource warning in shlex's __main__ section.
Report and original fix by Vajrasky Kok.
1 parent 7570cbd commit 838f2c4

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

Lib/shlex.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,17 @@ def quote(s):
290290
return "'" + s.replace("'", "'\"'\"'") + "'"
291291

292292

293-
if __name__ == '__main__':
294-
if len(sys.argv) == 1:
295-
lexer = shlex()
296-
else:
297-
file = sys.argv[1]
298-
lexer = shlex(open(file), file)
293+
def _print_tokens(lexer):
299294
while 1:
300295
tt = lexer.get_token()
301-
if tt:
302-
print("Token: " + repr(tt))
303-
else:
296+
if not tt:
304297
break
298+
print("Token: " + repr(tt))
299+
300+
if __name__ == '__main__':
301+
if len(sys.argv) == 1:
302+
_print_tokens(shlex())
303+
else:
304+
fn = sys.argv[1]
305+
with open(fn) as f:
306+
_print_tokens(shlex(f, fn))

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Core and Builtins
3333
Library
3434
-------
3535

36+
- Issue #18853: Fixed ResourceWarning in shlex.__nain__.
37+
3638
- Issue #9351: Defaults set with set_defaults on an argparse subparser
3739
are no longer ignored when also set on the parent parser.
3840

0 commit comments

Comments
 (0)