Skip to content

Commit b7a0b0f

Browse files
committed
Removal of ToRust trait to prevent incorrect str representation
1 parent 4be2f24 commit b7a0b0f

File tree

15 files changed

+87
-71
lines changed

15 files changed

+87
-71
lines changed

parser/src/ast.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*
2-
* Implement abstract syntax tree nodes for the python language.
3-
*/
1+
//! Implement abstract syntax tree nodes for the python language.
2+
//!
3+
//! Roughly equivalent to this: https://docs.python.org/3/library/ast.html
44
55
pub use super::lexer::Location;
66
/*
@@ -40,6 +40,7 @@ pub struct Located<T> {
4040

4141
pub type LocatedStatement = Located<Statement>;
4242

43+
/// Abstract syntax tree nodes for python statements.
4344
#[derive(Debug, PartialEq)]
4445
pub enum Statement {
4546
Break,

parser/src/lexer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//! This module takes care of lexing python source text. This means source
2+
//! code is translated into seperate tokens.
3+
14
pub use super::token::Tok;
25
use std::collections::HashMap;
36

parser/src/parser.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ pub fn parse_statement(source: &str) -> Result<ast::LocatedStatement, String> {
6969
do_lalr_parsing!(source, Statement, StartStatement)
7070
}
7171

72+
/// Parses a python expression
73+
///
74+
/// # Example
75+
/// ```
76+
/// use rustpython_parser::{parser, ast};
77+
/// let expr = parser::parse_expression("1+2").unwrap();
78+
///
79+
/// assert_eq!(ast::Expression::Binop {
80+
/// a: Box::new(ast::Expression::Number {
81+
/// value: ast::Number::Integer { value: 1 }
82+
/// }),
83+
/// op: ast::Operator::Add,
84+
/// b: Box::new(ast::Expression::Number {
85+
/// value: ast::Number::Integer { value: 2 }
86+
/// })
87+
/// },
88+
/// expr);
89+
///
90+
/// ```
7291
pub fn parse_expression(source: &str) -> Result<ast::Expression, String> {
7392
do_lalr_parsing!(source, Expression, StartExpression)
7493
}

vm/src/builtins.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Builtin function definitions.
2+
//!
3+
//! Implements functions listed here: https://docs.python.org/3/library/builtins.html
4+
15
// use std::ops::Deref;
26
use std::char;
37
use std::collections::HashMap;
@@ -463,10 +467,8 @@ fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
463467
pub fn builtin_print(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
464468
trace!("print called with {:?}", args);
465469
for a in args.args {
466-
let s = match vm.to_str(a) {
467-
Ok(v) => objstr::get_value(&v),
468-
Err(err) => return Err(err),
469-
};
470+
let v = vm.to_str(&a)?;
471+
let s = objstr::get_value(&v);
470472
print!("{}", s);
471473
}
472474
println!();

vm/src/compile.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
2-
* Take an AST and transform it into bytecode
3-
*
4-
* Inspirational code:
5-
* https://github.com/python/cpython/blob/master/Python/compile.c
6-
* https://github.com/micropython/micropython/blob/master/py/compile.c
7-
*/
1+
//!
2+
//!
3+
//! Take an AST and transform it into bytecode
4+
//!
5+
//! Inspirational code:
6+
//! https://github.com/python/cpython/blob/master/Python/compile.c
7+
//! https://github.com/micropython/micropython/blob/master/py/compile.c
88
99
use super::bytecode::{self, CallType, CodeObject, Instruction};
1010
use super::pyobject::{PyObject, PyObjectKind, PyResult};
@@ -19,6 +19,7 @@ struct Compiler {
1919
current_source_location: ast::Location,
2020
}
2121

22+
/// Compile a given sourcecode into a bytecode object.
2223
pub fn compile(
2324
vm: &mut VirtualMachine,
2425
source: &str,

vm/src/exceptions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ pub fn print_exception(vm: &mut VirtualMachine, exc: &PyObjectRef) {
3030
for element in elements.iter() {
3131
if objtype::isinstance(&element, &vm.ctx.tuple_type()) {
3232
let element = objtuple::get_elements(&element);
33-
let filename = if let Ok(x) = vm.to_str(element[0].clone()) {
33+
let filename = if let Ok(x) = vm.to_str(&element[0]) {
3434
objstr::get_value(&x)
3535
} else {
3636
"<error>".to_string()
3737
};
3838

39-
let lineno = if let Ok(x) = vm.to_str(element[1].clone()) {
39+
let lineno = if let Ok(x) = vm.to_str(&element[1]) {
4040
objstr::get_value(&x)
4141
} else {
4242
"<error>".to_string()
4343
};
4444

45-
let obj_name = if let Ok(x) = vm.to_str(element[2].clone()) {
45+
let obj_name = if let Ok(x) = vm.to_str(&element[2]) {
4646
objstr::get_value(&x)
4747
} else {
4848
"<error>".to_string()
@@ -58,7 +58,7 @@ pub fn print_exception(vm: &mut VirtualMachine, exc: &PyObjectRef) {
5858
println!("No traceback set on exception");
5959
}
6060

61-
match vm.to_str(exc.clone()) {
61+
match vm.to_str(exc) {
6262
Ok(txt) => println!("{}", objstr::get_value(&txt)),
6363
Err(err) => println!("Error during error {:?}", err),
6464
}

vm/src/frame.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::obj::objstr;
1616
use super::obj::objtype;
1717
use super::pyobject::{
1818
AttributeProtocol, DictProtocol, IdProtocol, ParentProtocol, PyFuncArgs, PyObject,
19-
PyObjectKind, PyObjectRef, PyResult, ToRust, TypeProtocol,
19+
PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
2020
};
2121
use super::vm::VirtualMachine;
2222
use num_bigint::ToBigInt;
@@ -461,9 +461,8 @@ impl Frame {
461461
let kwarg_names = self.pop_value();
462462
let args: Vec<PyObjectRef> = self.pop_multiple(*count);
463463

464-
let kwarg_names = kwarg_names
465-
.to_vec()
466-
.unwrap()
464+
let kwarg_names = vm
465+
.extract_elements(&kwarg_names)?
467466
.iter()
468467
.map(|pyobj| objstr::get_value(pyobj))
469468
.collect();

vm/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
//! This crate contains most python logic.
2+
//!
3+
//! - Compilation
4+
//! - Bytecode
5+
//! - Import mechanics
6+
//! - Base objects
7+
18
#[macro_use]
29
extern crate bitflags;
310
#[macro_use]

vm/src/macros.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ macro_rules! arg_check {
6464
match expected_type {
6565
Some(expected_type) => {
6666
if !objtype::isinstance(arg, &expected_type) {
67-
let arg_typ = arg.typ().clone();
68-
let actual_type = arg_typ.borrow().str().clone();
67+
let arg_typ = arg.typ();
68+
let expected_type_name = $vm.to_pystr(expected_type)?;
69+
let actual_type = $vm.to_pystr(&arg_typ)?;
6970
return Err($vm.new_type_error(format!(
7071
"argument of type {} is required for parameter {} ({}) (got: {})",
71-
expected_type.borrow().str(),
72+
expected_type_name,
7273
arg_position + 1,
7374
arg_name,
7475
actual_type

vm/src/obj/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! This package contains the python basic/builtin types
2+
13
pub mod objbool;
24
pub mod objbytes;
35
pub mod objcomplex;

0 commit comments

Comments
 (0)