Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Align f-string related bytecodes with 3.13
  • Loading branch information
ShaharNaveh committed Dec 2, 2025
commit e04b88d1fcf55ff5dc631423aa158f1fd0c79578
74 changes: 41 additions & 33 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ use malachite_bigint::BigInt;
use num_complex::Complex;
use num_traits::{Num, ToPrimitive};
use ruff_python_ast::{
Alias, Arguments, BoolOp, CmpOp, Comprehension, ConversionFlag, DebugText, Decorator, DictItem,
ExceptHandler, ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext,
ExprFString, ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp,
FString, FStringFlags, FStringPart, Identifier, Int, InterpolatedElement,
InterpolatedStringElement, InterpolatedStringElements, Keyword, MatchCase, ModExpression,
ModModule, Operator, Parameters, Pattern, PatternMatchAs, PatternMatchClass,
PatternMatchMapping, PatternMatchOr, PatternMatchSequence, PatternMatchSingleton,
PatternMatchStar, PatternMatchValue, Singleton, Stmt, StmtExpr, TypeParam, TypeParamParamSpec,
TypeParamTypeVar, TypeParamTypeVarTuple, TypeParams, UnaryOp, WithItem,
Alias, Arguments, BoolOp, CmpOp, Comprehension, DebugText, Decorator, DictItem, ExceptHandler,
ExceptHandlerExceptHandler, Expr, ExprAttribute, ExprBoolOp, ExprContext, ExprFString,
ExprList, ExprName, ExprSlice, ExprStarred, ExprSubscript, ExprTuple, ExprUnaryOp, FString,
FStringFlags, FStringPart, Identifier, Int, InterpolatedElement, InterpolatedStringElement,
InterpolatedStringElements, Keyword, MatchCase, ModExpression, ModModule, Operator, Parameters,
Pattern, PatternMatchAs, PatternMatchClass, PatternMatchMapping, PatternMatchOr,
PatternMatchSequence, PatternMatchSingleton, PatternMatchStar, PatternMatchValue, Singleton,
Stmt, StmtExpr, TypeParam, TypeParamParamSpec, TypeParamTypeVar, TypeParamTypeVarTuple,
TypeParams, UnaryOp, WithItem,
};
use ruff_text_size::{Ranged, TextRange};
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, ConversionFlag, 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 {
ruff_python_ast::ConversionFlag::None => ConversionFlag::None,
ruff_python_ast::ConversionFlag::Str => ConversionFlag::Str,
ruff_python_ast::ConversionFlag::Repr => ConversionFlag::Repr,
ruff_python_ast::ConversionFlag::Ascii => ConversionFlag::Ascii,
};

if let Some(DebugText { leading, trailing }) = &fstring_expr.debug_text {
let range = fstring_expr.expression.range();
Expand All @@ -5645,35 +5651,37 @@ 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),
(ConversionFlag::None, None)
) {
conversion = ConversionFlag::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 {
ConversionFlag::None => {}
ConversionFlag::Str | ConversionFlag::Repr | ConversionFlag::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
112 changes: 92 additions & 20 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
/// No conversion flag.
///
/// ```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 ConversionFlag {
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 @@ -479,18 +517,18 @@ impl fmt::Display for Label {
impl OpArgType for ConversionFlag {
#[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<ConversionFlag>,
},
/// 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
25 changes: 19 additions & 6 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,7 +2252,7 @@ impl ExecutingFrame<'_> {
Err(vm.new_value_error(msg))
}

fn format_value(
fn convert_value(
&mut self,
conversion: bytecode::ConversionFlag,
vm: &VirtualMachine,
Expand All @@ -2251,9 +2266,7 @@ impl ExecutingFrame<'_> {
ConversionFlag::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
Loading