bpo-33262: Deprecate passing None for `s` to shlex.split() (GH-6514) · python/cpython@975ac32

5 files changed

lines changed

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
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

=======

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:

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)

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.