Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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.

144 changes: 106 additions & 38 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,76 @@ 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}")
}
}

impl OpArgType for ConvertValueOparg {
#[inline]
fn from_op_arg(x: u32) -> Option<Self> {
Some(match x {
// Ruff `ConversionFlag::None` is `-1i8`,
// when its converted to `u8` its value is `u8::MAX`
0 | 255 => Self::None,
1 => Self::Str,
2 => Self::Repr,
3 => Self::Ascii,
_ => return None,
})
}

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

/// Resume type for the RESUME instruction
Expand Down Expand Up @@ -476,24 +534,6 @@ 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,
}
}

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

op_arg_enum!(
/// The kind of Raise that occurred.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -620,6 +660,18 @@ pub enum Instruction {
Continue {
target: Arg<Label>,
},
/// 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>,
},
CopyItem {
index: Arg<u32>,
},
Expand All @@ -635,24 +687,40 @@ pub enum Instruction {
index: Arg<u32>,
},
EndAsyncFor,

/// Marker bytecode for the end of a finally sequence.
/// When this bytecode is executed, the eval loop does one of those things:
/// - Continue at a certain bytecode position
/// - Propagate the exception
/// - Return from a function
/// - Do nothing at all, just continue
EndFinally,

/// Enter a finally block, without returning, excepting, just because we are there.
EnterFinally,
ExtendedArg,
ForIter {
target: Arg<Label>,
},
FormatValue {
conversion: 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,
GetAIter,
GetANext,
GetAwaitable,
Expand Down Expand Up @@ -727,12 +795,10 @@ pub enum Instruction {
PopJumpIfTrue {
target: Arg<Label>,
},

PrintExpr,
Raise {
kind: Arg<RaiseKind>,
},

/// Resume execution (e.g., at function start, after yield, etc.)
Resume {
arg: Arg<u32>,
Expand All @@ -750,7 +816,6 @@ pub enum Instruction {
SetFunctionAttribute {
attr: Arg<MakeFunctionFlags>,
},

SetupAnnotation,
SetupAsyncWith {
end: Arg<Label>,
Expand All @@ -759,7 +824,6 @@ pub enum Instruction {
SetupExcept {
handler: Arg<Label>,
},

/// Setup a finally handler, which will be called whenever one of this events occurs:
/// - the block is popped
/// - the function returns
Expand Down Expand Up @@ -1656,6 +1720,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 +1776,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 @@ -1824,6 +1890,7 @@ impl Instruction {
CompareOperation { op } => w!(CompareOperation, ?op),
ContainsOp(inv) => w!(CONTAINS_OP, ?inv),
Continue { target } => w!(Continue, target),
ConvertValue { oparg } => write!(f, "{:pad$}{}", "CONVERT_VALUE", oparg.get(arg)),
CopyItem { index } => w!(CopyItem, index),
DeleteAttr { idx } => w!(DeleteAttr, name = idx),
DeleteDeref(idx) => w!(DeleteDeref, cell_name = idx),
Expand All @@ -1837,7 +1904,8 @@ impl Instruction {
EnterFinally => w!(EnterFinally),
ExtendedArg => w!(ExtendedArg, Arg::<u32>::marker()),
ForIter { target } => w!(ForIter, target),
FormatValue { conversion } => w!(FormatValue, ?conversion),
FormatSimple => w!(FORMAT_SIMPLE),
FormatWithSpec => w!(FORMAT_WITH_SPEC),
GetAIter => w!(GetAIter),
GetANext => w!(GetANext),
GetAwaitable => w!(GetAwaitable),
Expand Down
Loading
Loading