Repository navigation
🐛 fix(xonsh): preserve literals across source encodings - #3432
Merged
gaborbernat merged 4 commits intoOct 17, 2026
Merged
Conversation
gaborbernat
added a commit
to gaborbernat/virtualenv
that referenced
this pull request
Oct 17, 2026
Replace the quote test that still described repr() with the round trip through GBK from pypa#3432, and give quote() the same docstring, so the two PRs merge without conflict in either order. Assert the warning the generator logs when it skips an expanding destination, and run xonsh through one fixture instead of three copies of the subprocess call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The xonsh activation script holds the environment path and the prompt as Python string literals, written by
reprinto a UTF-8 file. xonsh reads the file with the codec named by$XONSH_ENCODING. Under GBK or Big5, the last byte of a non-ASCII character pairs with the ASCII byte after it, so a backslash in front of an escaped quote disappears, the quote ends the literal early, and the rest of the value runs as code when the script is sourced. The prompt is a command-line argument, so whoever supplies it supplies the code. 🐛 After this PR every literal in the script is ASCII and decodes to the same text under any ASCII-compatible codec. This resolves GHSA-394h-qwmc-48pf. #3411 carries the samequotechange next to its activation-state fixes, so the two merge in either order.Source decoding merged a backslash into the character before it
quote, from #3125, returnedreprof the value.reprkeeps printable non-ASCII characters as they are, and when the value holds both kinds of quote it delimits with'and writes the inner'as the escape sequence\'. The generator writes the file as UTF-8. xonsh'ssourceopens it with$XONSH_ENCODINGand$XONSH_ENCODING_ERRORS(source_alias_fn,open), whose defaults aresys.getdefaultencoding()andsurrogateescape, and hands the text to the parser. GBK and Big5 are double-byte codecs (standard encodings): a lead byte between 0x81 and 0xFE takes the next byte in the range 0x40 to 0xFE as its trail. The UTF-8 form of中ise4 b8 ad, and\is5c, so中\decodes as the two characters涓璡and the backslash is gone. A prompt written as'中\'…'therefore reads as'涓璡'…': the literal closes after two characters, and the tokenizer continues with the rest of the value as statements. With'and"in a prompt, a;and a#make that remainder a valid statement followed by a comment; sourced underXONSH_ENCODING=gbkorbig5the old script runs it. A value whose last byte has no valid trail, such as中followed by', is read throughsurrogateescapeas a lone surrogate instead: the literal keeps its shape and the value is wrong.quotenow returnsasciiof the value (quote).asciiwrites every non-ASCII character as a\x,\uor\Uescape, so every byte of the file is below 0x80, no double-byte codec pairs two of them, and the literal parser turns中back into中. The Python activator took the same step in #3402 (quote). The quote test encodes the literal as ASCII, decodes it as GBK and checks thatast.literal_evalreturns the original value, for a value that holds中, a quote and a backslash.sequenceDiagram participant G as generator participant F as activate.xsh, UTF-8 bytes participant D as xonsh source decoder participant P as literal parser rect rgb(255, 214, 214) G->>F: repr: 27 e4 b8 ad 5c 27 for '中\' F->>D: $XONSH_ENCODING=gbk D->>P: '涓璡' and the backslash is gone Note over P: literal closes early, the rest of the value runs as code end rect rgb(216, 243, 220) G->>F: ascii: 27 5c 75 34 65 32 64 5c 27 for '\u4e2d\' F->>D: any ASCII-compatible codec D->>P: text unchanged Note over P: escapes decode back to 中 and ' endBehavior changes
Generated
activate.xshfiles write non-ASCII characters in the environment path and prompt as escapes instead of literally. Scripts generated by earlier versions are not rewritten; running virtualenv on the environment again regenerates them. Nothing else in the script changes.