Vendor CPython's shlex module

Merged
EV
4d ago
+1084-10

AI disclosure: implemented with gemini-4-carbon, reviewed against CPython 3.14 with gpt-6.2-astra.

Summary

Vendors CPython 3.14.10's pure-Python Lib/shlex.py under #1275 with zero runtime patches and zero typeshed stub patches, and enables CPython's upstream test_shlex.py suite.

Motivation

In recent interactive fast-agent PTC sessions, models frequently reach for shlex.quote() / shlex.join() / shlex.split() when building shell commands:

  • 4× import shlex failures (gpt-6.2-astra: 2×, gemini-4-neon: 2×) + 8× manual def sq(s): ... quoting workarounds written by gemini-4-neon after hitting import-not-found.

With io.StringIO (#1420), generator expressions (#1435), and str.maketrans/translate (#1301) now on main, CPython's Lib/shlex.py loads and runs unmodified.

Changes

  • Stdlib vendoring & registration:
    • Added "shlex.py" to crates/monty/stdlib/vendor.toml (generated crates/monty/stdlib/Lib/shlex.py and crates/monty/src/modules/vendored/sources.rs via make vendor-stdlib).
    • Updated crates/monty/stdlib/NOTICE and synced crates/monty-runtime/LICENSE via make update-licenses.
    • Registered "shlex" in crates/monty-types/src/type_checking.rs (SANDBOX_MODULE_NAMES) and crates/monty-js/ts/externalModule.ts (SANDBOX_MODULES).
  • Typeshed & type checking:
    • Added "shlex.pyi" in crates/monty-typeshed/vendor.toml, enabled shlex: 3.0- in 0001-versions.patch, and synced via make update-typeshed.
    • Added shlex type-checking assertions in crates/monty-type-checking/tests/good_types.py.
  • CPython upstream suite (test_shlex.py) & runner isolation:
    • Enabled "test_shlex.py" in crates/monty/stdlib/vendor.toml with minimal test-only patches (0008-shlex.patch, 0009-shlex-type-errors.patch), skips.toml (18 exclusions for unittest.mock, tempfile, string, sys.stdin, import_helper), and assertNotIn in unittest.pyi.
    • Temporarily restores os._monty_original_environ / os._monty_original_getenv inside scripts/run_stdlib_suite.py's _load_suite context (restoring in finally) and ensures test_fixtures.py initialization finishes before taking CPYTHON_TEST_LOCK in crates/monty-datatest/src/stdlib_suite.rs, so test.support.script_helper works without weakening VirtualEnviron isolation for parity cases.
  • Docs:
    • Updated docs/limitations/modules.md and docs/limitations/index.md (shlex entries, instream=None without sys.stdin, and shlex.split(f.read()) vs iterating a configured shlex.shlex(f, ...) on unread open() streams).

Tests

  • Added crates/monty/test_cases/shlex__module.py covering quote(), join(), split() (POSIX/non-POSIX, comments, escapes, Unicode, StringIO, errors), shlex.shlex lexer features, sourcehook, and Monty-specific behavior.

  • Verified make check-stdlib, make check-typeshed, make check-stdlib-patches, monty-datatest --features cpython-tests -- shlex (5/5), os__environ (2/2), full 831-case Monty suite, make test-type-checking, make format-rs, make lint-rs, make lint-py, make lint-md.

  • I have read the contribution guide.

  • I understand and can explain the proposed changes, including any generated with AI assistance.


Summary by cubic

Vendors CPython 3.14's shlex module so shlex.quote(), shlex.join(), and shlex.split() work for building shell commands, and enables the upstream test_shlex.py suite.

Notes

  • shlex runs CPython's source unmodified; it loads because io.StringIO, generator expressions, and str.translate are now supported.
  • shlex.shlex() with no instream raises AttributeError because Monty has no sys.stdin.
  • shlex.split(f) on an unread open() stream raises NotImplementedError; use shlex.split(f.read()) or iterate a configured shlex.shlex(f, ...) instance.

Tests

  • 17 upstream tests are excluded because they need unittest.mock, tempfile, string, or test.support.import_helper.
  • Adds a Monty test case covering quoting, splitting, lexer features, sourcehook, and error paths.