Skip to content

Commit

Permalink
Fix dis.dis without compiler feature
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Aug 7, 2024
1 parent 8673169 commit a2df2f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
2 changes: 2 additions & 0 deletions stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
18 changes: 13 additions & 5 deletions stdlib/src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ mod decl {
use crate::vm::{
builtins::{PyCode, PyDictRef, PyStrRef},
bytecode::CodeFlags,
compiler, PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
PyObjectRef, PyRef, PyResult, TryFromObject, VirtualMachine,
};

#[pyfunction]
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
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, "<dis>".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,
"<dis>".to_owned(),
)
.map_err(|err| vm.new_syntax_error(&err, Some(_co_str.as_str())))?
} else {
PyRef::try_from_object(vm, obj)?
};
Expand Down

0 comments on commit a2df2f0

Please sign in to comment.