Update shlex from v3.14.2-288-g06f9c8ca1c

This commit is contained in:
CPython Developers authored and Jeong, YunWon committed 2026-02-02 08:10:15 +09:00
1 parent a1f08386a2
commit 5662e988b0
2 files changed
+26 -15

No files matched your search

+13 -7
View File
@@ -7,11 +7,7 @@
# iterator interface by Gustavo Niemeyer, April 2003.
# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.
import os
import re
import sys
from collections import deque
from io import StringIO
__all__ = ["shlex", "split", "quote", "join"]
@@ -20,6 +16,8 @@ class shlex:
"A lexical analyzer class for simple shell-like syntaxes."
def __init__(self, instream=None, infile=None, posix=False,
punctuation_chars=False):
from collections import deque # deferred import for performance
if isinstance(instream, str):
instream = StringIO(instream)
if instream is not None:
@@ -278,6 +276,7 @@ class shlex:
def sourcehook(self, newfile):
"Hook called on a filename to be sourced."
import os.path
if newfile[0] == '"':
newfile = newfile[1:-1]
# This implements cpp-like semantics for relative-path inclusion.
@@ -318,13 +317,20 @@ def join(split_command):
return ' '.join(quote(arg) for arg in split_command)
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
def quote(s):
"""Return a shell-escaped version of the string *s*."""
if not s:
return "''"
if _find_unsafe(s) is None:
if not isinstance(s, str):
raise TypeError(f"expected string object, got {type(s).__name__!r}")
# Use bytes.translate() for performance
safe_chars = (b'%+,-./0123456789:=@'
b'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'
b'abcdefghijklmnopqrstuvwxyz')
# No quoting is needed if `s` is an ASCII string consisting only of `safe_chars`
if s.isascii() and not s.encode().translate(None, delete=safe_chars):
return s
# use single quotes, and put single quotes into double quotes
+13 -8
View File
@@ -3,6 +3,8 @@ import itertools
import shlex
import string
import unittest
from test.support import cpython_only
from test.support import import_helper
# The original test data set was from shellwords, by Hartmut Goebel.
@@ -165,14 +167,12 @@ class ShlexTest(unittest.TestCase):
with self.assertRaises(ValueError):
shlex.split(None)
# TODO: RUSTPYTHON; ValueError: Error Retrieving Value
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON; ValueError: Error Retrieving Value
def testSplitPosix(self):
"""Test data splitting with posix parser"""
self.splitTest(self.posix_data, comments=True)
# TODO: RUSTPYTHON; ValueError: Error Retrieving Value
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON; ValueError: Error Retrieving Value
def testCompat(self):
"""Test compatibility interface"""
for i in range(len(self.data)):
@@ -313,8 +313,7 @@ class ShlexTest(unittest.TestCase):
s = shlex.shlex("'')abc", punctuation_chars=True)
self.assertEqual(list(s), expected)
# TODO: RUSTPYTHON; ValueError: Error Retrieving Value
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON; ValueError: Error Retrieving Value
def testUnicodeHandling(self):
"""Test punctuation_chars and whitespace_split handle unicode."""
ss = "\u2119\u01b4\u2602\u210c\u00f8\u1f24"
@@ -334,6 +333,7 @@ class ShlexTest(unittest.TestCase):
unsafe = '"`$\\!' + unicode_sample
self.assertEqual(shlex.quote(''), "''")
self.assertEqual(shlex.quote(None), "''")
self.assertEqual(shlex.quote(safeunquoted), safeunquoted)
self.assertEqual(shlex.quote('test file name'), "'test file name'")
for u in unsafe:
@@ -342,6 +342,8 @@ class ShlexTest(unittest.TestCase):
for u in unsafe:
self.assertEqual(shlex.quote("test%s'name'" % u),
"'test%s'\"'\"'name'\"'\"''" % u)
self.assertRaises(TypeError, shlex.quote, 42)
self.assertRaises(TypeError, shlex.quote, b"abc")
def testJoin(self):
for split_command, command in [
@@ -354,8 +356,7 @@ class ShlexTest(unittest.TestCase):
joined = shlex.join(split_command)
self.assertEqual(joined, command)
# TODO: RUSTPYTHON; ValueError: Error Retrieving Value
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON; ValueError: Error Retrieving Value
def testJoinRoundtrip(self):
all_data = self.data + self.posix_data
for command, *split_command in all_data:
@@ -371,6 +372,10 @@ class ShlexTest(unittest.TestCase):
with self.assertRaises(AttributeError):
shlex_instance.punctuation_chars = False
@cpython_only
def test_lazy_imports(self):
import_helper.ensure_lazy_imports('shlex', {'collections', 're', 'os'})
# Allow this test to be used with old shlex.py
if not getattr(shlex, "split", None):