-
Notifications
You must be signed in to change notification settings - Fork 23
fix(sessions): Escape strings in the sessions __repr__ output #361
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
leongdl
merged 4 commits into
OpenJobDescription:mainline
from
leongdl:fix/repr-escaping-sessions
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
673a7b4
fix(sessions): Escape strings in the sessions __repr__ output
leongdl 4a464df
test(sessions): Gate the PosixSessionUser repr tests on posix
leongdl 9a961e2
fix(sessions): Address review findings on the repr escaping
leongdl 1fb4d46
test(sessions): Bound the Session repr cases by filename portability
leongdl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| mod expr; | ||
| mod model; | ||
| mod pickle_helpers; | ||
| mod py_repr; | ||
| mod sessions; | ||
|
|
||
| use pyo3::prelude::*; | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Rendering values into `__repr__` output that Python can parse. | ||
| //! | ||
| //! `format!("{:?}", s)` is not a Python literal writer. Rust's `Debug` for | ||
| //! `str` special-cases only the quote, the backslash, and NUL, tab, CR and | ||
| //! LF; every other control character and every non-printable falls through | ||
| //! to Rust's brace form, `\u{1b}` or `\u{a0}`. Python wants exactly four | ||
| //! hex digits after `\u`, so those do not parse. ESC is the one to keep in | ||
| //! mind: ANSI colour sequences in captured process output hit this far more | ||
| //! often than any exotic codepoint does. | ||
| //! | ||
| //! Delegating to CPython's own `repr()` removes the guesswork rather than | ||
| //! reimplementing its escaping table: the output is by construction | ||
| //! whatever the running interpreter produces, including its per-string | ||
| //! choice of quote character. | ||
| //! | ||
| //! Scope: the `sessions` reprs route through here. Reprs under `model/` | ||
| //! and the rest of `expr/` still use `{:?}` or hand-rolled quoting and | ||
| //! carry the same defect — see the tracking note in the pull request that | ||
| //! introduced this module. A new repr should use these helpers. | ||
| //! | ||
| //! Callers must not hold a lock across `py_str`: it re-enters the | ||
| //! interpreter, which can run arbitrary Python (allocation may trigger a | ||
| //! GC pass and with it `__del__` and weakref callbacks). Read what you | ||
| //! need out from under the guard, drop it, then format. | ||
|
|
||
| use pyo3::prelude::*; | ||
| use pyo3::types::{PyString, PyStringMethods}; | ||
|
|
||
| /// CPython's `repr()` of `value`, ready to embed in a `__repr__`. | ||
| pub(crate) fn py_str(py: Python<'_>, value: &str) -> PyResult<String> { | ||
|
leongdl marked this conversation as resolved.
|
||
| // `to_cow` reads the UTF-8 directly and propagates failure. Going via | ||
| // `to_string()` would resolve to PyO3's `Display`, which calls `str()` | ||
| // on the object -- a second interpreter round-trip whose error has | ||
| // nowhere to go but a panic out of `__repr__`. | ||
| Ok(PyString::new(py, value).repr()?.to_cow()?.into_owned()) | ||
| } | ||
|
|
||
|
leongdl marked this conversation as resolved.
|
||
| /// An optional `int` as Python spells it. `Debug` would emit `Some(0)`, | ||
| /// which evaluates to a `NameError`. | ||
| pub(crate) fn py_opt_int(value: Option<i32>) -> String { | ||
| match value { | ||
| Some(v) => v.to_string(), | ||
| None => "None".to_string(), | ||
| } | ||
| } | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.