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

Skip to content

Commit 975ac32

Browse files
authored
bpo-33262: Deprecate passing None for s to shlex.split() (GH-6514)
* bpo-33262: Deprecate passing None for `s` to shlex.split() This reads the string to split from standard input. * Update What's New. * Fix shlex.rst
1 parent 7c72383 commit 975ac32

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

Doc/library/shlex.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ The :mod:`shlex` module defines the following functions:
3636
instance, passing ``None`` for *s* will read the string to split from
3737
standard input.
3838

39+
.. deprecated:: 3.9
40+
Passing ``None`` for *s* will raise an exception in future Python
41+
versions.
3942

4043
.. function:: join(split_command)
4144

Doc/whatsnew/3.9.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ Deprecated
624624
by :c:func:`Py_Initialize()` since Python 3.7.
625625
(Contributed by Victor Stinner in :issue:`39877`.)
626626

627+
* Passing ``None`` as the first argument to the :func:`shlex.split` function
628+
has been deprecated. (Contributed by Zackery Spytz in :issue:`33262`.)
629+
627630

628631
Removed
629632
=======

Lib/shlex.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ def __next__(self):
304304

305305
def split(s, comments=False, posix=True):
306306
"""Split the string *s* using shell-like syntax."""
307+
if s is None:
308+
import warnings
309+
warnings.warn("Passing None for 's' to shlex.split() is deprecated.",
310+
DeprecationWarning, stacklevel=2)
307311
lex = shlex(s, posix=posix)
308312
lex.whitespace_split = True
309313
if not comments:

Lib/test/test_shlex.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shlex
44
import string
55
import unittest
6-
6+
from unittest import mock
77

88

99
# The original test data set was from shellwords, by Hartmut Goebel.
@@ -162,6 +162,11 @@ def oldSplit(self, s):
162162
tok = lex.get_token()
163163
return ret
164164

165+
@mock.patch('sys.stdin', io.StringIO())
166+
def testSplitNoneDeprecation(self):
167+
with self.assertWarns(DeprecationWarning):
168+
shlex.split(None)
169+
165170
def testSplitPosix(self):
166171
"""Test data splitting with posix parser"""
167172
self.splitTest(self.posix_data, comments=True)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate passing None as an argument for :func:`shlex.split()`'s ``s``
2+
parameter. Patch by Zackery Spytz.

0 commit comments

Comments
 (0)