Skip to content

Commit ae63092

Browse files
committed
Fix f-string conversion flag ValueError when compiling AST (#6533)
The ast_from_object for ConversionFlag was using bytecode::ConvertValueOparg::from_op_arg() which only accepts internal oparg values (0, 1, 2, 3, 255), but Python's AST uses ASCII codes ('s'=115, 'r'=114, 'a'=97, -1=None). This caused compile() on parsed AST with conversion flags to fail with "invalid conversion flag".
1 parent c9bf8df commit ae63092

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

crates/vm/src/stdlib/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::{
1515
builtins::PyIntRef,
1616
builtins::{PyDict, PyModule, PyStrRef, PyType},
1717
class::{PyClassImpl, StaticType},
18-
compiler::core::bytecode::OpArgType,
1918
compiler::{CompileError, ParseError},
2019
convert::ToPyObject,
2120
};

crates/vm/src/stdlib/ast/other.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
2-
use num_traits::ToPrimitive;
3-
use rustpython_compiler_core::{SourceFile, bytecode};
2+
use rustpython_compiler_core::SourceFile;
43

54
impl Node for ruff::ConversionFlag {
65
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
@@ -12,16 +11,15 @@ impl Node for ruff::ConversionFlag {
1211
_source_file: &SourceFile,
1312
object: PyObjectRef,
1413
) -> PyResult<Self> {
15-
i32::try_from_object(vm, object)?
16-
.to_u32()
17-
.and_then(bytecode::ConvertValueOparg::from_op_arg)
18-
.map(|flag| match flag {
19-
bytecode::ConvertValueOparg::None => Self::None,
20-
bytecode::ConvertValueOparg::Str => Self::Str,
21-
bytecode::ConvertValueOparg::Repr => Self::Repr,
22-
bytecode::ConvertValueOparg::Ascii => Self::Ascii,
23-
})
24-
.ok_or_else(|| vm.new_value_error("invalid conversion flag"))
14+
// Python's AST uses ASCII codes: 's', 'r', 'a', -1=None
15+
// Note: 255 is -1i8 as u8 (ruff's ConversionFlag::None)
16+
match i32::try_from_object(vm, object)? {
17+
-1 | 255 => Ok(Self::None),
18+
x if x == b's' as i32 => Ok(Self::Str),
19+
x if x == b'r' as i32 => Ok(Self::Repr),
20+
x if x == b'a' as i32 => Ok(Self::Ascii),
21+
_ => Err(vm.new_value_error("invalid conversion flag")),
22+
}
2523
}
2624
}
2725

0 commit comments

Comments
 (0)