mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Accept slices to function calls
Instead of allocated types, per [0]. [0] https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#ptr_arg
This commit is contained in:
@@ -40,7 +40,7 @@ pub fn parse(filename: &Path) -> Result<ast::Program, String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_program(source: &String) -> Result<ast::Program, String> {
|
||||
pub fn parse_program(source: &str) -> Result<ast::Program, String> {
|
||||
let lxr = lexer::Lexer::new(&source);
|
||||
match python::ProgramParser::new().parse(lxr) {
|
||||
Err(lalrpop_util::ParseError::UnrecognizedToken {
|
||||
@@ -52,7 +52,7 @@ pub fn parse_program(source: &String) -> Result<ast::Program, String> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_statement(source: &String) -> Result<ast::LocatedStatement, String> {
|
||||
pub fn parse_statement(source: &str) -> Result<ast::LocatedStatement, String> {
|
||||
let lxr = lexer::Lexer::new(&source);
|
||||
match python::StatementParser::new().parse(lxr) {
|
||||
Err(why) => Err(String::from(format!("{:?}", why))),
|
||||
@@ -60,7 +60,7 @@ pub fn parse_statement(source: &String) -> Result<ast::LocatedStatement, String>
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
|
||||
pub fn parse_expression(source: &str) -> Result<ast::Expression, String> {
|
||||
let lxr = lexer::Lexer::new(&source);
|
||||
match python::ExpressionParser::new().parse(lxr) {
|
||||
Err(why) => Err(String::from(format!("{:?}", why))),
|
||||
|
||||
@@ -18,7 +18,7 @@ struct Compiler {
|
||||
|
||||
pub fn compile(
|
||||
vm: &mut VirtualMachine,
|
||||
source: &String,
|
||||
source: &str,
|
||||
mode: Mode,
|
||||
source_path: Option<String>,
|
||||
) -> Result<PyObjectRef, String> {
|
||||
@@ -115,7 +115,7 @@ impl Compiler {
|
||||
self.emit(Instruction::ReturnValue);
|
||||
}
|
||||
|
||||
fn compile_statements(&mut self, statements: &Vec<ast::LocatedStatement>) {
|
||||
fn compile_statements(&mut self, statements: &[ast::LocatedStatement]) {
|
||||
for statement in statements {
|
||||
self.compile_statement(statement)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::compile;
|
||||
use super::pyobject::{PyObjectRef, PyResult};
|
||||
use super::vm::VirtualMachine;
|
||||
|
||||
pub fn eval(vm: &mut VirtualMachine, source: &String, scope: PyObjectRef) -> PyResult {
|
||||
pub fn eval(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> PyResult {
|
||||
match compile::compile(vm, source, compile::Mode::Eval, None) {
|
||||
Ok(bytecode) => {
|
||||
debug!("Code object: {:?}", bytecode);
|
||||
|
||||
@@ -13,7 +13,7 @@ use super::compile;
|
||||
use super::pyobject::{DictProtocol, PyObjectKind, PyResult};
|
||||
use super::vm::VirtualMachine;
|
||||
|
||||
fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult {
|
||||
fn import_uncached_module(vm: &mut VirtualMachine, module: &str) -> PyResult {
|
||||
// Check for Rust-native modules
|
||||
if let Some(module) = vm.stdlib_inits.get(module) {
|
||||
return Ok(module(&vm.ctx).clone());
|
||||
@@ -53,7 +53,7 @@ fn import_uncached_module(vm: &mut VirtualMachine, module: &String) -> PyResult
|
||||
Ok(vm.ctx.new_module(module, scope))
|
||||
}
|
||||
|
||||
fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
|
||||
fn import_module(vm: &mut VirtualMachine, module_name: &str) -> PyResult {
|
||||
// First, see if we already loaded the module:
|
||||
let sys_modules = vm.sys_module.get_item("modules").unwrap();
|
||||
if let Some(module) = sys_modules.get_item(module_name) {
|
||||
@@ -64,7 +64,7 @@ fn import_module(vm: &mut VirtualMachine, module_name: &String) -> PyResult {
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<String>) -> PyResult {
|
||||
pub fn import(vm: &mut VirtualMachine, module_name: &str, symbol: &Option<String>) -> PyResult {
|
||||
let module = import_module(vm, module_name)?;
|
||||
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
|
||||
// that
|
||||
@@ -75,7 +75,7 @@ pub fn import(vm: &mut VirtualMachine, module_name: &String, symbol: &Option<Str
|
||||
Ok(obj)
|
||||
}
|
||||
|
||||
fn find_source(vm: &VirtualMachine, name: &String) -> io::Result<PathBuf> {
|
||||
fn find_source(vm: &VirtualMachine, name: &str) -> io::Result<PathBuf> {
|
||||
let sys_path = vm.sys_module.get_item("path").unwrap();
|
||||
let mut paths: Vec<PathBuf> = match sys_path.borrow().kind {
|
||||
PyObjectKind::List { ref elements } => elements
|
||||
|
||||
@@ -63,12 +63,12 @@ impl PySliceableSequence for Vec<PyObjectRef> {
|
||||
pub fn get_item(
|
||||
vm: &mut VirtualMachine,
|
||||
sequence: &PyObjectRef,
|
||||
elements: &Vec<PyObjectRef>,
|
||||
elements: &[PyObjectRef],
|
||||
subscript: PyObjectRef,
|
||||
) -> PyResult {
|
||||
match &(subscript.borrow()).kind {
|
||||
PyObjectKind::Integer { value } => {
|
||||
let pos_index = elements.get_pos(*value);
|
||||
let pos_index = elements.to_vec().get_pos(*value);
|
||||
if pos_index < elements.len() {
|
||||
let obj = elements[pos_index].clone();
|
||||
Ok(obj)
|
||||
@@ -84,10 +84,10 @@ pub fn get_item(
|
||||
} => Ok(PyObject::new(
|
||||
match &(sequence.borrow()).kind {
|
||||
PyObjectKind::Tuple { elements: _ } => PyObjectKind::Tuple {
|
||||
elements: elements.get_slice_items(&subscript),
|
||||
elements: elements.to_vec().get_slice_items(&subscript),
|
||||
},
|
||||
PyObjectKind::List { elements: _ } => PyObjectKind::List {
|
||||
elements: elements.get_slice_items(&subscript),
|
||||
elements: elements.to_vec().get_slice_items(&subscript),
|
||||
},
|
||||
ref kind => panic!("sequence get_item called for non-sequence: {:?}", kind),
|
||||
},
|
||||
|
||||
@@ -147,18 +147,18 @@ impl PySliceableSequence for String {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subscript(vm: &mut VirtualMachine, value: &String, b: PyObjectRef) -> PyResult {
|
||||
pub fn subscript(vm: &mut VirtualMachine, value: &str, b: PyObjectRef) -> PyResult {
|
||||
// let value = a
|
||||
match &(*b.borrow()).kind {
|
||||
&PyObjectKind::Integer { value: ref pos } => {
|
||||
let idx = value.get_pos(*pos);
|
||||
let idx = value.to_string().get_pos(*pos);
|
||||
Ok(vm.new_str(value[idx..idx + 1].to_string()))
|
||||
}
|
||||
&PyObjectKind::Slice {
|
||||
start: _,
|
||||
stop: _,
|
||||
step: _,
|
||||
} => Ok(vm.new_str(value.get_slice_items(&b))),
|
||||
} => Ok(vm.new_str(value.to_string().get_slice_items(&b).to_string())),
|
||||
_ => panic!(
|
||||
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
|
||||
value, b
|
||||
|
||||
@@ -273,7 +273,7 @@ impl PyContext {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_class(&self, name: &String, base: PyObjectRef) -> PyObjectRef {
|
||||
pub fn new_class(&self, name: &str, base: PyObjectRef) -> PyObjectRef {
|
||||
objtype::new(self.type_type(), name, vec![base], self.new_dict()).unwrap()
|
||||
}
|
||||
|
||||
@@ -289,10 +289,10 @@ impl PyContext {
|
||||
}.into_ref()
|
||||
}
|
||||
|
||||
pub fn new_module(&self, name: &String, scope: PyObjectRef) -> PyObjectRef {
|
||||
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
|
||||
PyObject::new(
|
||||
PyObjectKind::Module {
|
||||
name: name.clone(),
|
||||
name: name.to_string(),
|
||||
dict: scope.clone(),
|
||||
},
|
||||
self.module_type.clone(),
|
||||
|
||||
@@ -51,7 +51,7 @@ fn create_node(ctx: &PyContext, _name: &str) -> PyObjectRef {
|
||||
node
|
||||
}
|
||||
|
||||
fn statements_to_ast(ctx: &PyContext, statements: &Vec<ast::LocatedStatement>) -> PyObjectRef {
|
||||
fn statements_to_ast(ctx: &PyContext, statements: &[ast::LocatedStatement]) -> PyObjectRef {
|
||||
let mut py_statements = vec![];
|
||||
for statement in statements {
|
||||
py_statements.push(statement_to_ast(ctx, statement));
|
||||
|
||||
12
vm/src/vm.rs
12
vm/src/vm.rs
@@ -219,13 +219,13 @@ impl VirtualMachine {
|
||||
self.current_frame().last_value()
|
||||
}
|
||||
|
||||
fn store_name(&mut self, name: &String) -> Option<PyResult> {
|
||||
fn store_name(&mut self, name: &str) -> Option<PyResult> {
|
||||
let obj = self.pop_value();
|
||||
self.current_frame_mut().locals.set_item(name, obj);
|
||||
None
|
||||
}
|
||||
|
||||
fn load_name(&mut self, name: &String) -> Option<PyResult> {
|
||||
fn load_name(&mut self, name: &str) -> Option<PyResult> {
|
||||
// Lookup name in scope and put it onto the stack!
|
||||
let mut scope = self.current_frame().locals.clone();
|
||||
loop {
|
||||
@@ -628,8 +628,8 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
fn import(&mut self, module: &String, symbol: &Option<String>) -> Option<PyResult> {
|
||||
let obj = match import(self, module, symbol) {
|
||||
fn import(&mut self, module: &str, symbol: &Option<String>) -> Option<PyResult> {
|
||||
let obj = match import(self, &module.to_string(), symbol) {
|
||||
Ok(value) => value,
|
||||
Err(value) => return Some(Err(value)),
|
||||
};
|
||||
@@ -643,7 +643,7 @@ impl VirtualMachine {
|
||||
objtype::get_attribute(self, obj.clone(), attr_name)
|
||||
}
|
||||
|
||||
fn load_attr(&mut self, attr_name: &String) -> Option<PyResult> {
|
||||
fn load_attr(&mut self, attr_name: &str) -> Option<PyResult> {
|
||||
let parent = self.pop_value();
|
||||
match self.get_attribute(parent, attr_name) {
|
||||
Ok(obj) => {
|
||||
@@ -654,7 +654,7 @@ impl VirtualMachine {
|
||||
}
|
||||
}
|
||||
|
||||
fn store_attr(&mut self, attr_name: &String) -> Option<PyResult> {
|
||||
fn store_attr(&mut self, attr_name: &str) -> Option<PyResult> {
|
||||
let parent = self.pop_value();
|
||||
let value = self.pop_value();
|
||||
parent.set_attr(attr_name, value);
|
||||
|
||||
Reference in New Issue
Block a user