Skip to content

Commit 8462e7b

Browse files
Merge pull request RustPython#1506 from youknowone/refactoring
Refactor function signatures
2 parents 53fe356 + 6c0f852 commit 8462e7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+439
-523
lines changed

vm/src/builtins.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ use std::str;
99

1010
use num_bigint::Sign;
1111
use num_traits::{Signed, ToPrimitive, Zero};
12+
#[cfg(feature = "rustpython-compiler")]
13+
use rustpython_compiler::compile;
1214

15+
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
1316
use crate::obj::objbool::{self, IntoPyBool};
17+
use crate::obj::objbyteinner::PyByteInner;
1418
use crate::obj::objbytes::PyBytesRef;
1519
use crate::obj::objcode::PyCodeRef;
1620
use crate::obj::objdict::PyDictRef;
@@ -19,18 +23,14 @@ use crate::obj::objint::{self, PyIntRef};
1923
use crate::obj::objiter;
2024
use crate::obj::objstr::{PyString, PyStringRef};
2125
use crate::obj::objtype::{self, PyClassRef};
22-
#[cfg(feature = "rustpython-compiler")]
23-
use rustpython_compiler::compile;
24-
25-
use crate::function::{single_or_tuple_any, Args, KwArgs, OptionalArg, PyFuncArgs};
26+
use crate::pyhash;
2627
use crate::pyobject::{
2728
Either, IdProtocol, IntoPyObject, ItemProtocol, PyIterable, PyObjectRef, PyResult, PyValue,
2829
TryFromObject, TypeProtocol,
2930
};
3031
use crate::scope::Scope;
3132
use crate::vm::VirtualMachine;
3233

33-
use crate::obj::objbyteinner::PyByteInner;
3434
#[cfg(not(target_arch = "wasm32"))]
3535
use crate::stdlib::io::io_open;
3636

@@ -299,25 +299,25 @@ fn builtin_hasattr(obj: PyObjectRef, attr: PyStringRef, vm: &VirtualMachine) ->
299299
}
300300
}
301301

302-
fn builtin_hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
303-
vm._hash(&obj).and_then(|v| Ok(vm.new_int(v)))
302+
fn builtin_hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<pyhash::PyHash> {
303+
vm._hash(&obj)
304304
}
305305

306306
// builtin_help
307307

308308
fn builtin_hex(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
309309
let n = number.as_bigint();
310310
let s = if n.is_negative() {
311-
format!("-0x{:x}", n.abs())
311+
format!("-0x{:x}", -n)
312312
} else {
313313
format!("0x{:x}", n)
314314
};
315315

316316
Ok(vm.new_str(s))
317317
}
318318

