forked from Rust-related/RustPython
Merge pull request #1786 from palaviv/more-tests
Add more CPython tests
This commit is contained in:
@@ -8,7 +8,7 @@ import os
|
||||
import os.path
|
||||
import subprocess
|
||||
import py_compile
|
||||
import zipfile
|
||||
# import zipfile XXX RustPython
|
||||
|
||||
from importlib.util import source_from_cache
|
||||
from test.support import make_legacy_pyc, strip_python_stderr
|
||||
|
||||
2044
Lib/test/test_builtin.py
Normal file
2044
Lib/test/test_builtin.py
Normal file
File diff suppressed because it is too large
Load Diff
1410
Lib/test/test_exceptions.py
Normal file
1410
Lib/test/test_exceptions.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,13 +4,14 @@ use crate::frame::{ExecutionResult, FrameRef};
|
||||
use crate::pyobject::{PyObjectRef, PyResult};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Coro {
|
||||
frame: FrameRef,
|
||||
closed: Cell<bool>,
|
||||
running: Cell<bool>,
|
||||
exceptions: RefCell<Vec<PyBaseExceptionRef>>,
|
||||
}
|
||||
|
||||
impl Coro {
|
||||
@@ -19,6 +20,7 @@ impl Coro {
|
||||
frame,
|
||||
closed: Cell::new(false),
|
||||
running: Cell::new(false),
|
||||
exceptions: RefCell::new(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,15 +31,32 @@ impl Coro {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_with_context<F>(&self, func: F, vm: &VirtualMachine) -> PyResult<ExecutionResult>
|
||||
where
|
||||
F: FnOnce() -> PyResult<ExecutionResult>,
|
||||
{
|
||||
self.running.set(true);
|
||||
let curr_exception_stack_len = vm.exceptions.borrow().len();
|
||||
vm.exceptions
|
||||
.borrow_mut()
|
||||
.append(&mut self.exceptions.borrow_mut());
|
||||
let result = func();
|
||||
self.exceptions.replace(
|
||||
vm.exceptions
|
||||
.borrow_mut()
|
||||
.split_off(curr_exception_stack_len),
|
||||
);
|
||||
self.running.set(false);
|
||||
result
|
||||
}
|
||||
|
||||
pub fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
if self.closed.get() {
|
||||
return Err(objiter::new_stop_iteration(vm));
|
||||
}
|
||||
|
||||
self.frame.push_value(value.clone());
|
||||
self.running.set(true);
|
||||
let result = vm.run_frame(self.frame.clone());
|
||||
self.running.set(false);
|
||||
let result = self.run_with_context(|| vm.run_frame(self.frame.clone()), vm);
|
||||
self.maybe_close(&result);
|
||||
result?.into_result(vm)
|
||||
}
|
||||
@@ -53,9 +72,8 @@ impl Coro {
|
||||
return Err(exceptions::normalize(exc_type, exc_val, exc_tb, vm)?);
|
||||
}
|
||||
vm.frames.borrow_mut().push(self.frame.clone());
|
||||
self.running.set(true);
|
||||
let result = self.frame.gen_throw(vm, exc_type, exc_val, exc_tb);
|
||||
self.running.set(false);
|
||||
let result =
|
||||
self.run_with_context(|| self.frame.gen_throw(vm, exc_type, exc_val, exc_tb), vm);
|
||||
self.maybe_close(&result);
|
||||
vm.frames.borrow_mut().pop();
|
||||
result?.into_result(vm)
|
||||
@@ -66,14 +84,17 @@ impl Coro {
|
||||
return Ok(());
|
||||
}
|
||||
vm.frames.borrow_mut().push(self.frame.clone());
|
||||
self.running.set(true);
|
||||
let result = self.frame.gen_throw(
|
||||
let result = self.run_with_context(
|
||||
|| {
|
||||
self.frame.gen_throw(
|
||||
vm,
|
||||
vm.ctx.exceptions.generator_exit.clone().into_object(),
|
||||
vm.get_none(),
|
||||
vm.get_none(),
|
||||
)
|
||||
},
|
||||
vm,
|
||||
vm.ctx.exceptions.generator_exit.clone().into_object(),
|
||||
vm.get_none(),
|
||||
vm.get_none(),
|
||||
);
|
||||
self.running.set(false);
|
||||
vm.frames.borrow_mut().pop();
|
||||
self.closed.set(true);
|
||||
match result {
|
||||
|
||||
@@ -413,18 +413,14 @@ fn os_rmdir(path: PyStringRef, dir_fd: DirFd, vm: &VirtualMachine) -> PyResult<(
|
||||
}
|
||||
|
||||
fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult {
|
||||
match fs::read_dir(path.as_str()) {
|
||||
Ok(iter) => {
|
||||
let res: PyResult<Vec<PyObjectRef>> = iter
|
||||
.map(|entry| match entry {
|
||||
Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())),
|
||||
Err(s) => Err(convert_io_error(vm, s)),
|
||||
})
|
||||
.collect();
|
||||
Ok(vm.ctx.new_list(res?))
|
||||
}
|
||||
Err(s) => Err(vm.new_os_error(s.to_string())),
|
||||
}
|
||||
let res: PyResult<Vec<PyObjectRef>> = fs::read_dir(path.as_str())
|
||||
.map_err(|err| convert_io_error(vm, err))?
|
||||
.map(|entry| match entry {
|
||||
Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())),
|
||||
Err(s) => Err(convert_io_error(vm, s)),
|
||||
})
|
||||
.collect();
|
||||
Ok(vm.ctx.new_list(res?))
|
||||
}
|
||||
|
||||
fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsStr> {
|
||||
|
||||
Reference in New Issue
Block a user