From a1b35aaa3ed5a5e6395cb71bf43f2184fb5952c3 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 17 Apr 2018 13:27:53 -0600 Subject: [PATCH 1/3] bpo-33262: Deprecate passing None for `s` to shlex.split() This reads the string to split from standard input. --- Doc/library/shlex.rst | 2 ++ Lib/shlex.py | 4 ++++ Lib/test/test_shlex.py | 7 ++++++- .../next/Library/2018-04-17-13-23-29.bpo-33262.vHC7YQ.rst | 2 ++ 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-04-17-13-23-29.bpo-33262.vHC7YQ.rst diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst index fb335c69006816..67e92202e10cf0 100644 --- a/Doc/library/shlex.rst +++ b/Doc/library/shlex.rst @@ -36,6 +36,8 @@ The :mod:`shlex` module defines the following functions: instance, passing ``None`` for *s* will read the string to split from standard input. + .. deprecated:: 3.8 + Passing ``None`` for *s*. .. function:: quote(s) diff --git a/Lib/shlex.py b/Lib/shlex.py index 2c9786c517a350..514ecf89241b92 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -298,6 +298,10 @@ def __next__(self): return token def split(s, comments=False, posix=True): + if s is None: + import warnings + warnings.warn("Passing None for 's' to shlex.split() is deprecated.", + DeprecationWarning, stacklevel=2) lex = shlex(s, posix=posix) lex.whitespace_split = True if not comments: diff --git a/Lib/test/test_shlex.py b/Lib/test/test_shlex.py index fd35788e81b272..8c6297eb48ec55 100644 --- a/Lib/test/test_shlex.py +++ b/Lib/test/test_shlex.py @@ -2,7 +2,7 @@ import shlex import string import unittest - +from unittest import mock # The original test data set was from shellwords, by Hartmut Goebel. @@ -161,6 +161,11 @@ def oldSplit(self, s): tok = lex.get_token() return ret + @mock.patch('sys.stdin', io.StringIO()) + def testSplitNoneDeprecation(self): + with self.assertWarns(DeprecationWarning): + shlex.split(None) + def testSplitPosix(self): """Test data splitting with posix parser""" self.splitTest(self.posix_data, comments=True) diff --git a/Misc/NEWS.d/next/Library/2018-04-17-13-23-29.bpo-33262.vHC7YQ.rst b/Misc/NEWS.d/next/Library/2018-04-17-13-23-29.bpo-33262.vHC7YQ.rst new file mode 100644 index 00000000000000..2afe13aeb0fca9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-04-17-13-23-29.bpo-33262.vHC7YQ.rst @@ -0,0 +1,2 @@ +Deprecate passing None as an argument for :func:`shlex.split()`'s ``s`` +parameter. Patch by Zackery Spytz. From afec0b254cd6c5f2fbd494c36bb1aeaa9febb846 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 6 Apr 2019 13:24:46 -0600 Subject: [PATCH 2/3] Update What's New. --- Doc/whatsnew/3.8.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 0fc4d774bcde2b..205ff78bc0aca2 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -622,6 +622,9 @@ Deprecated `. (Contributed by Serhiy Storchaka in :issue:`36492`.) +* Passing ``None`` as the first argument to the :func:`shlex.split` function + has been deprecated. (Contributed by Zackery Spytz in :issue:`33262`.) + API and Feature Removals ======================== From d491fbdea8d4776ed6760035417ad3c86df07865 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sat, 28 Mar 2020 08:28:58 -0600 Subject: [PATCH 3/3] Fix shlex.rst --- Doc/library/shlex.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/shlex.rst b/Doc/library/shlex.rst index 0a15656ae4c4a8..7f7f0c7f124ac5 100644 --- a/Doc/library/shlex.rst +++ b/Doc/library/shlex.rst @@ -37,7 +37,8 @@ The :mod:`shlex` module defines the following functions: standard input. .. deprecated:: 3.9 - Passing ``None`` for *s* will raise an exception in future Python versions. + Passing ``None`` for *s* will raise an exception in future Python + versions. .. function:: join(split_command)