Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-118761: Optimise import time for shlex #132036

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 24, 2025
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 16 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
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
import re
import sys
from collections import deque
picnixz marked this conversation as resolved.
Show resolved Hide resolved

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
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.