mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #3842 from youknowone/freebsd
fix --no-default-features build and freebsd CI
This commit is contained in:
10
.github/workflows/ci.yaml
vendored
10
.github/workflows/ci.yaml
vendored
@@ -124,6 +124,16 @@ jobs:
|
||||
command: check
|
||||
args: --target i686-unknown-linux-musl
|
||||
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
target: x86_64-unknown-freebsd
|
||||
|
||||
- name: Check compilation for freebsd
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: check
|
||||
args: --target x86_64-unknown-freebsd
|
||||
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
target: wasm32-unknown-unknown
|
||||
|
||||
@@ -18,10 +18,8 @@ mod builtins {
|
||||
int::PyIntRef,
|
||||
iter::PyCallableIterator,
|
||||
list::{PyList, SortOptions},
|
||||
PyByteArray, PyBytes, PyBytesRef, PyCode, PyDictRef, PyStr, PyStrRef, PyTuple,
|
||||
PyTupleRef, PyType,
|
||||
PyByteArray, PyBytes, PyDictRef, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType,
|
||||
},
|
||||
class::PyClassImpl,
|
||||
common::{hash::PyHash, str::to_ascii},
|
||||
format::call_object_format,
|
||||
function::Either,
|
||||
@@ -32,12 +30,11 @@ mod builtins {
|
||||
protocol::{PyIter, PyIterReturn},
|
||||
py_io,
|
||||
readline::{Readline, ReadlineResult},
|
||||
scope::Scope,
|
||||
stdlib::sys,
|
||||
types::PyComparisonOp,
|
||||
AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
|
||||
};
|
||||
use num_traits::{Signed, ToPrimitive, Zero};
|
||||
use num_traits::{Signed, ToPrimitive};
|
||||
|
||||
#[pyfunction]
|
||||
fn abs(x: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
@@ -123,7 +120,7 @@ mod builtins {
|
||||
}
|
||||
#[cfg(feature = "rustpython-ast")]
|
||||
{
|
||||
use crate::stdlib::ast;
|
||||
use crate::{class::PyClassImpl, stdlib::ast};
|
||||
|
||||
let mode_str = args.mode.as_str();
|
||||
|
||||
@@ -152,6 +149,8 @@ mod builtins {
|
||||
}
|
||||
#[cfg(feature = "rustpython-parser")]
|
||||
{
|
||||
use crate::builtins::PyBytesRef;
|
||||
use num_traits::Zero;
|
||||
use rustpython_parser::parser;
|
||||
|
||||
let source = Either::<PyStrRef, PyBytesRef>::try_from_object(vm, args.source)?;
|
||||
@@ -215,7 +214,7 @@ mod builtins {
|
||||
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
impl ScopeArgs {
|
||||
fn make_scope(self, vm: &VirtualMachine) -> PyResult<Scope> {
|
||||
fn make_scope(self, vm: &VirtualMachine) -> PyResult<crate::scope::Scope> {
|
||||
let (globals, locals) = match self.globals {
|
||||
Some(globals) => {
|
||||
if !globals.contains_key(identifier!(vm, __builtins__), vm) {
|
||||
@@ -239,7 +238,7 @@ mod builtins {
|
||||
),
|
||||
};
|
||||
|
||||
let scope = Scope::with_builtins(Some(locals), globals, vm);
|
||||
let scope = crate::scope::Scope::with_builtins(Some(locals), globals, vm);
|
||||
Ok(scope)
|
||||
}
|
||||
}
|
||||
@@ -249,7 +248,7 @@ mod builtins {
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
#[pyfunction]
|
||||
fn eval(
|
||||
source: Either<PyStrRef, PyRef<PyCode>>,
|
||||
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
|
||||
scope: ScopeArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
@@ -261,7 +260,7 @@ mod builtins {
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
#[pyfunction]
|
||||
fn exec(
|
||||
source: Either<PyStrRef, PyRef<PyCode>>,
|
||||
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
|
||||
scope: ScopeArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
@@ -271,7 +270,7 @@ mod builtins {
|
||||
#[cfg(feature = "rustpython-compiler")]
|
||||
fn run_code(
|
||||
vm: &VirtualMachine,
|
||||
source: Either<PyStrRef, PyRef<PyCode>>,
|
||||
source: Either<PyStrRef, PyRef<crate::builtins::PyCode>>,
|
||||
scope: ScopeArgs,
|
||||
mode: compile::Mode,
|
||||
func: &str,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use crate::{
|
||||
builtins::PyCode,
|
||||
builtins::{PyCode, PyDictRef},
|
||||
compile::{self, CompileError, CompileOpts},
|
||||
PyRef, VirtualMachine,
|
||||
convert::TryFromObject,
|
||||
scope::Scope,
|
||||
AsObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
|
||||
};
|
||||
|
||||
impl VirtualMachine {
|
||||
@@ -31,4 +33,88 @@ impl VirtualMachine {
|
||||
) -> Result<PyRef<PyCode>, CompileError> {
|
||||
compile::compile(source, mode, source_path, opts).map(|code| self.ctx.new_code(code))
|
||||
}
|
||||
|
||||
pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> {
|
||||
if get_importer(path, self)?.is_some() {
|
||||
self.insert_sys_path(self.new_pyobj(path))?;
|
||||
let runpy = self.import("runpy", None, 0)?;
|
||||
let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?;
|
||||
self.invoke(
|
||||
&run_module_as_main,
|
||||
(identifier!(self, __main__).to_owned(), false),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let dir = std::path::Path::new(path)
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
self.insert_sys_path(self.new_pyobj(dir))?;
|
||||
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(source) => {
|
||||
self.run_code_string(scope, &source, path.to_owned())?;
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Failed reading file '{}': {}", path, err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(source, crate::compile::Mode::Exec, source_path.clone())
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
scope.globals.set_item(
|
||||
identifier!(self, __file__),
|
||||
self.new_pyobj(source_path),
|
||||
self,
|
||||
)?;
|
||||
self.run_code_obj(code_obj, scope)
|
||||
}
|
||||
|
||||
pub fn run_block_expr(&self, scope: Scope, source: &str) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(
|
||||
source,
|
||||
crate::compile::Mode::BlockExpr,
|
||||
"<embedded>".to_owned(),
|
||||
)
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
self.run_code_obj(code_obj, scope)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
|
||||
let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?;
|
||||
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
|
||||
if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
|
||||
return Ok(Some(importer));
|
||||
}
|
||||
let path = vm.ctx.new_str(path);
|
||||
let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
|
||||
let mut importer = None;
|
||||
let path_hooks: Vec<PyObjectRef> = path_hooks.try_into_value(vm)?;
|
||||
for path_hook in path_hooks {
|
||||
match vm.invoke(&path_hook, (path.clone(),)) {
|
||||
Ok(imp) => {
|
||||
importer = Some(imp);
|
||||
break;
|
||||
}
|
||||
Err(e) if e.fast_isinstance(vm.ctx.exceptions.import_error) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(if let Some(imp) = importer {
|
||||
let imp = path_importer_cache.get_or_insert(vm, path.into(), || imp.clone())?;
|
||||
Some(imp)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ use crate::{
|
||||
bytecode,
|
||||
codecs::CodecsRegistry,
|
||||
common::{hash::HashSecret, lock::PyMutex, rc::PyRc},
|
||||
convert::{ToPyObject, TryFromObject},
|
||||
convert::ToPyObject,
|
||||
frame::{ExecutionResult, Frame, FrameRef},
|
||||
frozen,
|
||||
function::{ArgMapping, FuncArgs},
|
||||
@@ -726,62 +726,6 @@ impl VirtualMachine {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_script(&self, scope: Scope, path: &str) -> PyResult<()> {
|
||||
if get_importer(path, self)?.is_some() {
|
||||
self.insert_sys_path(self.new_pyobj(path))?;
|
||||
let runpy = self.import("runpy", None, 0)?;
|
||||
let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?;
|
||||
self.invoke(
|
||||
&run_module_as_main,
|
||||
(identifier!(self, __main__).to_owned(), false),
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let dir = std::path::Path::new(path)
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap();
|
||||
self.insert_sys_path(self.new_pyobj(dir))?;
|
||||
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(source) => {
|
||||
self.run_code_string(scope, &source, path.to_owned())?;
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Failed reading file '{}': {}", path, err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn run_code_string(&self, scope: Scope, source: &str, source_path: String) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(source, crate::compile::Mode::Exec, source_path.clone())
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
scope.globals.set_item(
|
||||
identifier!(self, __file__),
|
||||
self.new_pyobj(source_path),
|
||||
self,
|
||||
)?;
|
||||
self.run_code_obj(code_obj, scope)
|
||||
}
|
||||
|
||||
pub fn run_block_expr(&self, scope: Scope, source: &str) -> PyResult {
|
||||
let code_obj = self
|
||||
.compile(
|
||||
source,
|
||||
crate::compile::Mode::BlockExpr,
|
||||
"<embedded>".to_owned(),
|
||||
)
|
||||
.map_err(|err| self.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
self.run_code_obj(code_obj, scope)
|
||||
}
|
||||
|
||||
pub fn run_module(&self, module: &str) -> PyResult<()> {
|
||||
let runpy = self.import("runpy", None, 0)?;
|
||||
let run_module_as_main = runpy.get_attr("_run_module_as_main", self)?;
|
||||
@@ -790,34 +734,6 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
|
||||
let path_importer_cache = vm.sys_module.get_attr("path_importer_cache", vm)?;
|
||||
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
|
||||
if let Some(importer) = path_importer_cache.get_item_opt(path, vm)? {
|
||||
return Ok(Some(importer));
|
||||
}
|
||||
let path = vm.ctx.new_str(path);
|
||||
let path_hooks = vm.sys_module.get_attr("path_hooks", vm)?;
|
||||
let mut importer = None;
|
||||
let path_hooks: Vec<PyObjectRef> = path_hooks.try_into_value(vm)?;
|
||||
for path_hook in path_hooks {
|
||||
match vm.invoke(&path_hook, (path.clone(),)) {
|
||||
Ok(imp) => {
|
||||
importer = Some(imp);
|
||||
break;
|
||||
}
|
||||
Err(e) if e.fast_isinstance(vm.ctx.exceptions.import_error) => continue,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok(if let Some(imp) = importer {
|
||||
let imp = path_importer_cache.get_or_insert(vm, path.into(), || imp.clone())?;
|
||||
Some(imp)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
impl AsRef<Context> for VirtualMachine {
|
||||
fn as_ref(&self) -> &Context {
|
||||
&self.ctx
|
||||
|
||||
Reference in New Issue
Block a user