Skip to content

Commit 310f89e

Browse files
committed
Remove bytecode compilation. Add feature to enable verbose logging in vm.
1 parent cc24b73 commit 310f89e

File tree

9 files changed

+29
-92
lines changed

9 files changed

+29
-92
lines changed

tests/compile_code.py

-60
This file was deleted.

tests/test_snippets.py

-19
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from pathlib import Path
1414
import shutil
1515

16-
import compile_code
17-
1816

1917
class _TestType(enum.Enum):
2018
functional = 1
@@ -40,8 +38,6 @@ def perform_test(filename, method, test_type):
4038
logger.info("Running %s via %s", filename, method)
4139
if method == "cpython":
4240
run_via_cpython(filename)
43-
elif method == "cpython_bytecode":
44-
run_via_cpython_bytecode(filename, test_type)
4541
elif method == "rustpython":
4642
run_via_rustpython(filename, test_type)
4743
else:
@@ -54,20 +50,6 @@ def run_via_cpython(filename):
5450
subprocess.check_call([sys.executable, filename], env=env)
5551

5652

57-
def run_via_cpython_bytecode(filename, test_type):
58-
# Step1: Create bytecode file:
59-
bytecode_filename = filename + ".bytecode"
60-
with open(bytecode_filename, "w") as f:
61-
compile_code.compile_to_bytecode(filename, out_file=f)
62-
63-
# Step2: run cpython bytecode:
64-
env = os.environ.copy()
65-
env["RUST_LOG"] = "info,cargo=error,jobserver=error"
66-
env["RUST_BACKTRACE"] = "1"
67-
with pushd(CPYTHON_RUNNER_DIR):
68-
subprocess.check_call(["cargo", "run", bytecode_filename], env=env)
69-
70-
7153
def run_via_rustpython(filename, test_type):
7254
env = os.environ.copy()
7355
env['RUST_LOG'] = 'info,cargo=error,jobserver=error'
@@ -142,7 +124,6 @@ def generate_slices(path):
142124

143125

144126
@populate("cpython")
145-
# @populate('cpython_bytecode')
146127
@populate("rustpython")
147128
class SampleTestCase(unittest.TestCase):
148129
@classmethod

vm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2018"
99

1010
[features]
1111
default = ["rustpython-parser", "rustpython-compiler"]
12+
vm-tracing-logging = []
1213

1314
[dependencies]
1415
# Crypto:

