Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ use rustpython_compiler_core::{
Mode, OneIndexed, PositionEncoding, SourceFile, SourceLocation,
bytecode::{
self, Arg as OpArgMarker, BinaryOperator, BuildSliceArgCount, CodeObject,
ComparisonOperator, ConstantData, Instruction, Invert, OpArg, OpArgType, UnpackExArgs,
ComparisonOperator, ConstantData, ConvertValueOparg, Instruction, Invert, OpArg, OpArgType,
UnpackExArgs,
},
};
use rustpython_wtf8::Wtf8Buf;
Expand Down Expand Up @@ -5636,7 +5637,12 @@ impl Compiler {
}
}
InterpolatedStringElement::Interpolation(fstring_expr) => {
let mut conversion = fstring_expr.conversion;
let mut conversion = match fstring_expr.conversion {
ConversionFlag::None => ConvertValueOparg::None,
ConversionFlag::Str => ConvertValueOparg::Str,
ConversionFlag::Repr => ConvertValueOparg::Repr,
ConversionFlag::Ascii => ConvertValueOparg::Ascii,
};

if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text {
let range = fstring_expr.expression.range();
Expand All @@ -5645,35 +5651,39 @@ impl Compiler {

self.emit_load_const(ConstantData::Str { value: text.into() });
element_count += 1;

// Match CPython behavior: If debug text is present, apply repr conversion.
// if no `format_spec` specified.
// See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456
if matches!(
(conversion, &fstring_expr.format_spec),
(ConvertValueOparg::None, None)
) {
conversion = ConvertValueOparg::Repr;
}
}

match &fstring_expr.format_spec {
None => {
self.emit_load_const(ConstantData::Str {
value: Wtf8Buf::new(),
});
// Match CPython behavior: If debug text is present, apply repr conversion.
// See: https://github.com/python/cpython/blob/f61afca262d3a0aa6a8a501db0b1936c60858e35/Parser/action_helpers.c#L1456
if conversion == ConversionFlag::None
&& fstring_expr.debug_text.is_some()
{
conversion = ConversionFlag::Repr;
}
self.compile_expression(&fstring_expr.expression)?;

match conversion {
ConvertValueOparg::None => {}
ConvertValueOparg::Str
| ConvertValueOparg::Repr
| ConvertValueOparg::Ascii => {
emit!(self, Instruction::ConvertValue { oparg: conversion })
}
}

match &fstring_expr.format_spec {
Some(format_spec) => {
self.compile_fstring_elements(flags, &format_spec.elements)?;

emit!(self, Instruction::FormatWithSpec);
}
None => {
emit!(self, Instruction::FormatSimple);
}
}

self.compile_expression(&fstring_expr.expression)?;

let conversion = match conversion {
ConversionFlag::None => bytecode::ConversionFlag::None,
ConversionFlag::Str => bytecode::ConversionFlag::Str,
ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii,
ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
};
emit!(self, Instruction::FormatValue { conversion });
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 94 additions & 22 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,56 @@ use num_complex::Complex64;
use rustpython_wtf8::{Wtf8, Wtf8Buf};
use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, num::NonZeroU8, ops::Deref};

/// Oparg values for [`Instruction::ConvertValue`].
///
/// ## See also
///
/// - [CPython FVC_* flags](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Include/ceval.h#L129-L132)
#[repr(u8)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(i8)]
#[allow(clippy::cast_possible_wrap)]
pub enum ConversionFlag {
/// No conversion
None = -1, // CPython uses -1
pub enum ConvertValueOparg {
/// No conversion.
///
/// ```python
/// f"{x}"
/// f"{x:4}"
/// ```
None = 0,
/// Converts by calling `str(<value>)`.
Str = b's' as i8,
/// Converts by calling `ascii(<value>)`.
Ascii = b'a' as i8,
///
/// ```python
/// f"{x!s}"
/// f"{x!s:2}"
/// ```
Str = 1,
/// Converts by calling `repr(<value>)`.
Repr = b'r' as i8,
///
/// ```python
/// f"{x!r}"
/// f"{x!r:2}"
/// ```
Repr = 2,
/// Converts by calling `ascii(<value>)`.
///
/// ```python
/// f"{x!a}"
/// f"{x!a:2}"
/// ```
Ascii = 3,
}

impl fmt::Display for ConvertValueOparg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let out = match self {
Self::Str => "1 (str)",
Self::Repr => "2 (repr)",
Self::Ascii => "3 (ascii)",
// We should never reach this. `FVC_NONE` are being handled by `Instruction::FormatSimple`
Self::None => "",
};

write!(f, "{out}")
}
}

/// Resume type for the RESUME instruction
Expand Down Expand Up @@ -476,21 +514,21 @@ impl fmt::Display for Label {
}
}

impl OpArgType for ConversionFlag {
impl OpArgType for ConvertValueOparg {
#[inline]
fn from_op_arg(x: u32) -> Option<Self> {
match x as u8 {
b's' => Some(Self::Str),
b'a' => Some(Self::Ascii),
b'r' => Some(Self::Repr),
std::u8::MAX => Some(Self::None),
_ => None,
}
Some(match x {
0 => Self::None,
1 => Self::Str,
2 => Self::Repr,
3 => Self::Ascii,
_ => return None,
})
}

#[inline]
fn to_op_arg(self) -> u32 {
self as i8 as u8 as u32
self as u32
}
}

Expand Down Expand Up @@ -777,9 +815,39 @@ pub enum Instruction {
UnpackEx {
args: Arg<UnpackExArgs>,
},
FormatValue {
conversion: Arg<ConversionFlag>,
/// Convert value to a string, depending on `oparg`:
///
/// ```python
/// value = STACK.pop()
/// result = func(value)
/// STACK.append(result)
/// ```
///
/// Used for implementing formatted string literals (f-strings).
ConvertValue {
oparg: Arg<ConvertValueOparg>,
},
/// Formats the value on top of stack:
///
/// ```python
/// value = STACK.pop()
/// result = value.__format__("")
/// STACK.append(result)
/// ```
///
/// Used for implementing formatted string literals (f-strings).
FormatSimple,
/// Formats the given value with the given format spec:
///
/// ```python
/// spec = STACK.pop()
/// value = STACK.pop()
/// result = value.__format__(spec)
/// STACK.append(result)
/// ```
///
/// Used for implementing formatted string literals (f-strings).
FormatWithSpec,
PopException,
Reverse {
amount: Arg<u32>,
Expand Down Expand Up @@ -1656,6 +1724,9 @@ impl Instruction {
CallMethodKeyword { nargs } => -1 - (nargs.get(arg) as i32) - 3 + 1,
CallFunctionEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 1 + 1,
CallMethodEx { has_kwargs } => -1 - (has_kwargs.get(arg) as i32) - 3 + 1,
ConvertValue { .. } => 0,
FormatSimple => 0,
FormatWithSpec => -1,
LoadMethod { .. } => -1 + 3,
ForIter { .. } => {
if jump {
Expand Down Expand Up @@ -1709,7 +1780,6 @@ impl Instruction {
let UnpackExArgs { before, after } = args.get(arg);
-1 + before as i32 + 1 + after as i32
}
FormatValue { .. } => -1,
PopException => 0,
Reverse { .. } => 0,
GetAwaitable => 0,
Expand Down Expand Up @@ -1891,10 +1961,12 @@ impl Instruction {
SetAdd { i } => w!(SetAdd, i),
MapAdd { i } => w!(MapAdd, i),
PrintExpr => w!(PrintExpr),
ConvertValue { oparg } => write!(f, "{:pad$}{}", "CONVERT_VALUE", oparg.get(arg)),
FormatSimple => w!(FORMAT_SIMPLE),
FormatWithSpec => w!(FORMAT_WITH_SPEC),
LoadBuildClass => w!(LoadBuildClass),
UnpackSequence { size } => w!(UnpackSequence, size),
UnpackEx { args } => w!(UnpackEx, args),
FormatValue { conversion } => w!(FormatValue, ?conversion),
PopException => w!(PopException),
Reverse { amount } => w!(Reverse, amount),
GetAwaitable => w!(GetAwaitable),
Expand Down
37 changes: 25 additions & 12 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,8 +1308,23 @@ impl ExecutingFrame<'_> {
let args = args.get(arg);
self.execute_unpack_ex(vm, args.before, args.after)
}
bytecode::Instruction::FormatValue { conversion } => {
self.format_value(conversion.get(arg), vm)
bytecode::Instruction::ConvertValue { oparg: conversion } => {
self.convert_value(conversion.get(arg), vm)
}
bytecode::Instruction::FormatSimple => {
let value = self.pop_value();
let formatted = vm.format(&value, vm.ctx.new_str(""))?;
self.push_value(formatted.into());

Ok(None)
}
bytecode::Instruction::FormatWithSpec => {
let spec = self.pop_value();
let value = self.pop_value();
let formatted = vm.format(&value, spec.downcast::<PyStr>().unwrap())?;
self.push_value(formatted.into());

Ok(None)
}
bytecode::Instruction::PopException => {
let block = self.pop_block();
Expand Down Expand Up @@ -2237,23 +2252,21 @@ impl ExecutingFrame<'_> {
Err(vm.new_value_error(msg))
}

fn format_value(
fn convert_value(
&mut self,
conversion: bytecode::ConversionFlag,
conversion: bytecode::ConvertValueOparg,
vm: &VirtualMachine,
) -> FrameResult {
use bytecode::ConversionFlag;
use bytecode::ConvertValueOparg;
let value = self.pop_value();
let value = match conversion {
ConversionFlag::Str => value.str(vm)?.into(),
ConversionFlag::Repr => value.repr(vm)?.into(),
ConversionFlag::Ascii => vm.ctx.new_str(builtins::ascii(value, vm)?).into(),
ConversionFlag::None => value,
ConvertValueOparg::Str => value.str(vm)?.into(),
ConvertValueOparg::Repr => value.repr(vm)?.into(),
ConvertValueOparg::Ascii => vm.ctx.new_str(builtins::ascii(value, vm)?).into(),
ConvertValueOparg::None => value,
};

let spec = self.pop_value();
let formatted = vm.format(&value, spec.downcast::<PyStr>().unwrap())?;
self.push_value(formatted.into());
self.push_value(value);
Ok(None)
}

Expand Down
10 changes: 5 additions & 5 deletions crates/vm/src/stdlib/ast/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ impl Node for ruff::ConversionFlag {
) -> PyResult<Self> {
i32::try_from_object(vm, object)?
.to_u32()
.and_then(bytecode::ConversionFlag::from_op_arg)
.and_then(bytecode::ConvertValueOparg::from_op_arg)
.map(|flag| match flag {
bytecode::ConversionFlag::None => Self::None,
bytecode::ConversionFlag::Str => Self::Str,
bytecode::ConversionFlag::Ascii => Self::Ascii,
bytecode::ConversionFlag::Repr => Self::Repr,
bytecode::ConvertValueOparg::None => Self::None,
bytecode::ConvertValueOparg::Str => Self::Str,
bytecode::ConvertValueOparg::Repr => Self::Repr,
bytecode::ConvertValueOparg::Ascii => Self::Ascii,
})
.ok_or_else(|| vm.new_value_error("invalid conversion flag"))
}
Expand Down
Loading