Skip to content

bpo-33262: Deprecate passing None for s to shlex.split() #6514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/library/shlex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ The :mod:`shlex` module defines the following functions:
instance, passing ``None`` for *s* will read the string to split from
standard input.

.. deprecated:: 3.9
Passing ``None`` for *s* will raise an exception in future Python
versions.

.. function:: join(split_command)

Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,9 @@ Deprecated
by :c:func:`Py_Initialize()` since Python 3.7.
(Contributed by Victor Stinner in :issue:`39877`.)

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


Removed
=======
Expand Down
4 changes: 4 additions & 0 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ def __next__(self):

def split(s, comments=False, posix=True):
"""Split the string *s* using shell-like syntax."""
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:
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shlex
import string
import unittest

from unittest import mock


# The original test data set was from shellwords, by Hartmut Goebel.
Expand Down Expand Up @@ -162,6 +162,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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate passing None as an argument for :func:`shlex.split()`'s ``s``
parameter. Patch by Zackery Spytz.