From a2df2f014b9d9a8356f9e265c5e5d5c24c97d761 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 7 Aug 2024 08:56:36 +0900 Subject: [PATCH] Fix dis.dis without compiler feature --- Cargo.toml | 2 +- stdlib/Cargo.toml | 2 ++ stdlib/src/dis.rs | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 32070587f..284567575 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,7 +105,7 @@ ssl-vendor = ["ssl", "rustpython-stdlib/ssl-vendor"] [dependencies] rustpython-compiler = { workspace = true } rustpython-pylib = { workspace = true, optional = true } -rustpython-stdlib = { workspace = true, optional = true } +rustpython-stdlib = { workspace = true, optional = true, features = ["compiler"] } rustpython-vm = { workspace = true, features = ["compiler"] } rustpython-parser = { workspace = true } diff --git a/stdlib/Cargo.toml b/stdlib/Cargo.toml index 718a80426..4872b2113 100644 --- a/stdlib/Cargo.toml +++ b/stdlib/Cargo.toml @@ -10,6 +10,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] +default = ["compiler"] +compiler = ["rustpython-vm/compiler"] threading = ["rustpython-common/threading", "rustpython-vm/threading"] zlib = ["libz-sys", "flate2/zlib"] bz2 = ["bzip2"] diff --git a/stdlib/src/dis.rs b/stdlib/src/dis.rs index 9ac6245a2..d9322d487 100644 --- a/stdlib/src/dis.rs +++ b/stdlib/src/dis.rs @@ -5,7 +5,7 @@ mod decl { use crate::vm::{ builtins::{PyCode, PyDictRef, PyStrRef}, bytecode::CodeFlags, - compiler, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine, + PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine, }; #[pyfunction] @@ -13,10 +13,18 @@ mod decl { let co = if let Ok(co) = obj.get_attr("__code__", vm) { // Method or function: PyRef::try_from_object(vm, co)? - } else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) { - // String: - vm.compile(co_str.as_str(), compiler::Mode::Exec, "".to_owned()) - .map_err(|err| vm.new_syntax_error(&err, Some(co_str.as_str())))? + } else if let Ok(_co_str) = PyStrRef::try_from_object(vm, obj.clone()) { + #[cfg(not(feature = "compiler"))] + return Err(vm.new_runtime_error( + "dis.dis() with str argument requires `compiler` feature".to_owned(), + )); + #[cfg(feature = "compiler")] + vm.compile( + _co_str.as_str(), + crate::vm::compiler::Mode::Exec, + "".to_owned(), + ) + .map_err(|err| vm.new_syntax_error(&err, Some(_co_str.as_str())))? } else { PyRef::try_from_object(vm, obj)? };