Skip to content

Port configurable repr quote from Ruff and refactoring #4951

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 4 commits into from
May 5, 2023

Conversation

youknowone
Copy link
Member

Logic was ported, but kept the default repr behaviors to use single quote.
I think the term repr - not representation or debug - means repr() in Python.
Instead of it, I added other configurable methods for each str and bytes.

Rather than the name repr, I found unicode_escape codec is doing the same thing.
I will try to organize them more under 'escape' later.

I realized str and bytes are doing almost same thing but have so much different interface.


Unrelated note: I am thinking the new crate name for these stuffs rustpython-literal including str/bytes/float repr

youknowone and others added 2 commits May 1, 2023 03:08
Co-Authored-By: Charlie Marsh <charlie.r.marsh@gmail.com>
@youknowone youknowone marked this pull request as draft May 2, 2023 18:27
@youknowone youknowone force-pushed the repr-quote branch 3 times, most recently from 6251537 to d21066d Compare May 3, 2023 02:01

let quote = if dquoted { '"' } else { '\'' };
// if we don't need to escape anything we can just copy
let unchanged = out_len == in_len;
Copy link
Member Author

@youknowone youknowone May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to line 406, this test seems always failing. It must have been always using slow path

Comment on lines +109 to +115
pub fn with_forced_quote(source: &'a str, quote: Quote) -> Self {
let layout = EscapeLayout { quote, len: None };
Self { source, layout }
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python requires length calculation due to allocating string object, but formatters doesn't.

will this be helpful for formatter?

let escape = if source.len() < TOO_LONG_TO_FORMAT {
    UnicodeEscape::output_layout(source, Quote::Double)
} else {
    UnicodeEscape::with_forced_quote(source, Quote::Double)
};

@youknowone youknowone marked this pull request as ready for review May 3, 2023 06:04
@youknowone youknowone requested a review from charliermarsh May 3, 2023 06:04
@youknowone youknowone force-pushed the repr-quote branch 2 times, most recently from 73f2689 to c8ee020 Compare May 3, 2023 06:59
@youknowone youknowone changed the title Port configurable repr quote from Ruff Port configurable repr quote from Ruff and refactoring May 3, 2023
@youknowone youknowone merged commit cea23d2 into RustPython:main May 5, 2023
@youknowone youknowone deleted the repr-quote branch May 5, 2023 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants