Vendor CPython's shlex module
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 shlexfailures (gpt-6.2-astra: 2×,gemini-4-neon: 2×) + 8× manualdef sq(s): ...quoting workarounds written bygemini-4-neonafter hittingimport-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"tocrates/monty/stdlib/vendor.toml(generatedcrates/monty/stdlib/Lib/shlex.pyandcrates/monty/src/modules/vendored/sources.rsviamake vendor-stdlib). - Updated
crates/monty/stdlib/NOTICEand syncedcrates/monty-runtime/LICENSEviamake update-licenses. - Registered
"shlex"incrates/monty-types/src/type_checking.rs(SANDBOX_MODULE_NAMES) andcrates/monty-js/ts/externalModule.ts(SANDBOX_MODULES).
- Added
- Typeshed & type checking:
- Added
"shlex.pyi"incrates/monty-typeshed/vendor.toml, enabledshlex: 3.0-in0001-versions.patch, and synced viamake update-typeshed. - Added
shlextype-checking assertions incrates/monty-type-checking/tests/good_types.py.
- Added
- CPython upstream suite (
test_shlex.py) & runner isolation:- Enabled
"test_shlex.py"incrates/monty/stdlib/vendor.tomlwith minimal test-only patches (0008-shlex.patch,0009-shlex-type-errors.patch),skips.toml(18 exclusions forunittest.mock,tempfile,string,sys.stdin,import_helper), andassertNotIninunittest.pyi. - Temporarily restores
os._monty_original_environ/os._monty_original_getenvinsidescripts/run_stdlib_suite.py's_load_suitecontext (restoring infinally) and ensurestest_fixtures.pyinitialization finishes before takingCPYTHON_TEST_LOCKincrates/monty-datatest/src/stdlib_suite.rs, sotest.support.script_helperworks without weakeningVirtualEnvironisolation for parity cases.
- Enabled
- Docs:
- Updated
docs/limitations/modules.mdanddocs/limitations/index.md(shlexentries,instream=Nonewithoutsys.stdin, andshlex.split(f.read())vs iterating a configuredshlex.shlex(f, ...)on unreadopen()streams).
- Updated
Tests
-
Added
crates/monty/test_cases/shlex__module.pycoveringquote(),join(),split()(POSIX/non-POSIX, comments, escapes, Unicode,StringIO, errors),shlex.shlexlexer 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
shlexruns CPython's source unmodified; it loads becauseio.StringIO, generator expressions, andstr.translateare now supported.shlex.shlex()with noinstreamraisesAttributeErrorbecause Monty has nosys.stdin.shlex.split(f)on an unreadopen()stream raisesNotImplementedError; useshlex.split(f.read())or iterate a configuredshlex.shlex(f, ...)instance.
Tests
- 17 upstream tests are excluded because they need
unittest.mock,tempfile,string, ortest.support.import_helper. - Adds a Monty test case covering quoting, splitting, lexer features,
sourcehook, and error paths.