319-
fn builtin_id(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
320-
Ok(vm.context().new_int(obj.get_id()))
319+
fn builtin_id(obj: PyObjectRef, _vm: &VirtualMachine) -> usize {
320+
obj.get_id()
321321
}
322322

323323
// builtin_input
@@ -492,7 +492,7 @@ fn builtin_oct(number: PyIntRef, vm: &VirtualMachine) -> PyResult {
492492
Ok(vm.new_str(s))
493493
}
494494

495-
fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) -> PyResult {
495+
fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) -> PyResult<u32> {
496496
match string {
497497
Either::A(bytes) => {
498498
let bytes_len = bytes.elements.len();
@@ -502,7 +502,7 @@ fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) ->
502502
bytes_len
503503
)));
504504
}
505-
Ok(vm.context().new_int(bytes.elements[0]))
505+
Ok(u32::from(bytes.elements[0]))
506506
}
507507
Either::B(string) => {
508508
let string = string.as_str();
@@ -514,7 +514,7 @@ fn builtin_ord(string: Either<PyByteInner, PyStringRef>, vm: &VirtualMachine) ->
514514
)));
515515
}
516516
match string.chars().next() {
517-
Some(character) => Ok(vm.context().new_int(character as i32)),
517+
Some(character) => Ok(character as u32),
518518
None => Err(vm.new_type_error(
519519
"ord() could not guess the integer representing this character".to_string(),
520520
)),

vm/src/cformat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::format::get_num_digits;
21
/// Implementation of Printf-Style string formatting
32
/// [https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting]
43
use num_bigint::{BigInt, Sign};
@@ -7,6 +6,8 @@ use std::cmp;
76
use std::fmt;
87
use std::str::FromStr;
98

9+
use crate::format::get_num_digits;
10+
1011
#[derive(Debug, PartialEq)]
1112
pub enum CFormatErrorType {
1213
UnmatchedKeyParentheses,

vm/src/frame.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::cell::RefCell;
22
use std::fmt;
33

4+
use indexmap::IndexMap;
5+
use itertools::Itertools;
6+
47
use crate::bytecode;
58
use crate::function::PyFuncArgs;
69
use crate::obj::objbool;
@@ -19,8 +22,6 @@ use crate::pyobject::{
1922
};
2023
use crate::scope::{NameProtocol, Scope};
2124
use crate::vm::VirtualMachine;
22-
use indexmap::IndexMap;
23-
use itertools::Itertools;
2425

2526
#[cfg(not(target_arch = "wasm32"))]
2627
use crate::stdlib::signal::check_signals;
@@ -1162,25 +1163,33 @@ impl Frame {
11621163
a.get_id()
11631164
}
11641165

1165-
fn _in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult {
1166+
fn _in(
1167+
&self,
1168+
vm: &VirtualMachine,
1169+
needle: PyObjectRef,
1170+
haystack: PyObjectRef,
1171+
) -> PyResult<bool> {
11661172
let found = vm._membership(haystack.clone(), needle)?;
1167-
Ok(vm.ctx.new_bool(objbool::boolval(vm, found)?))
1173+
Ok(objbool::boolval(vm, found)?)
11681174
}
11691175

1170-
fn _not_in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult {
1176+
fn _not_in(
1177+
&self,
1178+
vm: &VirtualMachine,
1179+
needle: PyObjectRef,
1180+
haystack: PyObjectRef,
1181+
) -> PyResult<bool> {
11711182
let found = vm._membership(haystack.clone(), needle)?;
1172-
Ok(vm.ctx.new_bool(!objbool::boolval(vm, found)?))
1183+
Ok(!objbool::boolval(vm, found)?)
11731184
}
11741185

11751186
fn _is(&self, a: PyObjectRef, b: PyObjectRef) -> bool {
11761187
// Pointer equal:
11771188
a.is(&b)
11781189
}
11791190

1180-
fn _is_not(&self, vm: &VirtualMachine, a: PyObjectRef, b: PyObjectRef) -> PyResult {
1181-
let result_bool = !a.is(&b);
1182-
let result = vm.ctx.new_bool(result_bool);
1183-
Ok(result)
1191+
fn _is_not(&self, a: PyObjectRef, b: PyObjectRef) -> bool {
1192+
!a.is(&b)
11841193
}
11851194

11861195
#[cfg_attr(feature = "flame-it", flame("Frame"))]
@@ -1199,9 +1208,9 @@ impl Frame {
11991208
bytecode::ComparisonOperator::Greater => vm._gt(a, b)?,
12001209
bytecode::ComparisonOperator::GreaterOrEqual => vm._ge(a, b)?,
12011210
bytecode::ComparisonOperator::Is => vm.ctx.new_bool(self._is(a, b)),
1202-
bytecode::ComparisonOperator::IsNot => self._is_not(vm, a, b)?,
1203-
bytecode::ComparisonOperator::In => self._in(vm, a, b)?,
1204-
bytecode::ComparisonOperator::NotIn => self._not_in(vm, a, b)?,
1211+
bytecode::ComparisonOperator::IsNot => vm.ctx.new_bool(self._is_not(a, b)),
1212+
bytecode::ComparisonOperator::In => vm.ctx.new_bool(self._in(vm, a, b)?),
1213+
bytecode::ComparisonOperator::NotIn => vm.ctx.new_bool(self._not_in(vm, a, b)?),
12051214
};
12061215

12071216
self.push_value(value);

vm/src/obj/objbool.rs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ The class bool is a subclass of the class int, and cannot be subclassed.";
8585
"__repr__" => context.new_rustfunc(bool_repr),
8686
"__format__" => context.new_rustfunc(bool_format),
8787
"__or__" => context.new_rustfunc(bool_or),
88-
"__ror__" => context.new_rustfunc(bool_ror),
88+
"__ror__" => context.new_rustfunc(bool_or),
8989
"__and__" => context.new_rustfunc(bool_and),
90-
"__rand__" => context.new_rustfunc(bool_rand),
90+
"__rand__" => context.new_rustfunc(bool_and),
9191
"__xor__" => context.new_rustfunc(bool_xor),
92-
"__rxor__" => context.new_rustfunc(bool_rxor),
92+
"__rxor__" => context.new_rustfunc(bool_xor),
9393
"__doc__" => context.new_str(bool_doc.to_string()),
9494
});
9595
}
9696

97-
pub fn not(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult {
97+
pub fn not(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<bool> {
9898
if objtype::isinstance(obj, &vm.ctx.bool_type()) {
9999
let value = get_value(obj);
100-
Ok(vm.ctx.new_bool(!value))
100+
Ok(!value)
101101
} else {
102102
Err(vm.new_type_error(format!("Can only invert a bool, on {:?}", obj)))
103103
}
@@ -128,80 +128,54 @@ fn bool_format(
128128
}
129129
}
130130

131-
fn do_bool_or(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
132-
if objtype::isinstance(lhs, &vm.ctx.bool_type())
133-
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
131+
fn bool_or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
132+
if objtype::isinstance(&lhs, &vm.ctx.bool_type())
133+
&& objtype::isinstance(&rhs, &vm.ctx.bool_type())
134134
{
135-
let lhs = get_value(lhs);
136-
let rhs = get_value(rhs);
135+
let lhs = get_value(&lhs);
136+
let rhs = get_value(&rhs);
137137
(lhs || rhs).into_pyobject(vm)
138138
} else {
139139
Ok(lhs.payload::<PyInt>().unwrap().or(rhs.clone(), vm))
140140
}
141141
}
142142

143-
fn bool_or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
144-
do_bool_or(vm, &lhs, &rhs)
145-
}
146-
147-
fn bool_ror(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
148-
do_bool_or(vm, &lhs, &rhs)
149-
}
150-
151-
fn do_bool_and(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
152-
if objtype::isinstance(lhs, &vm.ctx.bool_type())
153-
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
143+
fn bool_and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
144+
if objtype::isinstance(&lhs, &vm.ctx.bool_type())
145+
&& objtype::isinstance(&rhs, &vm.ctx.bool_type())
154146
{
155-
let lhs = get_value(lhs);
156-
let rhs = get_value(rhs);
147+
let lhs = get_value(&lhs);
148+
let rhs = get_value(&rhs);
157149
(lhs && rhs).into_pyobject(vm)
158150
} else {
159151
Ok(lhs.payload::<PyInt>().unwrap().and(rhs.clone(), vm))
160152
}
161153
}
162154

163-
fn bool_and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
164-
do_bool_and(vm, &lhs, &rhs)
165-
}
166-
167-
fn bool_rand(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
168-
do_bool_and(vm, &lhs, &rhs)
169-
}
170-
171-
fn do_bool_xor(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
172-
if objtype::isinstance(lhs, &vm.ctx.bool_type())
173-
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
155+
fn bool_xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
156+
if objtype::isinstance(&lhs, &vm.ctx.bool_type())
157+
&& objtype::isinstance(&rhs, &vm.ctx.bool_type())
174158
{
175-
let lhs = get_value(lhs);
176-
let rhs = get_value(rhs);
159+
let lhs = get_value(&lhs);
160+
let rhs = get_value(&rhs);
177161
(lhs ^ rhs).into_pyobject(vm)
178162
} else {
179163
Ok(lhs.payload::<PyInt>().unwrap().xor(rhs.clone(), vm))
180164
}
181165
}
182166

183-
fn bool_xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
184-
do_bool_xor(vm, &lhs, &rhs)
185-
}
186-
187-
fn bool_rxor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult {
188-
do_bool_xor(vm, &lhs, &rhs)
189-
}
190-
191167
fn bool_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
192168
arg_check!(
193169
vm,
194170
args,
195171
required = [(_zelf, Some(vm.ctx.type_type()))],
196172
optional = [(val, None)]
197173
);
198-
Ok(match val {
199-
Some(val) => {
200-
let bv = boolval(vm, val.clone())?;
201-
vm.new_bool(bv)
202-
}
203-
None => vm.context().new_bool(false),
204-
})
174+
let value = match val {
175+
Some(val) => boolval(vm, val.clone())?,
176+
None => false,
177+
};
178+
Ok(vm.new_bool(value))
205179
}
206180

207181
#[derive(Debug, Copy, Clone, PartialEq)]

0 commit comments

Comments
 (0)