Skip to content

Commit 19ed4f8

Browse files
committed
clean up vm::builtins
1 parent 61ded76 commit 19ed4f8

Some content is hidden

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

58 files changed

+313
-375
lines changed

common/src/str.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@ pub fn zfill(bytes: &[u8], width: usize) -> Vec<u8> {
2929
}
3030
}
3131

32+
/// Convert a string to ascii compatible, escaping unicodes into escape
33+
/// sequences.
34+
pub fn to_ascii(value: &str) -> String {
35+
let mut ascii = String::new();
36+
for c in value.chars() {
37+
if c.is_ascii() {
38+
ascii.push(c)
39+
} else {
40+
let c = c as i64;
41+
let hex = if c < 0x100 {
42+
format!("\\x{:02x}", c)
43+
} else if c < 0x10000 {
44+
format!("\\u{:04x}", c)
45+
} else {
46+
format!("\\U{:08x}", c)
47+
};
48+
ascii.push_str(&hex)
49+
}
50+
}
51+
ascii
52+
}
53+
3254
#[cfg(test)]
3355
mod tests {
3456
use super::*;

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ extern crate log;
77
use clap::{App, AppSettings, Arg, ArgMatches};
88
use rustpython_compiler::compile;
99
use rustpython_vm::{
10-
builtins::{isinstance, PyInt},
10+
builtins::PyInt,
1111
exceptions::print_exception,
1212
match_class,
13-
pyobject::{BorrowValue, ItemProtocol, PyResult},
13+
pyobject::{BorrowValue, ItemProtocol, PyResult, TypeProtocol},
1414
scope::Scope,
1515
util, InitParameter, Interpreter, PySettings, VirtualMachine,
1616
};
@@ -67,7 +67,7 @@ fn main() {
6767
// See if any exception leaked out:
6868
let exitcode = match res {
6969
Ok(()) => 0,
70-
Err(err) if isinstance(&err, &vm.ctx.exceptions.system_exit) => {
70+
Err(err) if err.isinstance(&vm.ctx.exceptions.system_exit) => {
7171
let args = err.args();
7272
match args.borrow_value().len() {
7373
0 => 0,

src/shell.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType}
44
use rustpython_parser::error::{LexicalErrorType, ParseErrorType};
55
use rustpython_vm::readline::{Readline, ReadlineResult};
66
use rustpython_vm::{
7-
builtins::isinstance,
87
exceptions::{print_exception, PyBaseExceptionRef},
9-
pyobject::{BorrowValue, PyResult},
8+
pyobject::{BorrowValue, PyResult, TypeProtocol},
109
scope::Scope,
1110
VirtualMachine,
1211
};
@@ -127,7 +126,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
127126
};
128127

129128
if let Err(exc) = result {
130-
if isinstance(&exc, &vm.ctx.exceptions.system_exit) {
129+
if exc.isinstance(&vm.ctx.exceptions.system_exit) {
131130
repl.save_history(&repl_history_path).unwrap();
132131
return Err(exc);
133132
}

vm/src/builtins/asyncgenerator.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use super::code::PyCodeRef;
2-
use super::pytype::{self, PyTypeRef};
2+
use super::pytype::PyTypeRef;
33
use crate::coroutine::{Coro, Variant};
44
use crate::exceptions::PyBaseExceptionRef;
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;
7-
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
7+
use crate::pyobject::{
8+
PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
9+
};
810
use crate::vm::VirtualMachine;
911

1012
use crossbeam_utils::atomic::AtomicCell;
@@ -15,7 +17,7 @@ pub struct PyAsyncGen {
1517
inner: Coro,
1618
running_async: AtomicCell<bool>,
1719
}
18-
pub type PyAsyncGenRef = PyRef<PyAsyncGen>;
20+
type PyAsyncGenRef = PyRef<PyAsyncGen>;
1921

2022
impl PyValue for PyAsyncGen {
2123
fn class(vm: &VirtualMachine) -> PyTypeRef {
@@ -29,7 +31,7 @@ impl PyAsyncGen {
2931
&self.inner
3032
}
3133

32-
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyAsyncGenRef {
34+
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyRef<Self> {
3335
PyAsyncGen {
3436
inner: Coro::new(frame, Variant::AsyncGen),
3537
running_async: AtomicCell::new(false),
@@ -127,8 +129,8 @@ impl PyAsyncGenWrappedValue {}
127129
impl PyAsyncGenWrappedValue {
128130
fn unbox(ag: &PyAsyncGen, val: PyResult, vm: &VirtualMachine) -> PyResult {
129131
if let Err(ref e) = val {
130-
if pytype::isinstance(&e, &vm.ctx.exceptions.stop_async_iteration)
131-
|| pytype::isinstance(&e, &vm.ctx.exceptions.generator_exit)
132+
if e.isinstance(&vm.ctx.exceptions.stop_async_iteration)
133+
|| e.isinstance(&vm.ctx.exceptions.generator_exit)
132134
{
133135
ag.inner.closed.store(true);
134136
}
@@ -385,8 +387,8 @@ impl PyAsyncGenAThrow {
385387
self.ag.running_async.store(false);
386388
self.state.store(AwaitableState::Closed);
387389
if self.aclose
388-
&& (pytype::isinstance(&exc, &vm.ctx.exceptions.stop_async_iteration)
389-
|| pytype::isinstance(&exc, &vm.ctx.exceptions.generator_exit))
390+
&& (exc.isinstance(&vm.ctx.exceptions.stop_async_iteration)
391+
|| exc.isinstance(&vm.ctx.exceptions.generator_exit))
390392
{
391393
vm.new_exception_empty(vm.ctx.exceptions.stop_iteration.clone())
392394
} else {

vm/src/builtins/builtinfunc.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use crate::pyobject::{
1111
use crate::slots::{Callable, SlotDescriptor};
1212
use crate::vm::VirtualMachine;
1313

14-
pub struct PyFuncDef {
14+
pub struct PyNativeFuncDef {
1515
pub func: PyNativeFunc,
1616
pub name: Option<PyStrRef>,
1717
pub doc: Option<PyStrRef>,
1818
}
1919

20-
impl From<PyNativeFunc> for PyFuncDef {
20+
impl From<PyNativeFunc> for PyNativeFuncDef {
2121
fn from(func: PyNativeFunc) -> Self {
2222
Self {
2323
func,
@@ -27,7 +27,7 @@ impl From<PyNativeFunc> for PyFuncDef {
2727
}
2828
}
2929

30-
impl PyFuncDef {
30+
impl PyNativeFuncDef {
3131
pub fn with_doc(mut self, doc: String, ctx: &PyContext) -> Self {
3232
self.doc = Some(ctx.new_stringref(doc));
3333
self
@@ -58,7 +58,7 @@ impl PyFuncDef {
5858

5959
#[pyclass(name = "builtin_function_or_method", module = false)]
6060
pub struct PyBuiltinFunction {
61-
value: PyFuncDef,
61+
value: PyNativeFuncDef,
6262
module: Option<PyObjectRef>,
6363
}
6464

@@ -80,11 +80,11 @@ impl fmt::Debug for PyBuiltinFunction {
8080

8181
impl From<PyNativeFunc> for PyBuiltinFunction {
8282
fn from(value: PyNativeFunc) -> Self {
83-
PyFuncDef::from(value).into()
83+
PyNativeFuncDef::from(value).into()
8484
}
8585
}
86-
impl From<PyFuncDef> for PyBuiltinFunction {
87-
fn from(value: PyFuncDef) -> Self {
86+
impl From<PyNativeFuncDef> for PyBuiltinFunction {
87+
fn from(value: PyNativeFuncDef) -> Self {
8888
Self {
8989
value,
9090
module: None,
@@ -135,7 +135,7 @@ impl PyBuiltinFunction {
135135

136136
#[pyclass(module = false, name = "method_descriptor")]
137137
pub struct PyBuiltinMethod {
138-
value: PyFuncDef,
138+
value: PyNativeFuncDef,
139139
}
140140

141141
impl PyValue for PyBuiltinMethod {
@@ -150,28 +150,12 @@ impl fmt::Debug for PyBuiltinMethod {
150150
}
151151
}
152152

153-
impl From<PyFuncDef> for PyBuiltinMethod {
154-
fn from(value: PyFuncDef) -> Self {
153+
impl From<PyNativeFuncDef> for PyBuiltinMethod {
154+
fn from(value: PyNativeFuncDef) -> Self {
155155
Self { value }
156156
}
157157
}
158158

159-
impl PyBuiltinMethod {
160-
pub fn new_with_name(func: PyNativeFunc, name: PyStrRef) -> Self {
161-
Self {
162-
value: PyFuncDef {
163-
func,
164-
name: Some(name),
165-
doc: None,
166-
},
167-
}
168-
}
169-
170-
pub fn as_func(&self) -> &PyNativeFunc {
171-
&self.value.func
172-
}
173-
}
174-
175159
impl SlotDescriptor for PyBuiltinMethod {
176160
fn descr_get(
177161
zelf: PyObjectRef,

vm/src/builtins/bytearray.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl PyByteArray {
107107
cls: PyTypeRef,
108108
options: ByteInnerNewOptions,
109109
vm: &VirtualMachine,
110-
) -> PyResult<PyByteArrayRef> {
110+
) -> PyResult<PyRef<Self>> {
111111
PyByteArray::from_inner(options.get_value(vm)?).into_ref_with_type(vm, cls)
112112
}
113113

vm/src/builtins/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl PyBytes {
111111
cls: PyTypeRef,
112112
options: ByteInnerNewOptions,
113113
vm: &VirtualMachine,
114-
) -> PyResult<PyBytesRef> {
114+
) -> PyResult<PyRef<Self>> {
115115
PyBytes {
116116
inner: options.get_value(vm)?,
117117
buffer_options: OnceCell::new(),

vm/src/builtins/classmethod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use crate::vm::VirtualMachine;
3030
pub struct PyClassMethod {
3131
callable: PyObjectRef,
3232
}
33-
pub type PyClassMethodRef = PyRef<PyClassMethod>;
3433

3534
impl From<PyObjectRef> for PyClassMethod {
3635
fn from(value: PyObjectRef) -> Self {
@@ -60,11 +59,7 @@ impl SlotDescriptor for PyClassMethod {
6059
#[pyimpl(with(SlotDescriptor), flags(BASETYPE, HAS_DICT))]
6160
impl PyClassMethod {
6261
#[pyslot]
63-
fn tp_new(
64-
cls: PyTypeRef,
65-
callable: PyObjectRef,
66-
vm: &VirtualMachine,
67-
) -> PyResult<PyClassMethodRef> {
62+
fn tp_new(cls: PyTypeRef, callable: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
6863
PyClassMethod { callable }.into_ref_with_type(vm, cls)
6964
}
7065

vm/src/builtins/complex.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use num_traits::Zero;
33

44
use super::float;
55
use super::pystr::PyStr;
6-
use super::pytype::{self, PyTypeRef};
6+
use super::pytype::PyTypeRef;
77
use crate::pyobject::{
88
BorrowValue, IntoPyObject, Never, PyArithmaticValue, PyClassImpl, PyComparisonValue, PyContext,
99
PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
@@ -21,8 +21,6 @@ pub struct PyComplex {
2121
value: Complex64,
2222
}
2323

24-
type PyComplexRef = PyRef<PyComplex>;
25-
2624
impl PyValue for PyComplex {
2725
fn class(vm: &VirtualMachine) -> PyTypeRef {
2826
vm.ctx.types.complex_type.clone()
@@ -223,7 +221,7 @@ impl PyComplex {
223221
}
224222

225223
#[pyslot]
226-
fn tp_new(cls: PyTypeRef, args: ComplexArgs, vm: &VirtualMachine) -> PyResult<PyComplexRef> {
224+
fn tp_new(cls: PyTypeRef, args: ComplexArgs, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
227225
let real = match args.real {
228226
None => Complex64::new(0.0, 0.0),
229227
Some(obj) => {
@@ -253,7 +251,7 @@ impl PyComplex {
253251
Some(obj) => {
254252
if let Some(c) = try_complex(&obj, vm)? {
255253
c
256-
} else if pytype::issubclass(obj.class(), &vm.ctx.types.str_type) {
254+
} else if obj.class().issubclass(&vm.ctx.types.str_type) {
257255
return Err(
258256
vm.new_type_error("complex() second arg can't be a string".to_owned())
259257
);

vm/src/builtins/coroutine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::function::OptionalArg;
77
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
88
use crate::vm::VirtualMachine;
99

10-
pub type PyCoroutineRef = PyRef<PyCoroutine>;
10+
type PyCoroutineRef = PyRef<PyCoroutine>;
1111

1212
#[pyclass(module = false, name = "coroutine")]
1313
#[derive(Debug)]
@@ -27,7 +27,7 @@ impl PyCoroutine {
2727
&self.inner
2828
}
2929

30-
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyCoroutineRef {
30+
pub fn new(frame: FrameRef, vm: &VirtualMachine) -> PyRef<Self> {
3131
PyCoroutine {
3232
inner: Coro::new(frame, Variant::Coroutine),
3333
}

0 commit comments

Comments
 (0)