mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 19:24:57 +08:00
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".
This commit is contained in:
1 parent
72a90da13b
commit
1856415196
2 files changed
+10
-13
No files matched your search
@@ -15,7 +15,6 @@ use crate::{
|
||||
builtins::PyIntRef,
|
||||
builtins::{PyDict, PyModule, PyStrRef, PyType},
|
||||
class::{PyClassImpl, StaticType},
|
||||
compiler::core::bytecode::OpArgType,
|
||||
compiler::{CompileError, ParseError},
|
||||
convert::ToPyObject,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use super::*;
|
||||
use num_traits::ToPrimitive;
|
||||
use rustpython_compiler_core::{SourceFile, bytecode};
|
||||
use rustpython_compiler_core::SourceFile;
|
||||
|
||||
impl Node for ruff::ConversionFlag {
|
||||
fn ast_to_object(self, vm: &VirtualMachine, _source_file: &SourceFile) -> PyObjectRef {
|
||||
@@ -12,16 +11,15 @@ impl Node for ruff::ConversionFlag {
|
||||
_source_file: &SourceFile,
|
||||
object: PyObjectRef,
|
||||
) -> PyResult<Self> {
|
||||
i32::try_from_object(vm, object)?
|
||||
.to_u32()
|
||||
.and_then(bytecode::ConvertValueOparg::from_op_arg)
|
||||
.map(|flag| match flag {
|
||||
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"))
|
||||
// Python's AST uses ASCII codes: 's', 'r', 'a', -1=None
|
||||
// Note: 255 is -1i8 as u8 (ruff's ConversionFlag::None)
|
||||
match i32::try_from_object(vm, object)? {
|
||||
-1 | 255 => Ok(Self::None),
|
||||
x if x == b's' as i32 => Ok(Self::Str),
|
||||
x if x == b'r' as i32 => Ok(Self::Repr),
|
||||
x if x == b'a' as i32 => Ok(Self::Ascii),
|
||||
_ => Err(vm.new_value_error("invalid conversion flag")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user