repr as single quote default

This commit is contained in:
Jeong YunWon
2023-05-01 03:30:34 +09:00
parent 2315ce956e
commit 9cc8f2dfa9
3 changed files with 23 additions and 9 deletions

View File

@@ -1,6 +1,10 @@
use crate::str::{Quote, ReprOverflowError};
pub fn repr(b: &[u8], quote: Quote) -> Result<String, ReprOverflowError> {
pub fn repr(b: &[u8]) -> Result<String, ReprOverflowError> {
repr_with(b, &[], "", Quote::Single)
}
pub fn repr_with_quote(b: &[u8], quote: Quote) -> Result<String, ReprOverflowError> {
repr_with(b, &[], "", quote)
}

View File

@@ -350,12 +350,8 @@ macro_rules! ascii {
/// Get a Display-able type that formats to the python `repr()` of the string value
#[inline]
pub fn repr(s: &str, quote: Quote) -> Repr<'_> {
Repr {
s,
quote,
info: OnceCell::new(),
}
pub fn repr(s: &str) -> Repr<'_> {
Repr::new(s, Quote::Single)
}
#[derive(Debug, Copy, Clone)]
@@ -423,10 +419,19 @@ pub struct Repr<'a> {
s: &'a str,
// the quote type we prefer to use
quote: Quote,
// the tuple is dquouted, out_len
info: OnceCell<Result<ReprInfo, ReprOverflowError>>,
}
impl<'a> Repr<'a> {
pub fn new(s: &'a str, quote: Quote) -> Self {
Self {
s,
quote,
info: OnceCell::new(),
}
}
}
impl Repr<'_> {
fn get_info(&self) -> Result<ReprInfo, ReprOverflowError> {
*self.info.get_or_init(|| ReprInfo::get(self.s, self.quote))

View File

@@ -249,7 +249,12 @@ impl PyBytesInner {
pub fn repr(&self, class_name: Option<&str>, vm: &VirtualMachine) -> PyResult<String> {
let repr = if let Some(class_name) = class_name {
rustpython_common::bytes::repr_with(&self.elements, &[class_name, "("], ")")
rustpython_common::bytes::repr_with(
&self.elements,
&[class_name, "("],
")",
rustpython_common::str::Quote::Single,
)
} else {
rustpython_common::bytes::repr(&self.elements)
};