Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 11 additions & 5 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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:
Expand Down Expand Up @@ -278,6 +276,7 @@ def read_token(self):

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.
Expand Down Expand Up @@ -318,7 +317,14 @@ def join(split_command):
return ' '.join(quote(arg) for arg in split_command)


_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
def _find_unsafe(s, /):
# this function replaces itself with the compiled pattern on execution,
# to allow as deferred import of re for performance
global _find_unsafe
import re
_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
return _find_unsafe(s)


def quote(s):
"""Return a shell-escaped version of the string *s*."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve import times by up to 33x for the :mod:`shlex` module. Patch by Adam
Turner.
Loading