vm/src/frame.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl Frame {
295295
let traceback = vm
296296
.get_attribute(exception.clone(), "__traceback__")
297297
.unwrap();
298-
trace!("Adding to traceback: {:?} {:?}", traceback, lineno);
298+
vm_trace!("Adding to traceback: {:?} {:?}", traceback, lineno);
299299
let raise_location = vm.ctx.new_tuple(vec![
300300
vm.ctx.new_str(filename.clone()),
301301
vm.ctx.new_int(lineno.row()),
@@ -335,6 +335,8 @@ impl Frame {
335335
/// Execute a single instruction.
336336
fn execute_instruction(&self, vm: &VirtualMachine) -> FrameResult {
337337
let instruction = self.fetch_instruction();
338+
339+
#[cfg(feature = "vm-tracing-logging")]
338340
{
339341
trace!("=======");
340342
/* TODO:
@@ -1142,6 +1144,7 @@ impl Frame {
11421144

11431145
fn jump(&self, label: bytecode::Label) {
11441146
let target_pc = self.code.label_map[&label];
1147+
#[cfg(feature = "vm-tracing-logging")]
11451148
trace!("jump from {:?} to {:?}", self.lasti, target_pc);
11461149
*self.lasti.borrow_mut() = target_pc;
11471150
}

vm/src/macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ macro_rules! match_class {
256256
}
257257
};
258258
}
259+
260+
261+
/// Super detailed logging. Might soon overflow your logbuffers
262+
/// Default, this logging is discarded, except when a the `vm-tracing-logging`
263+
/// build feature is enabled.
264+
macro_rules! vm_trace {
265+
($($arg:tt)+) => {
266+
#[cfg(feature = "vm-tracing-logging")]
267+
trace!($($arg)+);
268+
}
269+
}

vm/src/obj/objnone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl PyNoneRef {
4343
}
4444

4545
fn get_attribute(self, name: PyStringRef, vm: &VirtualMachine) -> PyResult {
46-
trace!("None.__getattribute__({:?}, {:?})", self, name);
46+
vm_trace!("None.__getattribute__({:?}, {:?})", self, name);
4747
let cls = self.class();
4848

4949
// Properties use a comparision with None to determine if they are either invoked by am

vm/src/obj/objobject.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn object_setattr(
6666
value: PyObjectRef,
6767
vm: &VirtualMachine,
6868
) -> PyResult<()> {
69-
trace!("object.__setattr__({:?}, {}, {:?})", obj, attr_name, value);
69+
vm_trace!("object.__setattr__({:?}, {}, {:?})", obj, attr_name, value);
7070
let cls = obj.class();
7171

7272
if let Some(attr) = objtype::class_get_attr(&cls, &attr_name.value) {
@@ -220,7 +220,7 @@ fn object_dict_setter(
220220

221221
fn object_getattribute(obj: PyObjectRef, name_str: PyStringRef, vm: &VirtualMachine) -> PyResult {
222222
let name = &name_str.value;
223-
trace!("object.__getattribute__({:?}, {:?})", obj, name);
223+
vm_trace!("object.__getattribute__({:?}, {:?})", obj, name);
224224
let cls = obj.class();
225225

226226
if let Some(attr) = objtype::class_get_attr(&cls, &name) {

vm/src/obj/objtype.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl PyClassRef {
114114

115115
fn getattribute(self, name_ref: PyStringRef, vm: &VirtualMachine) -> PyResult {
116116
let name = &name_ref.value;
117-
trace!("type.__getattribute__({:?}, {:?})", self, name);
117+
vm_trace!("type.__getattribute__({:?}, {:?})", self, name);
118118
let mcl = self.class();
119119

120120
if let Some(attr) = class_get_attr(&mcl, &name) {
@@ -241,7 +241,7 @@ pub fn issubclass(subclass: &PyClassRef, cls: &PyClassRef) -> bool {
241241
}
242242

243243
pub fn type_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
244-
debug!("type.__new__ {:?}", args);
244+
vm_trace!("type.__new__ {:?}", args);
245245
if args.args.len() == 2 {
246246
Ok(args.args[1].class().into_object())
247247
} else if args.args.len() == 4 {
@@ -265,7 +265,7 @@ pub fn type_new_class(
265265
}
266266

267267
pub fn type_call(class: PyClassRef, args: Args, kwargs: KwArgs, vm: &VirtualMachine) -> PyResult {
268-
debug!("type_call: {:?}", class);
268+
vm_trace!("type_call: {:?}", class);
269269
let new = class_get_attr(&class, "__new__").expect("All types should have a __new__.");
270270
let new_wrapped = vm.call_get_descriptor(new, class.into_object())?;
271271
let obj = vm.invoke(new_wrapped, (&args, &kwargs))?;
@@ -353,7 +353,7 @@ fn take_next_base(mut bases: Vec<Vec<PyClassRef>>) -> Option<(PyClassRef, Vec<Ve
353353
}
354354

355355
fn linearise_mro(mut bases: Vec<Vec<PyClassRef>>) -> Option<Vec<PyClassRef>> {
356-
debug!("Linearising MRO: {:?}", bases);
356+
vm_trace!("Linearising MRO: {:?}", bases);
357357
let mut result = vec![];
358358
loop {
359359
if (&bases).iter().all(Vec::is_empty) {

vm/src/vm.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl VirtualMachine {
170170

171171
fn new_exception_obj(&self, exc_type: PyClassRef, args: Vec<PyObjectRef>) -> PyResult {
172172
// TODO: add repr of args into logging?
173-
info!("New exception created: {}", exc_type.name);
173+
vm_trace!("New exception created: {}", exc_type.name);
174174
self.invoke(exc_type.into_object(), args)
175175
}
176176

@@ -377,7 +377,7 @@ impl VirtualMachine {
377377
let cls = obj.class();
378378
match objtype::class_get_attr(&cls, method_name) {
379379
Some(func) => {
380-
trace!(
380+
vm_trace!(
381381
"vm.call_method {:?} {:?} {:?} -> {:?}",
382382
obj,
383383
cls,
@@ -392,7 +392,8 @@ impl VirtualMachine {
392392
}
393393

394394
fn _invoke(&self, func_ref: PyObjectRef, args: PyFuncArgs) -> PyResult {
395-
trace!("Invoke: {:?} {:?}", func_ref, args);
395+
vm_trace!("Invoke: {:?} {:?}", func_ref, args);
396+
396397
if let Some(PyFunction {
397398
ref code,
398399
ref scope,
@@ -411,7 +412,7 @@ impl VirtualMachine {
411412
value(self, args)
412413
} else {
413414
// TODO: is it safe to just invoke __call__ otherwise?
414-
trace!("invoke __call__ for: {:?}", &func_ref.payload);
415+
vm_trace!("invoke __call__ for: {:?}", &func_ref.payload);
415416
self.call_method(&func_ref, "__call__", args)
416417
}
417418
}
@@ -630,7 +631,7 @@ impl VirtualMachine {
630631
T: TryIntoRef<PyString>,
631632
{
632633
let attr_name = attr_name.try_into_ref(self)?;
633-
trace!("vm.__getattribute__: {:?} {:?}", obj, attr_name);
634+
vm_trace!("vm.__getattribute__: {:?} {:?}", obj, attr_name);
634635
self.call_method(&obj, "__getattribute__", vec![attr_name.into_object()])
635636
}
636637

0 commit comments

Comments
 (0)