MCPcopy Create free account
hub / github.com/astral-sh/ruff / p_bytes_repr

Method p_bytes_repr

crates/ruff_python_codegen/src/generator.rs:185–206  ·  view source on GitHub ↗
(&mut self, s: &[u8], flags: BytesLiteralFlags)

Source from the content-addressed store, hash-verified

183 }
184
185 fn p_bytes_repr(&mut self, s: &[u8], flags: BytesLiteralFlags) {
186 // raw bytes are interpreted without escapes and should all be ascii (it's a python syntax
187 // error otherwise), but if this assumption is violated, a `Utf8Error` will be returned from
188 // `p_raw_bytes`, and we should fall back on the normal escaping behavior instead of
189 // panicking
190 if flags.prefix().is_raw() {
191 if let Ok(s) = std::str::from_utf8(s) {
192 write!(self.buffer, "{}", flags.display_contents(s))
193 .expect("Writing to a String buffer should never fail");
194 return;
195 }
196 }
197 let quote_style = self.mode.quote_style(flags);
198 let escape = AsciiEscape::with_preferred_quote(s, quote_style);
199 if let Some(len) = escape.layout().len {
200 self.buffer.reserve(len);
201 }
202 escape
203 .bytes_repr(flags.triple_quotes())
204 .write(&mut self.buffer)
205 .expect("Writing to a String buffer should never fail");
206 }
207
208 fn p_str_repr(&mut self, s: &str, flags: impl Into<AnyStringFlags>) {
209 let flags = flags.into();

Callers 1

unparse_exprMethod · 0.80

Calls 8

is_rawMethod · 0.80
expectMethod · 0.80
layoutMethod · 0.80
bytes_reprMethod · 0.80
prefixMethod · 0.45
quote_styleMethod · 0.45
writeMethod · 0.45
triple_quotesMethod · 0.45

Tested by

no test coverage detected