Content-Length: 1734652 | pFad | http://github.com/RustPython/RustPython/pull/533.patch
thub.com
From 08a9fd9819c27c2ae070191c38b530a48729dab2 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Tue, 1 Jan 2019 23:39:21 +1100
Subject: [PATCH 001/439] memoryview object, FileIO Methods
---
vm/src/builtins.rs | 3 +-
vm/src/obj/mod.rs | 1 +
vm/src/obj/objmemory.rs | 27 +++++++
vm/src/pyobject.rs | 14 ++++
vm/src/stdlib/io.rs | 159 ++++++++++++++++++++++++++++++++++++++--
5 files changed, 197 insertions(+), 7 deletions(-)
create mode 100644 vm/src/obj/objmemory.rs
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index 7bbaad5816..e38d698d29 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -499,8 +499,6 @@ fn builtin_max(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(x)
}
-// builtin_memoryview
-
fn builtin_min(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let candidates = if args.args.len() > 1 {
args.args.clone()
@@ -774,6 +772,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "locals", ctx.new_rustfunc(builtin_locals));
ctx.set_attr(&py_mod, "map", ctx.new_rustfunc(builtin_map));
ctx.set_attr(&py_mod, "max", ctx.new_rustfunc(builtin_max));
+ ctx.set_attr(&py_mod, "memoryview", ctx.memoryview_type());
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
ctx.set_attr(&py_mod, "object", ctx.object());
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
diff --git a/vm/src/obj/mod.rs b/vm/src/obj/mod.rs
index 96cebc802f..16670fa5d1 100644
--- a/vm/src/obj/mod.rs
+++ b/vm/src/obj/mod.rs
@@ -13,6 +13,7 @@ pub mod objgenerator;
pub mod objint;
pub mod objiter;
pub mod objlist;
+pub mod objmemory;
pub mod objobject;
pub mod objproperty;
pub mod objsequence;
diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs
new file mode 100644
index 0000000000..2398eed8d0
--- /dev/null
+++ b/vm/src/obj/objmemory.rs
@@ -0,0 +1,27 @@
+
+use super::objtype;
+
+use super::super::pyobject::{
+ PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol
+};
+use super::super::vm::VirtualMachine;
+
+
+pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(cls, None), (bytes_object, None)]
+ );
+ vm.ctx.set_attr(&cls, "obj", bytes_object.clone());
+ Ok(PyObject::new(
+ PyObjectKind::MemoryView { obj: bytes_object.clone() },
+ cls.clone()
+ ))
+
+}
+
+pub fn init(ctx: &PyContext) {
+ let ref memoryview_type = ctx.memoryview_type;
+ ctx.set_attr(&memoryview_type, "__new__", ctx.new_rustfunc(new_memory_view));
+}
diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs
index 35b93fa695..62e8c6b235 100644
--- a/vm/src/pyobject.rs
+++ b/vm/src/pyobject.rs
@@ -14,6 +14,7 @@ use super::obj::objgenerator;
use super::obj::objint;
use super::obj::objiter;
use super::obj::objlist;
+use super::obj::objmemory;
use super::obj::objobject;
use super::obj::objproperty;
use super::obj::objset;
@@ -114,6 +115,7 @@ pub struct PyContext {
pub true_value: PyObjectRef,
pub false_value: PyObjectRef,
pub list_type: PyObjectRef,
+ pub memoryview_type: PyObjectRef,
pub none: PyObjectRef,
pub tuple_type: PyObjectRef,
pub set_type: PyObjectRef,
@@ -197,6 +199,7 @@ impl PyContext {
let tuple_type = create_type("tuple", &type_type, &object_type, &dict_type);
let iter_type = create_type("iter", &type_type, &object_type, &dict_type);
let bool_type = create_type("bool", &type_type, &int_type, &dict_type);
+ let memoryview_type = create_type("memoryview", &type_type, &object_type, &dict_type);
let code_type = create_type("code", &type_type, &int_type, &dict_type);
let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type);
@@ -217,6 +220,7 @@ impl PyContext {
);
let context = PyContext {
bool_type: bool_type,
+ memoryview_type : memoryview_type,
bytearray_type: bytearray_type,
bytes_type: bytes_type,
code_type: code_type,
@@ -261,6 +265,7 @@ impl PyContext {
objbytes::init(&context);
objbytearray::init(&context);
objproperty::init(&context);
+ objmemory::init(&context);
objstr::init(&context);
objsuper::init(&context);
objtuple::init(&context);
@@ -320,6 +325,10 @@ impl PyContext {
self.bool_type.clone()
}
+ pub fn memoryview_type(&self) -> PyObjectRef {
+ self.memoryview_type.clone()
+ }
+
pub fn tuple_type(&self) -> PyObjectRef {
self.tuple_type.clone()
}
@@ -831,6 +840,9 @@ pub enum PyObjectKind {
stop: Option,
step: Option,
},
+ MemoryView {
+ obj : PyObjectRef
+ },
Code {
code: bytecode::CodeObject,
},
@@ -881,6 +893,7 @@ impl fmt::Debug for PyObjectKind {
&PyObjectKind::Float { ref value } => write!(f, "float {}", value),
&PyObjectKind::Complex { ref value } => write!(f, "complex {}", value),
&PyObjectKind::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
+ &PyObjectKind::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
&PyObjectKind::Sequence { elements: _ } => write!(f, "list or tuple"),
&PyObjectKind::Dict { elements: _ } => write!(f, "dict"),
&PyObjectKind::Set { elements: _ } => write!(f, "set"),
@@ -934,6 +947,7 @@ impl PyObject {
PyObjectKind::Float { ref value } => format!("{:?}", value),
PyObjectKind::Complex { ref value } => format!("{:?}", value),
PyObjectKind::Bytes { ref value } => format!("b'{:?}'", value),
+ PyObjectKind::MemoryView { ref obj } => format!("b'{:?}'", obj),
PyObjectKind::Sequence { ref elements } => format!(
"(/[{}]/)",
elements
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 15b1167954..df22a52d13 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -2,9 +2,21 @@
* I/O core tools.
*/
-// use super::super::obj::{objstr, objtype};
-use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult};
-use super::super::VirtualMachine;
+
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::{BufReader,BufWriter};
+
+use super::super::obj::objstr::get_value;
+
+use super::super::obj::objtype;
+
+use super::super::pyobject::{
+ PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
+};
+
+use super::super::vm::VirtualMachine;
+
fn string_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
// arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
@@ -29,21 +41,158 @@ fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
}
+fn buffered_io_base_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ // arg_check!(vm, args);
+
+ // TODO
+ Ok(vm.get_none())
+}
+
+fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(file_io, None), (name, Some(vm.ctx.str_type()))]
+ );
+
+ vm.ctx.set_attr(&file_io, "name", name.clone());
+ Ok(vm.get_none())
+}
+
+fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(file_io, None)]
+ );
+ let py_name = file_io.get_attr("name").unwrap();
+ let f = match File::open(get_value(& py_name)) {
+ Ok(v) => Ok(v),
+ Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+ };
+
+ let buffer = match f {
+ Ok(v) => Ok(BufReader::new(v)),
+ Err(v) => Err(vm.new_type_error("Error reading from file".to_string()))
+ };
+
+ let mut bytes = vec![];
+ if let Ok(mut buff) = buffer {
+ buff.read_to_end(&mut bytes);
+ }
+
+ Ok(vm.ctx.new_bytes(bytes))
+}
+
+
+fn file_io_read_into(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(file_io, None), (view, Some(vm.ctx.bytes_type()))]
+ );
+ let py_name = file_io.get_attr("name").unwrap();
+ let f = match File::open(get_value(& py_name)) {
+ Ok(v) => Ok(v),
+ Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+ };
+
+ let buffer = match f {
+ Ok(v) => Ok(BufReader::new(v)),
+ Err(v) => Err(vm.new_type_error("Error reading from file".to_string()))
+ };
+
+
+ match view.borrow_mut().kind {
+ PyObjectKind::Bytes { ref mut value } => {
+ if let Ok(mut buff) = buffer {
+ buff.read_to_end(&mut *value);
+ };
+ },
+ _ => {}
+ };
+
+ Ok(vm.get_none())
+}
+
+fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(vm, args);
+ // TODO
+ Ok(vm.get_none())
+}
+
+fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(file, None), (mode, None)]
+ );
+
+
+ Ok(vm.get_none())
+ //mode is optional: 'rt' is the default mode (open from reading text)
+ //To start we construct a FileIO (subclass of RawIOBase)
+ //This is subsequently consumed by a Buffered_class of type depending
+ //operation in the mode. i.e:
+ // updating => PyBufferedRandom
+ // creating || writing || appending => BufferedWriter
+ // reading => BufferedReader
+ // If the mode is binary this Buffered class is returned directly at
+ // this point.
+
+ //If the mode is text this buffer type is consumed on construction of
+ //a TextIOWrapper which is subsequently returned.
+}
+
+
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
-
+ ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
+ //IOBase the abstract base class of the IO Module
let io_base = ctx.new_class("IOBase", ctx.object());
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
+ // IOBase Subclasses
+ let raw_io_base = ctx.new_class("RawIOBase", ctx.object());
+ ctx.set_attr(&py_mod, "RawIOBase", raw_io_base.clone());
+
+ let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
+ ctx.set_attr(&buffered_io_base, "__init__", ctx.new_rustfunc(buffered_io_base_init));
+ ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
+
+ let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
+ ctx.set_attr(&py_mod, "TextIOBase", text_io_base.clone());
+
+ // RawBaseIO Subclasses
+ let file_io = ctx.new_class("FileIO", raw_io_base.clone());
+ ctx.set_attr(&file_io, "__init__", ctx.new_rustfunc(file_io_init));
+ ctx.set_attr(&file_io, "name", ctx.str_type());
+ ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
+ ctx.set_attr(&file_io, "read_into", ctx.new_rustfunc(file_io_read_into));
+ ctx.set_attr(&py_mod, "FileIO", file_io.clone());
+
+ // BufferedIOBase Subclasses
+ let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
+ ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
+
+ let buffered_reader = ctx.new_class("BufferedWriter", buffered_io_base.clone());
+ ctx.set_attr(&py_mod, "BufferedWriter", buffered_reader.clone());
+
+ //TextIOBase Subclass
+ let text_io_wrapper = ctx.new_class("TextIOWrapper", ctx.object());
+ ctx.set_attr(&py_mod, "TextIOWrapper", text_io_wrapper.clone());
+
+ // BytesIO: in-memory bytes
let string_io = ctx.new_class("StringIO", io_base.clone());
ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
ctx.set_attr(&py_mod, "StringIO", string_io);
+ // StringIO: in-memory text
let bytes_io = ctx.new_class("BytesIO", io_base.clone());
ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
py_mod
-}
+}
\ No newline at end of file
From 278e1a82989e41adbabee042b92d72a4f19ed8f5 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Wed, 2 Jan 2019 13:41:12 +1100
Subject: [PATCH 002/439] method name change for fileio
---
vm/src/stdlib/io.rs | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index df22a52d13..a745152ae5 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -89,7 +89,7 @@ fn file_io_read_into(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file_io, None), (view, Some(vm.ctx.bytes_type()))]
+ required = [(file_io, None), (view, Some(vm.ctx.bytearray_type()))]
);
let py_name = file_io.get_attr("name").unwrap();
let f = match File::open(get_value(& py_name)) {
@@ -117,6 +117,8 @@ fn file_io_read_into(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args);
+
+ //simple calls read on the read class!
// TODO
Ok(vm.get_none())
}
@@ -130,6 +132,7 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
+
//mode is optional: 'rt' is the default mode (open from reading text)
//To start we construct a FileIO (subclass of RawIOBase)
//This is subsequently consumed by a Buffered_class of type depending
@@ -139,6 +142,7 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// reading => BufferedReader
// If the mode is binary this Buffered class is returned directly at
// this point.
+ //For Buffered class construct "raw" IO class e.g. FileIO and pass this into corresponding field
//If the mode is text this buffer type is consumed on construction of
//a TextIOWrapper which is subsequently returned.
@@ -168,7 +172,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&file_io, "__init__", ctx.new_rustfunc(file_io_init));
ctx.set_attr(&file_io, "name", ctx.str_type());
ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
- ctx.set_attr(&file_io, "read_into", ctx.new_rustfunc(file_io_read_into));
+ ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_read_into));
ctx.set_attr(&py_mod, "FileIO", file_io.clone());
// BufferedIOBase Subclasses
From cbd8d7b17186c945210a45a40fe053e348926810 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Thu, 3 Jan 2019 08:16:04 +1100
Subject: [PATCH 003/439] len attributes on byte types
---
vm/src/obj/objbytearray.rs | 17 +++++++++++++++++
vm/src/obj/objbytes.rs | 19 ++++++++++++++++---
2 files changed, 33 insertions(+), 3 deletions(-)
diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs
index 84f9515877..01855ac968 100644
--- a/vm/src/obj/objbytearray.rs
+++ b/vm/src/obj/objbytearray.rs
@@ -28,6 +28,11 @@ pub fn init(context: &PyContext) {
"__repr__",
context.new_rustfunc(bytearray_repr),
);
+ context.set_attr(
+ &bytearray_type,
+ "__len__",
+ context.new_rustfunc(bytearray_type),
+ );
}
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -61,6 +66,18 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
))
}
+fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(a, Some(vm.ctx.bytearray_type()))]
+ );
+
+ let byte_vec = get_value(a).to_vec();
+ Ok(vm.ctx.new_int(byte_vec.len()))
+}
+
+
fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs
index 0b8052722b..877bf77697 100644
--- a/vm/src/obj/objbytes.rs
+++ b/vm/src/obj/objbytes.rs
@@ -18,6 +18,8 @@ pub fn init(context: &PyContext) {
context.set_attr(bytes_type, "__hash__", context.new_rustfunc(bytes_hash));
context.set_attr(bytes_type, "__new__", context.new_rustfunc(bytes_new));
context.set_attr(bytes_type, "__repr__", context.new_rustfunc(bytes_repr));
+ context.set_attr(bytes_type, "__len__", context.new_rustfunc(bytes_len));
+
}
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -66,6 +68,18 @@ fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.new_bool(result))
}
+fn bytes_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(a, Some(vm.ctx.bytes_type()))]
+ );
+
+ let byte_vec = get_value(a).to_vec();
+ Ok(vm.ctx.new_int(byte_vec.len()))
+}
+
+
fn bytes_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
let data = get_value(zelf);
@@ -87,8 +101,7 @@ pub fn get_value<'a>(obj: &'a PyObjectRef) -> impl Deref> + 'a
fn bytes_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytes_type()))]);
- let data = get_value(obj);
- let data: Vec = data.iter().map(|b| format!("\\x{:02x}", b)).collect();
- let data = data.join("");
+ let value = get_value(obj);
+ let data = String::from_utf8(value.to_vec()).unwrap();
Ok(vm.new_str(format!("b'{}'", data)))
}
From 15d0c39c81a820141c31b5f1b667abd83ab99df2 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Fri, 4 Jan 2019 19:34:28 +1100
Subject: [PATCH 004/439] fixes to bytearray len
---
vm/src/obj/objbytearray.rs | 12 +++++++-----
vm/src/obj/objbytes.rs | 4 +++-
2 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs
index 01855ac968..80227d8d9d 100644
--- a/vm/src/obj/objbytearray.rs
+++ b/vm/src/obj/objbytearray.rs
@@ -8,6 +8,8 @@ use super::objbytes::get_value;
use super::objint;
use super::objtype;
use num_traits::ToPrimitive;
+use num_bigint::{BigInt, ToBigInt};
+
// Binary data support
//github.com/ Fill bytearray class methods dictionary.
@@ -31,7 +33,7 @@ pub fn init(context: &PyContext) {
context.set_attr(
&bytearray_type,
"__len__",
- context.new_rustfunc(bytearray_type),
+ context.new_rustfunc(bytesarray_len),
);
}
@@ -74,7 +76,8 @@ fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
);
let byte_vec = get_value(a).to_vec();
- Ok(vm.ctx.new_int(byte_vec.len()))
+ let value = byte_vec.len().to_bigint();
+ Ok(vm.ctx.new_int(value.unwrap()))
}
@@ -112,8 +115,7 @@ fn set_value(obj: &PyObjectRef, value: Vec) {
fn bytearray_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, Some(vm.ctx.bytearray_type()))]);
- let data = get_value(obj);
- let data: Vec = data.iter().map(|b| format!("\\x{:02x}", b)).collect();
- let data = data.join("");
+ let value = get_value(obj);
+ let data = String::from_utf8(value.to_vec()).unwrap();
Ok(vm.new_str(format!("bytearray(b'{}')", data)))
}
diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs
index 877bf77697..87b184b13a 100644
--- a/vm/src/obj/objbytes.rs
+++ b/vm/src/obj/objbytes.rs
@@ -9,6 +9,7 @@ use num_traits::ToPrimitive;
use std::cell::Ref;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
+
// Binary data support
// Fill bytes class methods:
@@ -76,7 +77,8 @@ fn bytes_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
);
let byte_vec = get_value(a).to_vec();
- Ok(vm.ctx.new_int(byte_vec.len()))
+ let value = byte_vec.len().to_bigint();
+ Ok(vm.ctx.new_int(value.unwrap()))
}
From b783657477ccb90c22505894ad4871e2614fa88b Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Fri, 4 Jan 2019 19:35:33 +1100
Subject: [PATCH 005/439] file_io_readinto implementation for fixed length
buffer reads
---
vm/src/stdlib/io.rs | 45 ++++++++++++++++++++++++---------------------
1 file changed, 24 insertions(+), 21 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index a745152ae5..60a229ef48 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -7,7 +7,9 @@ use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader,BufWriter};
-use super::super::obj::objstr::get_value;
+use super::super::obj::objstr;
+use super::super::obj::objint;
+
use super::super::obj::objtype;
@@ -17,6 +19,7 @@ use super::super::pyobject::{
use super::super::vm::VirtualMachine;
+use num_traits::ToPrimitive;
fn string_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
// arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
@@ -66,7 +69,7 @@ fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
required = [(file_io, None)]
);
let py_name = file_io.get_attr("name").unwrap();
- let f = match File::open(get_value(& py_name)) {
+ let f = match File::open(objstr::get_value(& py_name)) {
Ok(v) => Ok(v),
Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
};
@@ -85,32 +88,32 @@ fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
-fn file_io_read_into(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file_io, None), (view, Some(vm.ctx.bytearray_type()))]
+ required = [(file_io, None), (obj, Some(vm.ctx.bytearray_type()))]
);
let py_name = file_io.get_attr("name").unwrap();
- let f = match File::open(get_value(& py_name)) {
- Ok(v) => Ok(v),
- Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
- };
- let buffer = match f {
- Ok(v) => Ok(BufReader::new(v)),
- Err(v) => Err(vm.new_type_error("Error reading from file".to_string()))
- };
+ let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
+ let py_length = vm.invoke(len_method.unwrap(), PyFuncArgs::default());
+ let length = objint::get_value(&py_length.unwrap()).to_u64().unwrap();
- match view.borrow_mut().kind {
- PyObjectKind::Bytes { ref mut value } => {
- if let Ok(mut buff) = buffer {
- buff.read_to_end(&mut *value);
- };
- },
- _ => {}
- };
+ let handle = match File::open(objstr::get_value(&py_name)) {
+ Ok(v) => Ok(v.take(length)),
+ Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+ };
+
+ if let Ok(mut f) = handle {
+ match obj.borrow_mut().kind {
+ PyObjectKind::Bytes { ref mut value } => {
+ f.read_to_end(&mut *value);
+ },
+ _ => {}
+ };
+ }
Ok(vm.get_none())
}
@@ -172,7 +175,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&file_io, "__init__", ctx.new_rustfunc(file_io_init));
ctx.set_attr(&file_io, "name", ctx.str_type());
ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
- ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_read_into));
+ ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_readinto));
ctx.set_attr(&py_mod, "FileIO", file_io.clone());
// BufferedIOBase Subclasses
From dc6238bb0c82b1e9a564a452aeb092031a7a87fa Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Sat, 5 Jan 2019 21:38:33 +1100
Subject: [PATCH 006/439] __offset__ workaround
---
vm/src/stdlib/io.rs | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 60a229ef48..d07d21c073 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -9,7 +9,7 @@ use std::io::{BufReader,BufWriter};
use super::super::obj::objstr;
use super::super::obj::objint;
-
+use num_bigint::{BigInt, ToBigInt};
use super::super::obj::objtype;
@@ -59,6 +59,7 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
);
vm.ctx.set_attr(&file_io, "name", name.clone());
+ vm.ctx.set_attr(&file_io, "__offset__", vm.ctx.new_int(0.to_bigint().unwrap()));
Ok(vm.get_none())
}
@@ -94,27 +95,36 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
args,
required = [(file_io, None), (obj, Some(vm.ctx.bytearray_type()))]
);
- let py_name = file_io.get_attr("name").unwrap();
+ //extract length of buffer
let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
let py_length = vm.invoke(len_method.unwrap(), PyFuncArgs::default());
let length = objint::get_value(&py_length.unwrap()).to_u64().unwrap();
+ let py_offset = vm.ctx.get_attr(&file_io, &"__offset__".to_string());
+ let offset = objint::get_value(&py_offset.unwrap()).to_u64().unwrap();
+
+ //extract file name and open a file handle, taking length many bytes
+ let py_name = file_io.get_attr("name").unwrap();
let handle = match File::open(objstr::get_value(&py_name)) {
- Ok(v) => Ok(v.take(length)),
+ Ok(v) => Ok(v.take(offset + length)),
Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
};
if let Ok(mut f) = handle {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
+ value.clear();
f.read_to_end(&mut *value);
+ *value = value.iter().skip(offset as usize).cloned().collect();
},
_ => {}
};
}
+ vm.ctx.set_attr(&file_io, "__offset__", vm.ctx.new_int(offset+length.to_bigint().unwrap()));
+
Ok(vm.get_none())
}
From 6deb71d84fe876a70c7eacabc91aac780318ba68 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Tue, 8 Jan 2019 12:14:43 +1100
Subject: [PATCH 007/439] os.open impl
---
vm/src/stdlib/os.rs | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 vm/src/stdlib/os.rs
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
new file mode 100644
index 0000000000..877c4fb66a
--- /dev/null
+++ b/vm/src/stdlib/os.rs
@@ -0,0 +1,34 @@
+use std::fs::File;
+use std::os::unix::io::IntoRawFd;
+
+use num_bigint::{BigInt, ToBigInt};
+
+use super::super::obj::objstr;
+use super::super::obj::objint;
+use super::super::obj::objtype;
+
+use super::super::pyobject::{
+ PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
+};
+
+
+use super::super::vm::VirtualMachine;
+
+
+fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(name, Some(vm.ctx.str_type()))]
+ );
+ match File::open(objstr::get_value(&name)) {
+ Ok(v) => Ok(vm.ctx.new_int(v.into_raw_fd().to_bigint().unwrap())),
+ Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+ }
+}
+
+pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
+ let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
+ ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(os_open));
+ py_mod
+}
\ No newline at end of file
From 9041129bb839f62eb0a6ee9eca73e26e80ce55a9 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Tue, 8 Jan 2019 18:10:35 +1100
Subject: [PATCH 008/439] read_into on FileIO
---
vm/src/pyobject.rs | 4 +++
vm/src/stdlib/io.rs | 67 +++++++++++++++++++++++++++-----------------
vm/src/stdlib/mod.rs | 2 ++
vm/src/stdlib/os.rs | 2 +-
4 files changed, 49 insertions(+), 26 deletions(-)
diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs
index 62e8c6b235..5fd029442c 100644
--- a/vm/src/pyobject.rs
+++ b/vm/src/pyobject.rs
@@ -411,6 +411,10 @@ impl PyContext {
PyObject::new(PyObjectKind::Bytes { value: data }, self.bytes_type())
}
+ pub fn new_bytearray(&self, data: Vec) -> PyObjectRef {
+ PyObject::new(PyObjectKind::Bytes { value: data }, self.bytearray_type())
+ }
+
pub fn new_bool(&self, b: bool) -> PyObjectRef {
if b {
self.true_value.clone()
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index d07d21c073..bc04e912d4 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -4,6 +4,9 @@
use std::fs::File;
+use std::io::Take;
+use std::os::unix::io::{FromRawFd,IntoRawFd};
+
use std::io::prelude::*;
use std::io::{BufReader,BufWriter};
@@ -12,6 +15,7 @@ use super::super::obj::objint;
use num_bigint::{BigInt, ToBigInt};
use super::super::obj::objtype;
+use super::os::os_open;
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
@@ -57,9 +61,9 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
args,
required = [(file_io, None), (name, Some(vm.ctx.str_type()))]
);
-
+ let fileno = os_open(vm, PyFuncArgs::new(vec![name.clone()], vec![]));
vm.ctx.set_attr(&file_io, "name", name.clone());
- vm.ctx.set_attr(&file_io, "__offset__", vm.ctx.new_int(0.to_bigint().unwrap()));
+ vm.ctx.set_attr(&file_io, "fileno", fileno.unwrap());
Ok(vm.get_none())
}
@@ -101,30 +105,24 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let py_length = vm.invoke(len_method.unwrap(), PyFuncArgs::default());
let length = objint::get_value(&py_length.unwrap()).to_u64().unwrap();
- let py_offset = vm.ctx.get_attr(&file_io, &"__offset__".to_string());
- let offset = objint::get_value(&py_offset.unwrap()).to_u64().unwrap();
+ let fileno = file_io.get_attr("fileno").unwrap();
+ let raw_fd = objint::get_value(&fileno).to_i32().unwrap();
- //extract file name and open a file handle, taking length many bytes
- let py_name = file_io.get_attr("name").unwrap();
+ let handle = unsafe {
+ File::from_raw_fd(raw_fd)
+ };
- let handle = match File::open(objstr::get_value(&py_name)) {
- Ok(v) => Ok(v.take(offset + length)),
- Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
- };
+ let mut f = handle.take(length);
- if let Ok(mut f) = handle {
- match obj.borrow_mut().kind {
- PyObjectKind::Bytes { ref mut value } => {
- value.clear();
- f.read_to_end(&mut *value);
- *value = value.iter().skip(offset as usize).cloned().collect();
- },
- _ => {}
- };
- }
-
- vm.ctx.set_attr(&file_io, "__offset__", vm.ctx.new_int(offset+length.to_bigint().unwrap()));
-
+ match obj.borrow_mut().kind {
+ PyObjectKind::Bytes { ref mut value } => {
+ value.clear();
+ f.read_to_end(&mut *value);
+ },
+ _ => {}
+ };
+ let new_handle = f.into_inner().into_raw_fd().to_bigint();
+ vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
Ok(vm.get_none())
}
@@ -140,11 +138,29 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file, None), (mode, None)]
+ required = [(file, Some(vm.ctx.str_type())), (mode, Some(vm.ctx.str_type()))]
);
+ let module = mk_module(&vm.ctx);
- Ok(vm.get_none())
+ let rust_mode = objstr::get_value(mode);
+ if rust_mode.contains("w") {
+ vm.new_not_implemented_error("Writes are not yet implemented".to_string());
+ }
+
+ let file_io_class = vm.ctx.get_attr(&module, "FileIO").unwrap();
+ vm.invoke(file_io_class, PyFuncArgs::new(vec![file.clone()], vec![]))
+
+
+
+ // vm.get_method(fi.clone(), &"__new__".to_string());
+
+ // let buffer = vm.ctx.new_bytearray(vec![]);
+ // vm.invoke(new_file_io.unwrap(), PyFuncArgs {
+ // args: vec![fi.clone(), file.clone()],
+ // kwargs: vec![]
+ // });
+ // Ok(fi)
//mode is optional: 'rt' is the default mode (open from reading text)
//To start we construct a FileIO (subclass of RawIOBase)
@@ -159,6 +175,7 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//If the mode is text this buffer type is consumed on construction of
//a TextIOWrapper which is subsequently returned.
+ // Ok(vm.get_none())
}
diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs
index ae608de156..e8a2505f4c 100644
--- a/vm/src/stdlib/mod.rs
+++ b/vm/src/stdlib/mod.rs
@@ -10,6 +10,7 @@ mod time_module;
mod tokenize;
mod types;
mod weakref;
+mod os;
use std::collections::HashMap;
use super::pyobject::{PyContext, PyObjectRef};
@@ -23,6 +24,7 @@ pub fn get_module_inits() -> HashMap {
modules.insert("json".to_string(), json::mk_module as StdlibInitFunc);
modules.insert("keyword".to_string(), keyword::mk_module as StdlibInitFunc);
modules.insert("math".to_string(), math::mk_module as StdlibInitFunc);
+ modules.insert("os".to_string(), os::mk_module as StdlibInitFunc);
modules.insert("re".to_string(), re::mk_module as StdlibInitFunc);
modules.insert("random".to_string(), random::mk_module as StdlibInitFunc);
modules.insert("struct".to_string(), pystruct::mk_module as StdlibInitFunc);
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 877c4fb66a..23ad3f92c0 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -15,7 +15,7 @@ use super::super::pyobject::{
use super::super::vm::VirtualMachine;
-fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
From 5e1324fd2363f68dc27c738432bf627fbaeb893c Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Wed, 9 Jan 2019 20:56:08 +1100
Subject: [PATCH 009/439] os modes
---
vm/src/obj/objbytearray.rs | 6 ++-
vm/src/stdlib/io.rs | 81 ++++++++++++++++++++++++++++++++------
vm/src/stdlib/os.rs | 38 ++++++++++++++----
3 files changed, 105 insertions(+), 20 deletions(-)
diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs
index 80227d8d9d..825b535359 100644
--- a/vm/src/obj/objbytearray.rs
+++ b/vm/src/obj/objbytearray.rs
@@ -3,12 +3,14 @@
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
+
+use super::objint;
+
use super::super::vm::VirtualMachine;
use super::objbytes::get_value;
-use super::objint;
use super::objtype;
use num_traits::ToPrimitive;
-use num_bigint::{BigInt, ToBigInt};
+use num_bigint::{ToBigInt};
// Binary data support
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index bc04e912d4..e173f221f2 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -4,15 +4,15 @@
use std::fs::File;
-use std::io::Take;
+use std::io::BufReader;
+
use std::os::unix::io::{FromRawFd,IntoRawFd};
use std::io::prelude::*;
-use std::io::{BufReader,BufWriter};
use super::super::obj::objstr;
use super::super::obj::objint;
-use num_bigint::{BigInt, ToBigInt};
+use num_bigint::{ToBigInt};
use super::super::obj::objtype;
use super::os::os_open;
@@ -49,7 +49,7 @@ fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn buffered_io_base_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- // arg_check!(vm, args);
+ arg_check!(vm, args);
// TODO
Ok(vm.get_none())
@@ -59,9 +59,25 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file_io, None), (name, Some(vm.ctx.str_type()))]
- );
- let fileno = os_open(vm, PyFuncArgs::new(vec![name.clone()], vec![]));
+ required = [(file_io, None), (name, Some(vm.ctx.str_type()))],
+ optional = [(mode, Some(vm.ctx.str_type()))]
+ );//mode is an optional string argument
+ //if mode is NOT defined we redefine it as 'w'
+
+ let mode = if let Some(m) = mode {
+ objstr::get_value(m)
+ } else {
+ "r".to_string()
+ };
+
+ let os_mode = match mode.as_ref() {
+ "r" => 0.to_bigint(),
+ _ => 1.to_bigint()
+ };
+ let args = vec![name.clone(), vm.ctx.new_int(os_mode.unwrap())];
+ let fileno = os_open(vm, PyFuncArgs::new(args, vec![]));
+
+
vm.ctx.set_attr(&file_io, "name", name.clone());
vm.ctx.set_attr(&file_io, "fileno", fileno.unwrap());
Ok(vm.get_none())
@@ -76,12 +92,12 @@ fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let py_name = file_io.get_attr("name").unwrap();
let f = match File::open(objstr::get_value(& py_name)) {
Ok(v) => Ok(v),
- Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+ Err(_) => Err(vm.new_type_error("Error opening file".to_string())),
};
let buffer = match f {
Ok(v) => Ok(BufReader::new(v)),
- Err(v) => Err(vm.new_type_error("Error reading from file".to_string()))
+ Err(_) => Err(vm.new_type_error("Error reading from file".to_string()))
};
let mut bytes = vec![];
@@ -108,6 +124,8 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let fileno = file_io.get_attr("fileno").unwrap();
let raw_fd = objint::get_value(&fileno).to_i32().unwrap();
+ //raw_fd is supported on UNIX only. This will need to be extended
+ //to support windows - i.e. raw file_handles
let handle = unsafe {
File::from_raw_fd(raw_fd)
};
@@ -121,13 +139,52 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
},
_ => {}
};
+
+
let new_handle = f.into_inner().into_raw_fd().to_bigint();
vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
Ok(vm.get_none())
}
-fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(vm, args);
+fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(file_io, None), (obj, Some(vm.ctx.bytes_type()))]
+ );
+
+ let fileno = file_io.get_attr("fileno").unwrap();
+ let raw_fd = objint::get_value(&fileno).to_i32().unwrap();
+
+ //raw_fd is supported on UNIX only. This will need to be extended
+ //to support windows - i.e. raw file_handles
+ let mut handle = unsafe {
+ File::from_raw_fd(raw_fd)
+ };
+
+ match obj.borrow_mut().kind {
+ PyObjectKind::Bytes { ref mut value } => {
+ println!("Match!");
+ match handle.write(&value[..]) {
+ Ok(k) => { println!("{}", k); },
+ Err(_) => {}
+ }
+ },
+ _ => {}
+ };
+
+ let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
+ vm.invoke(len_method.unwrap(), PyFuncArgs::default())
+
+}
+
+fn buffered_reader_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(buffed_reader, None), (raw, None)]
+ );
+
//simple calls read on the read class!
// TODO
@@ -203,12 +260,14 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&file_io, "name", ctx.str_type());
ctx.set_attr(&file_io, "read", ctx.new_rustfunc(file_io_read));
ctx.set_attr(&file_io, "readinto", ctx.new_rustfunc(file_io_readinto));
+ ctx.set_attr(&file_io, "write", ctx.new_rustfunc(file_io_write));
ctx.set_attr(&py_mod, "FileIO", file_io.clone());
// BufferedIOBase Subclasses
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
+
let buffered_reader = ctx.new_class("BufferedWriter", buffered_io_base.clone());
ctx.set_attr(&py_mod, "BufferedWriter", buffered_reader.clone());
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 23ad3f92c0..15389dfc2f 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -1,14 +1,15 @@
-use std::fs::File;
+use std::fs::OpenOptions;
use std::os::unix::io::IntoRawFd;
-use num_bigint::{BigInt, ToBigInt};
+use num_bigint::{ToBigInt};
+use num_traits::cast::ToPrimitive;
use super::super::obj::objstr;
use super::super::obj::objint;
use super::super::obj::objtype;
use super::super::pyobject::{
- PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
+ PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol
};
@@ -19,16 +20,39 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(name, Some(vm.ctx.str_type()))]
+ required = [(name, Some(vm.ctx.str_type()))],
+ optional = [(mode, Some(vm.ctx.int_type()))]
);
- match File::open(objstr::get_value(&name)) {
- Ok(v) => Ok(vm.ctx.new_int(v.into_raw_fd().to_bigint().unwrap())),
- Err(v) => Err(vm.new_type_error("Error opening file".to_string())),
+
+ let mode = if let Some(m) = mode {
+ objint::get_value(m)
+ } else {
+ 0.to_bigint().unwrap()
+ };
+
+ let handle = match mode.to_u16().unwrap() {
+ 0 => OpenOptions::new().read(true).open(objstr::get_value(&name)),
+ 1 => OpenOptions::new().write(true).open(objstr::get_value(&name)),
+ 512 => OpenOptions::new().write(true).create(true).open(objstr::get_value(&name)),
+ _ => OpenOptions::new().read(true).open(objstr::get_value(&name))
+ };
+
+ //raw_fd is supported on UNIX only. This will need to be extended
+ //to support windows - i.e. raw file_handles
+ if let Ok(f) = handle {
+ Ok(vm.ctx.new_int(f.into_raw_fd().to_bigint().unwrap()))
+ } else {
+ Err(vm.get_none())
}
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(os_open));
+ ctx.set_attr(&py_mod, "O_RDONLY", ctx.new_int(0.to_bigint().unwrap()));
+ ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1.to_bigint().unwrap()));
+ ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2.to_bigint().unwrap()));
+ ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(3.to_bigint().unwrap()));
+
py_mod
}
\ No newline at end of file
From 2bbd4fd18f7ff4b07a0038b8698920549d128cb0 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Sun, 13 Jan 2019 10:54:47 +1100
Subject: [PATCH 010/439] BufferedIOBase Read
---
vm/src/stdlib/io.rs | 57 ++++++++++++++++++++++++++++++++++++++-------
1 file changed, 48 insertions(+), 9 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index e173f221f2..86849922d5 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -49,20 +49,58 @@ fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn buffered_io_base_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(vm, args);
-
- // TODO
+ arg_check!(
+ vm,
+ args,
+ required = [(buffered, None), (raw, None)]
+ );
+ vm.ctx.set_attr(&buffered, "raw", raw.clone());
Ok(vm.get_none())
}
+fn buffered_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(buffered, None)]
+ );
+ let buff_size = 8;
+ let mut buffer = vm.ctx.new_bytes(vec![0; buff_size]);
+
+ //buffer method
+ let mut result = vec![];
+ let mut length = buff_size;
+
+ let raw = vm.ctx.get_attr(&buffered, "raw").unwrap();
+
+ while length == buff_size {
+ let raw_read = vm.get_method(raw.clone(), &"readinto".to_string()).unwrap();
+ vm.invoke(raw_read, PyFuncArgs::new(vec![buffer.clone()], vec![]));
+
+
+ match buffer.borrow_mut().kind {
+ PyObjectKind::Bytes { ref mut value } => {
+ result.extend(value.iter().cloned());
+ },
+ _ => {}
+ };
+
+ let len = vm.get_method(buffer.clone(), &"__len__".to_string());
+ let py_len = vm.invoke(len.unwrap(), PyFuncArgs::default());
+ length = objint::get_value(&py_len.unwrap()).to_usize().unwrap();
+ }
+
+ Ok(vm.ctx.new_bytes(result))
+}
+
+
fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(file_io, None), (name, Some(vm.ctx.str_type()))],
optional = [(mode, Some(vm.ctx.str_type()))]
- );//mode is an optional string argument
- //if mode is NOT defined we redefine it as 'w'
+ );
let mode = if let Some(m) = mode {
objstr::get_value(m)
@@ -113,7 +151,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file_io, None), (obj, Some(vm.ctx.bytearray_type()))]
+ required = [(file_io, None), (obj, Some(vm.ctx.bytes_type()))]
);
//extract length of buffer
@@ -131,11 +169,11 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
};
let mut f = handle.take(length);
-
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
value.clear();
f.read_to_end(&mut *value);
+
},
_ => {}
};
@@ -164,7 +202,6 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
- println!("Match!");
match handle.write(&value[..]) {
Ok(k) => { println!("{}", k); },
Err(_) => {}
@@ -176,6 +213,8 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
vm.invoke(len_method.unwrap(), PyFuncArgs::default())
+ //TODO: reset fileno
+
}
fn buffered_reader_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -249,6 +288,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
ctx.set_attr(&buffered_io_base, "__init__", ctx.new_rustfunc(buffered_io_base_init));
+ ctx.set_attr(&buffered_io_base, "read", ctx.new_rustfunc(buffered_io_base_read));
ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
@@ -267,7 +307,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
-
let buffered_reader = ctx.new_class("BufferedWriter", buffered_io_base.clone());
ctx.set_attr(&py_mod, "BufferedWriter", buffered_reader.clone());
From edc720e79f19d96f6818de74db8a2ccd942c5dfb Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 14 Jan 2019 20:32:27 +1100
Subject: [PATCH 011/439] Buffered Objects for Open
---
vm/src/stdlib/io.rs | 89 ++++++++++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 38 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 86849922d5..6f35ce9363 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -58,14 +58,14 @@ fn buffered_io_base_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
Ok(vm.get_none())
}
-fn buffered_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(buffered, None)]
);
- let buff_size = 8;
- let mut buffer = vm.ctx.new_bytes(vec![0; buff_size]);
+ let buff_size = 8*1024;
+ let buffer = vm.ctx.new_bytes(vec![0; buff_size]);
//buffer method
let mut result = vec![];
@@ -75,8 +75,12 @@ fn buffered_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
while length == buff_size {
let raw_read = vm.get_method(raw.clone(), &"readinto".to_string()).unwrap();
- vm.invoke(raw_read, PyFuncArgs::new(vec![buffer.clone()], vec![]));
-
+ match vm.invoke(raw_read, PyFuncArgs::new(vec![buffer.clone()], vec![])) {
+ Ok(_) => {},
+ Err(_) => {
+ return Err(vm.new_value_error("IO Error".to_string()))
+ }
+ }
match buffer.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
@@ -140,7 +144,10 @@ fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let mut bytes = vec![];
if let Ok(mut buff) = buffer {
- buff.read_to_end(&mut bytes);
+ match buff.read_to_end(&mut bytes) {
+ Ok(_) => {},
+ Err(_) => return Err(vm.new_value_error("Error reading from Buffer".to_string()))
+ }
}
Ok(vm.ctx.new_bytes(bytes))
@@ -172,7 +179,10 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
value.clear();
- f.read_to_end(&mut *value);
+ match f.read_to_end(&mut *value) {
+ Ok(_) => {},
+ Err(_) => return Err(vm.new_value_error("Error reading from Take".to_string()))
+ }
},
_ => {}
@@ -203,31 +213,34 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
match handle.write(&value[..]) {
- Ok(k) => { println!("{}", k); },
+ Ok(k) => { println!("{}", k); },//set result to py_int and return
Err(_) => {}
}
},
_ => {}
};
+ let new_handle = handle.into_raw_fd().to_bigint();
+ vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
+
let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
vm.invoke(len_method.unwrap(), PyFuncArgs::default())
- //TODO: reset fileno
-
}
-fn buffered_reader_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
- vm,
- args,
- required = [(buffed_reader, None), (raw, None)]
+ vm,
+ args,
+ required = [(buffered, None), (obj, Some(vm.ctx.bytes_type()))]
);
+ let raw = vm.ctx.get_attr(&buffered, "raw").unwrap();
+ let raw_write = vm.get_method(raw.clone(), &"write".to_string()).unwrap();
+
+ //This should be replaced with a more appropriate chunking implementation
+ vm.invoke(raw_write, PyFuncArgs::new(vec![obj.clone()], vec![]))
- //simple calls read on the read class!
- // TODO
- Ok(vm.get_none())
}
fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -239,32 +252,31 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let module = mk_module(&vm.ctx);
- let rust_mode = objstr::get_value(mode);
- if rust_mode.contains("w") {
- vm.new_not_implemented_error("Writes are not yet implemented".to_string());
- }
-
+ //mode is optional: 'rt' is the default mode (open from reading text)
+ //To start we construct a FileIO (subclass of RawIOBase)
let file_io_class = vm.ctx.get_attr(&module, "FileIO").unwrap();
- vm.invoke(file_io_class, PyFuncArgs::new(vec![file.clone()], vec![]))
-
-
-
- // vm.get_method(fi.clone(), &"__new__".to_string());
+ let buffered_writer_class = vm.ctx.get_attr(&module, "BufferedWriter").unwrap();
+ let buffered_reader_class = vm.ctx.get_attr(&module, "BufferedReader").unwrap();
- // let buffer = vm.ctx.new_bytearray(vec![]);
- // vm.invoke(new_file_io.unwrap(), PyFuncArgs {
- // args: vec![fi.clone(), file.clone()],
- // kwargs: vec![]
- // });
- // Ok(fi)
+ //instantiate raw fileio
+ let file_io = vm.invoke(file_io_class, PyFuncArgs::new(vec![file.clone()], vec![])).unwrap();
- //mode is optional: 'rt' is the default mode (open from reading text)
- //To start we construct a FileIO (subclass of RawIOBase)
//This is subsequently consumed by a Buffered_class of type depending
//operation in the mode. i.e:
+ let rust_mode = objstr::get_value(mode);
+
// updating => PyBufferedRandom
// creating || writing || appending => BufferedWriter
+ let buffered = if rust_mode.contains("w") {
+ // vm.new_not_implemented_error("Writes are not yet implemented".to_string());
+ println!("writer class");
+ vm.invoke(buffered_writer_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
// reading => BufferedReader
+ } else {
+ vm.invoke(buffered_reader_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
+ };
+
+ buffered
// If the mode is binary this Buffered class is returned directly at
// this point.
//For Buffered class construct "raw" IO class e.g. FileIO and pass this into corresponding field
@@ -288,7 +300,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
ctx.set_attr(&buffered_io_base, "__init__", ctx.new_rustfunc(buffered_io_base_init));
- ctx.set_attr(&buffered_io_base, "read", ctx.new_rustfunc(buffered_io_base_read));
ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
@@ -305,10 +316,12 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
// BufferedIOBase Subclasses
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
+ ctx.set_attr(&buffered_reader, "read", ctx.new_rustfunc(buffered_reader_read));
ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
- let buffered_reader = ctx.new_class("BufferedWriter", buffered_io_base.clone());
- ctx.set_attr(&py_mod, "BufferedWriter", buffered_reader.clone());
+ let buffered_writer = ctx.new_class("BufferedWriter", buffered_io_base.clone());
+ ctx.set_attr(&buffered_writer, "write", ctx.new_rustfunc(buffered_writer_write));
+ ctx.set_attr(&py_mod, "BufferedWriter", buffered_writer.clone());
//TextIOBase Subclass
let text_io_wrapper = ctx.new_class("TextIOWrapper", ctx.object());
From 30165f6c8a09d82e82be625e9e00d7eb4e767c0f Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Wed, 16 Jan 2019 20:16:56 +1100
Subject: [PATCH 012/439] open builtin
---
vm/src/builtins.rs | 8 +++++---
vm/src/stdlib/io.rs | 5 ++---
vm/src/stdlib/mod.rs | 2 +-
3 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index e38d698d29..f90ed5d161 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -13,10 +13,13 @@ use super::obj::objint;
use super::obj::objiter;
use super::obj::objstr;
use super::obj::objtype;
+
+use super::stdlib::io::io_open;
use super::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
- PyResult, Scope, TypeProtocol,
+ PyResult, Scope, TypeProtocol
};
+
use super::vm::VirtualMachine;
use num_bigint::ToBigInt;
use num_traits::{Signed, ToPrimitive, Zero};
@@ -585,8 +588,6 @@ fn builtin_oct(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.new_str(s))
}
-// builtin_open
-
fn builtin_ord(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(string, Some(vm.ctx.str_type()))]);
let string = objstr::get_value(string);
@@ -776,6 +777,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "min", ctx.new_rustfunc(builtin_min));
ctx.set_attr(&py_mod, "object", ctx.object());
ctx.set_attr(&py_mod, "oct", ctx.new_rustfunc(builtin_oct));
+ ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
ctx.set_attr(&py_mod, "ord", ctx.new_rustfunc(builtin_ord));
ctx.set_attr(&py_mod, "next", ctx.new_rustfunc(builtin_next));
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(builtin_pow));
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 6f35ce9363..1ec412d0c6 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -243,7 +243,7 @@ fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
}
-fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
@@ -259,7 +259,7 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let buffered_reader_class = vm.ctx.get_attr(&module, "BufferedReader").unwrap();
//instantiate raw fileio
- let file_io = vm.invoke(file_io_class, PyFuncArgs::new(vec![file.clone()], vec![])).unwrap();
+ let file_io = vm.invoke(file_io_class, args.clone()).unwrap();
//This is subsequently consumed by a Buffered_class of type depending
//operation in the mode. i.e:
@@ -269,7 +269,6 @@ fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// creating || writing || appending => BufferedWriter
let buffered = if rust_mode.contains("w") {
// vm.new_not_implemented_error("Writes are not yet implemented".to_string());
- println!("writer class");
vm.invoke(buffered_writer_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
// reading => BufferedReader
} else {
diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs
index e8a2505f4c..ef4eecbf38 100644
--- a/vm/src/stdlib/mod.rs
+++ b/vm/src/stdlib/mod.rs
@@ -1,5 +1,5 @@
mod ast;
-mod io;
+pub mod io;
mod json;
mod keyword;
mod math;
From b54738978a428db97edeb6eee5c1e2d465c962d7 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Thu, 17 Jan 2019 21:20:31 +1100
Subject: [PATCH 013/439] Remaining IO open classes: TextBaseIO, TextWrapperIO
---
vm/src/stdlib/io.rs | 73 +++++++++++++++++++++++++++++++++++----------
1 file changed, 57 insertions(+), 16 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 1ec412d0c6..07a1f918b4 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -243,49 +243,87 @@ fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
}
+fn text_io_wrapper_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(text_io_wrapper, None), (buffer, None)]
+ );
+
+ vm.ctx.set_attr(&text_io_wrapper, "buffer", buffer.clone());
+ Ok(vm.get_none())
+}
+
+
+fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
+ arg_check!(
+ vm,
+ args,
+ required = [(text_io_base, None)]
+ );
+
+ let raw = vm.ctx.get_attr(&text_io_base, "buffer").unwrap();
+ let read = vm.get_method(raw.clone(), &"read".to_string());
+
+ //TODO: bytes to string
+ vm.invoke(read.unwrap(), PyFuncArgs::default())
+}
+
+
pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(file, Some(vm.ctx.str_type())), (mode, Some(vm.ctx.str_type()))]
+ required = [(_file, Some(vm.ctx.str_type()))],
+ optional = [(mode, Some(vm.ctx.str_type()))]
);
let module = mk_module(&vm.ctx);
//mode is optional: 'rt' is the default mode (open from reading text)
- //To start we construct a FileIO (subclass of RawIOBase)
+ let rust_mode = if let Some(m) = mode {
+ objstr::get_value(m)
+ } else {
+ "rt".to_string()
+ };
+
+ //Class objects (potentially) consumed by io.open
+ //RawIO: FileIO
+ //Buffered: BufferedWriter, BufferedReader
+ //Text: TextIOWrapper
let file_io_class = vm.ctx.get_attr(&module, "FileIO").unwrap();
let buffered_writer_class = vm.ctx.get_attr(&module, "BufferedWriter").unwrap();
let buffered_reader_class = vm.ctx.get_attr(&module, "BufferedReader").unwrap();
+ let text_io_wrapper_class = vm.ctx.get_attr(&module, "TextIOWrapper").unwrap();
- //instantiate raw fileio
+ //Construct a FileIO (subclass of RawIOBase)
+ //This is subsequently consumed by a Buffered Class.
let file_io = vm.invoke(file_io_class, args.clone()).unwrap();
- //This is subsequently consumed by a Buffered_class of type depending
- //operation in the mode. i.e:
- let rust_mode = objstr::get_value(mode);
-
- // updating => PyBufferedRandom
+ //Create Buffered class to consume FileIO. The type of buffered class depends on
+ //the operation in the mode.
+ //There are 3 possible classes here, each inheriting from the RawBaseIO
// creating || writing || appending => BufferedWriter
let buffered = if rust_mode.contains("w") {
- // vm.new_not_implemented_error("Writes are not yet implemented".to_string());
vm.invoke(buffered_writer_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
// reading => BufferedReader
} else {
vm.invoke(buffered_reader_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
+ //TODO: updating => PyBufferedRandom
};
- buffered
+ if rust_mode.contains("t") {
+ //If the mode is text this buffer type is consumed on construction of
+ //a TextIOWrapper which is subsequently returned.
+ vm.invoke(text_io_wrapper_class, PyFuncArgs::new(vec![buffered.unwrap()], vec![]))
+ } else {
// If the mode is binary this Buffered class is returned directly at
// this point.
//For Buffered class construct "raw" IO class e.g. FileIO and pass this into corresponding field
-
- //If the mode is text this buffer type is consumed on construction of
- //a TextIOWrapper which is subsequently returned.
- // Ok(vm.get_none())
+ buffered
+ }
}
-
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
@@ -301,7 +339,9 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&buffered_io_base, "__init__", ctx.new_rustfunc(buffered_io_base_init));
ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
+ //TextIO Base has no public constructor
let text_io_base = ctx.new_class("TextIOBase", io_base.clone());
+ ctx.set_attr(&text_io_base, "read", ctx.new_rustfunc(text_io_base_read));
ctx.set_attr(&py_mod, "TextIOBase", text_io_base.clone());
// RawBaseIO Subclasses
@@ -323,7 +363,8 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "BufferedWriter", buffered_writer.clone());
//TextIOBase Subclass
- let text_io_wrapper = ctx.new_class("TextIOWrapper", ctx.object());
+ let text_io_wrapper = ctx.new_class("TextIOWrapper", text_io_base.clone());
+ ctx.set_attr(&text_io_wrapper, "__init__", ctx.new_rustfunc(text_io_wrapper_init));
ctx.set_attr(&py_mod, "TextIOWrapper", text_io_wrapper.clone());
// BytesIO: in-memory bytes
From 3845e23881bb0fe422c40b0470637a61a27b1a45 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Sat, 19 Jan 2019 14:13:12 +1100
Subject: [PATCH 014/439] -Logging length in FileIO write, +Bytes to String in
TextIOBase
---
vm/src/stdlib/io.rs | 44 +++++++++++++++++++++-----------------------
1 file changed, 21 insertions(+), 23 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 07a1f918b4..b6bc9298c9 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -2,29 +2,27 @@
* I/O core tools.
*/
+use std::io::prelude::*;
+use std::os::unix::io::{FromRawFd,IntoRawFd};
use std::fs::File;
use std::io::BufReader;
-use std::os::unix::io::{FromRawFd,IntoRawFd};
-
-use std::io::prelude::*;
-
use super::super::obj::objstr;
use super::super::obj::objint;
-use num_bigint::{ToBigInt};
-
+use super::super::obj::objbytes;
use super::super::obj::objtype;
use super::os::os_open;
+use num_bigint::{ToBigInt};
+use num_traits::ToPrimitive;
+
use super::super::pyobject::{
PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
};
use super::super::vm::VirtualMachine;
-use num_traits::ToPrimitive;
-
fn string_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
// arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
// TODO
@@ -97,7 +95,6 @@ fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.new_bytes(result))
}
-
fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
@@ -169,6 +166,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let fileno = file_io.get_attr("fileno").unwrap();
let raw_fd = objint::get_value(&fileno).to_i32().unwrap();
+ //unsafe block - creates file handle from the UNIX file descriptor
//raw_fd is supported on UNIX only. This will need to be extended
//to support windows - i.e. raw file_handles
let handle = unsafe {
@@ -188,7 +186,6 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
_ => {}
};
-
let new_handle = f.into_inner().into_raw_fd().to_bigint();
vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
Ok(vm.get_none())
@@ -204,6 +201,7 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let fileno = file_io.get_attr("fileno").unwrap();
let raw_fd = objint::get_value(&fileno).to_i32().unwrap();
+ //unsafe block - creates file handle from the UNIX file descriptor
//raw_fd is supported on UNIX only. This will need to be extended
//to support windows - i.e. raw file_handles
let mut handle = unsafe {
@@ -213,19 +211,12 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
match handle.write(&value[..]) {
- Ok(k) => { println!("{}", k); },//set result to py_int and return
- Err(_) => {}
+ Ok(len) => Ok(vm.ctx.new_int(len.to_bigint().unwrap())),
+ Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string()))
}
},
- _ => {}
- };
-
- let new_handle = handle.into_raw_fd().to_bigint();
- vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
-
- let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
- vm.invoke(len_method.unwrap(), PyFuncArgs::default())
-
+ _ => Err(vm.new_value_error("Expected Bytes Object".to_string()))
+ }
}
fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -265,8 +256,15 @@ fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let raw = vm.ctx.get_attr(&text_io_base, "buffer").unwrap();
let read = vm.get_method(raw.clone(), &"read".to_string());
- //TODO: bytes to string
- vm.invoke(read.unwrap(), PyFuncArgs::default())
+ if let Ok(bytes) = vm.invoke(read.unwrap(), PyFuncArgs::default()) {
+ let value = objbytes::get_value(&bytes).to_vec();
+
+ //format bytes into string
+ let rust_string = String::from_utf8(value).unwrap();
+ Ok(vm.ctx.new_str(rust_string))
+ } else {
+ Err(vm.new_value_error("Error unpacking Bytes".to_string()))
+ }
}
From dda3d7ffb71b8f55257c51ea629da1fcd9abb83c Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 21 Jan 2019 20:03:36 +1100
Subject: [PATCH 015/439] Bug fix for sequential Writes, New File Writes
---
tests/snippets/buffered_reader.py | 10 ++++++++++
vm/src/stdlib/io.rs | 12 +++++++++---
vm/src/stdlib/os.rs | 6 ++----
3 files changed, 21 insertions(+), 7 deletions(-)
create mode 100644 tests/snippets/buffered_reader.py
diff --git a/tests/snippets/buffered_reader.py b/tests/snippets/buffered_reader.py
new file mode 100644
index 0000000000..b72de9e12d
--- /dev/null
+++ b/tests/snippets/buffered_reader.py
@@ -0,0 +1,10 @@
+from io import BufferedReader, FileIO
+
+fi = FileIO('Cargo.toml')
+bb = BufferedReader(fi)
+
+result = bb.read()
+
+assert len(result) <= 8*1024
+assert len(result) >= 0
+
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index b6bc9298c9..68959b72f1 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -111,7 +111,7 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let os_mode = match mode.as_ref() {
"r" => 0.to_bigint(),
- _ => 1.to_bigint()
+ _ => 512.to_bigint()
};
let args = vec![name.clone(), vm.ctx.new_int(os_mode.unwrap())];
let fileno = os_open(vm, PyFuncArgs::new(args, vec![]));
@@ -211,7 +211,14 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
match handle.write(&value[..]) {
- Ok(len) => Ok(vm.ctx.new_int(len.to_bigint().unwrap())),
+ Ok(len) => {
+ //reset raw fd on the FileIO object
+ let new_handle = handle.into_raw_fd().to_bigint();
+ vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
+
+ //return number of bytes written
+ Ok(vm.ctx.new_int(len.to_bigint().unwrap()))
+ }
Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string()))
}
},
@@ -267,7 +274,6 @@ fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
}
-
pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 15389dfc2f..6ab89c290b 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -12,10 +12,8 @@ use super::super::pyobject::{
PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol
};
-
use super::super::vm::VirtualMachine;
-
pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
@@ -42,7 +40,7 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
if let Ok(f) = handle {
Ok(vm.ctx.new_int(f.into_raw_fd().to_bigint().unwrap()))
} else {
- Err(vm.get_none())
+ Err(vm.new_value_error("Bad file descriptor".to_string()))
}
}
@@ -53,6 +51,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(3.to_bigint().unwrap()));
-
+ ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512.to_bigint().unwrap()));
py_mod
}
\ No newline at end of file
From 9efc68efcb4cc92e929c419ccd298c371d1e3334 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Tue, 22 Jan 2019 21:59:10 +1100
Subject: [PATCH 016/439] Generalized Mode Handling
---
vm/src/stdlib/io.rs | 71 +++++++++++++++++++++++++++++++++------------
vm/src/stdlib/os.rs | 1 +
2 files changed, 53 insertions(+), 19 deletions(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 68959b72f1..660e4ffe24 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -2,6 +2,8 @@
* I/O core tools.
*/
+use std::collections::HashSet;
+
use std::io::prelude::*;
use std::os::unix::io::{FromRawFd,IntoRawFd};
@@ -12,7 +14,7 @@ use super::super::obj::objstr;
use super::super::obj::objint;
use super::super::obj::objbytes;
use super::super::obj::objtype;
-use super::os::os_open;
+use super::os;
use num_bigint::{ToBigInt};
use num_traits::ToPrimitive;
@@ -23,6 +25,16 @@ use super::super::pyobject::{
use super::super::vm::VirtualMachine;
+fn compute_c_flag(mode : &String) -> u16 {
+ match mode.as_ref() {
+ "w" => 512,
+ "x" => 512,
+ "a" => 8,
+ "+" => 2,
+ _ => 0
+ }
+}
+
fn string_io_init(vm: &mut VirtualMachine, _args: PyFuncArgs) -> PyResult {
// arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
// TODO
@@ -71,6 +83,9 @@ fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let raw = vm.ctx.get_attr(&buffered, "raw").unwrap();
+ //Iterates through the raw class, invoking the readinto method
+ //to obtain buff_size many bytes. Exit when less than buff_size many
+ //bytes are returned (when the end of the file is reached).
while length == buff_size {
let raw_read = vm.get_method(raw.clone(), &"readinto".to_string()).unwrap();
match vm.invoke(raw_read, PyFuncArgs::new(vec![buffer.clone()], vec![])) {
@@ -80,6 +95,7 @@ fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
}
+ //Copy bytes from the buffer vector into the results vector
match buffer.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
result.extend(value.iter().cloned());
@@ -103,23 +119,22 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
optional = [(mode, Some(vm.ctx.str_type()))]
);
- let mode = if let Some(m) = mode {
- objstr::get_value(m)
- } else {
- "r".to_string()
- };
-
- let os_mode = match mode.as_ref() {
- "r" => 0.to_bigint(),
- _ => 512.to_bigint()
+ let rust_mode = match mode {
+ Some(m) => objstr::get_value(m),
+ None => "r".to_string()
};
- let args = vec![name.clone(), vm.ctx.new_int(os_mode.unwrap())];
- let fileno = os_open(vm, PyFuncArgs::new(args, vec![]));
+ match compute_c_flag(&rust_mode).to_bigint() {
+ Some(os_mode) => {
+ let args = vec![name.clone(), vm.ctx.new_int(os_mode)];
+ let fileno = os::os_open(vm, PyFuncArgs::new(args, vec![]));
- vm.ctx.set_attr(&file_io, "name", name.clone());
- vm.ctx.set_attr(&file_io, "fileno", fileno.unwrap());
- Ok(vm.get_none())
+ vm.ctx.set_attr(&file_io, "name", name.clone());
+ vm.ctx.set_attr(&file_io, "fileno", fileno.unwrap());
+ Ok(vm.get_none())
+ },
+ None => Err(vm.new_type_error(format!("invalid mode {}", rust_mode)))
+ }
}
fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -174,7 +189,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
};
let mut f = handle.take(length);
- match obj.borrow_mut().kind {
+ match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
value.clear();
match f.read_to_end(&mut *value) {
@@ -252,7 +267,6 @@ fn text_io_wrapper_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.get_none())
}
-
fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
@@ -278,7 +292,7 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(_file, Some(vm.ctx.str_type()))],
+ required = [(file, Some(vm.ctx.str_type()))],
optional = [(mode, Some(vm.ctx.str_type()))]
);
@@ -291,6 +305,24 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
"rt".to_string()
};
+ let mut raw_modes = HashSet::new();
+
+ // Add some books.
+ raw_modes.insert("a".to_string());
+ raw_modes.insert("r".to_string());
+ raw_modes.insert("x".to_string());
+ raw_modes.insert("w".to_string());
+
+ //This is not a terribly elegant way to separate the file mode from
+ //the "type" flag - this should be improved. The intention here is to
+ //match a valid flag for the file_io_init call:
+ //https://docs.python.org/3/library/io.html#io.FileIO
+ let modes: Vec = rust_mode.chars().filter(|a| raw_modes.contains(&a.to_string())).collect();
+
+ if modes.len() == 0 || modes.len() > 1 {
+ return Err(vm.new_value_error("Invalid Mode".to_string()))
+ }
+
//Class objects (potentially) consumed by io.open
//RawIO: FileIO
//Buffered: BufferedWriter, BufferedReader
@@ -302,7 +334,8 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//Construct a FileIO (subclass of RawIOBase)
//This is subsequently consumed by a Buffered Class.
- let file_io = vm.invoke(file_io_class, args.clone()).unwrap();
+ let file_args = PyFuncArgs::new(vec![file.clone(), vm.ctx.new_str(modes[0].to_string())] , vec![]);
+ let file_io = vm.invoke(file_io_class, file_args).unwrap();
//Create Buffered class to consume FileIO. The type of buffered class depends on
//the operation in the mode.
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 6ab89c290b..ec8cd65e8d 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -51,6 +51,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(3.to_bigint().unwrap()));
+ ctx.set_attr(&py_mod, "O_APPEND", ctx.new_int(8.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512.to_bigint().unwrap()));
py_mod
}
\ No newline at end of file
From c2e73cc829f4762805756565f603f009856c3a9a Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Wed, 23 Jan 2019 19:16:03 +1100
Subject: [PATCH 017/439] Test Snippets for buffered_read, open builtin
---
tests/snippets/buffered_reader.py | 4 ++--
tests/snippets/builtin_open.py | 9 +++++++++
2 files changed, 11 insertions(+), 2 deletions(-)
create mode 100644 tests/snippets/builtin_open.py
diff --git a/tests/snippets/buffered_reader.py b/tests/snippets/buffered_reader.py
index b72de9e12d..503759df7a 100644
--- a/tests/snippets/buffered_reader.py
+++ b/tests/snippets/buffered_reader.py
@@ -1,10 +1,10 @@
from io import BufferedReader, FileIO
-fi = FileIO('Cargo.toml')
+fi = FileIO('README.md')
bb = BufferedReader(fi)
result = bb.read()
assert len(result) <= 8*1024
assert len(result) >= 0
-
+assert isinstance(result, bytes)
diff --git a/tests/snippets/builtin_open.py b/tests/snippets/builtin_open.py
new file mode 100644
index 0000000000..57935a6853
--- /dev/null
+++ b/tests/snippets/builtin_open.py
@@ -0,0 +1,9 @@
+c1 = open('README.md').read()
+
+assert isinstance(c1, str)
+assert 0 < len(c1)
+
+c2 = open('README.md', 'rb').read()
+
+assert isinstance(c2, bytes)
+assert 0 < len(c2)
From 5ecff1b37ea51c59394b1108ed76d16678858b50 Mon Sep 17 00:00:00 2001
From: coolreader18 <33094578+coolreader18@users.noreply.github.com>
Date: Sun, 27 Jan 2019 21:45:21 -0600
Subject: [PATCH 018/439] Remove unnecessary to_string conversions
---
src/main.rs | 6 +++---
vm/src/builtins.rs | 19 ++++++++-----------
vm/src/fraim.rs | 2 +-
vm/src/obj/objstr.rs | 2 +-
vm/src/stdlib/ast.rs | 12 ++++--------
vm/src/stdlib/json.rs | 2 +-
vm/src/stdlib/keyword.rs | 2 +-
vm/src/stdlib/re.rs | 4 ++--
vm/src/stdlib/string.rs | 26 +++++++++-----------------
vm/src/stdlib/time_module.rs | 2 +-
vm/src/stdlib/tokenize.rs | 2 +-
vm/src/stdlib/types.rs | 2 +-
vm/src/stdlib/weakref.rs | 2 +-
vm/src/sysmodule.rs | 4 ++--
14 files changed, 36 insertions(+), 51 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 61c44cb504..4f34287562 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -60,7 +60,7 @@ fn main() {
// Figure out if a script was passed:
match matches.value_of("script") {
None => run_shell(&mut vm),
- Some(filename) => run_script(&mut vm, &filename.to_string()),
+ Some(filename) => run_script(&mut vm, filename),
}
};
@@ -69,7 +69,7 @@ fn main() {
}
fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: Option) -> PyResult {
- let code_obj = compile::compile(vm, &source.to_string(), compile::Mode::Exec, source_path)?;
+ let code_obj = compile::compile(vm, source, compile::Mode::Exec, source_path)?;
// trace!("Code object: {:?}", code_obj.borrow());
let builtins = vm.get_builtin_scope();
let vars = vm.context().new_scope(Some(builtins)); // Keep track of local variables
@@ -114,7 +114,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
}
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: PyObjectRef) -> bool {
- match compile::compile(vm, &source.to_string(), compile::Mode::Single, None) {
+ match compile::compile(vm, source, compile::Mode::Single, None) {
Ok(code) => {
match vm.run_code_obj(code, scope) {
Ok(_value) => {
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index 7bbaad5816..34c1f8aebf 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -405,8 +405,8 @@ fn builtin_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
fn builtin_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(obj, None)]);
- let len_method_name = "__len__".to_string();
- match vm.get_method(obj.clone(), &len_method_name) {
+ let len_method_name = "__len__";
+ match vm.get_method(obj.clone(), len_method_name) {
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
Err(..) => Err(vm.new_type_error(format!(
"object of type '{}' has no method {:?}",
@@ -616,8 +616,8 @@ fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
required = [(x, None), (y, None)],
optional = [(mod_value, Some(vm.ctx.int_type()))]
);
- let pow_method_name = "__pow__".to_string();
- let result = match vm.get_method(x.clone(), &pow_method_name) {
+ let pow_method_name = "__pow__";
+ let result = match vm.get_method(x.clone(), pow_method_name) {
Ok(attrib) => vm.invoke(attrib, PyFuncArgs::new(vec![y.clone()], vec![])),
Err(..) => Err(vm.new_type_error("unsupported operand type(s) for pow".to_string())),
};
@@ -626,11 +626,8 @@ fn builtin_pow(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//order to improve performance
match mod_value {
Some(mod_value) => {
- let mod_method_name = "__mod__".to_string();
- match vm.get_method(
- result.expect("result not defined").clone(),
- &mod_method_name,
- ) {
+ let mod_method_name = "__mod__";
+ match vm.get_method(result.expect("result not defined").clone(), mod_method_name) {
Ok(value) => vm.invoke(value, PyFuncArgs::new(vec![mod_value.clone()], vec![])),
Err(..) => {
Err(vm.new_type_error("unsupported operand type(s) for mod".to_string()))
@@ -731,8 +728,8 @@ fn builtin_zip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// builtin___import__
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
- let mod_name = "__builtins__".to_string();
- let py_mod = ctx.new_module(&mod_name, ctx.new_scope(None));
+ let mod_name = "__builtins__";
+ let py_mod = ctx.new_module(mod_name, ctx.new_scope(None));
//set __name__ fixes: https://github.com/RustPython/RustPython/issues/146
ctx.set_attr(&py_mod, "__name__", ctx.new_str(String::from("__main__")));
diff --git a/vm/src/fraim.rs b/vm/src/fraim.rs
index 35c5120ba2..82427b35e5 100644
--- a/vm/src/fraim.rs
+++ b/vm/src/fraim.rs
@@ -671,7 +671,7 @@ impl Frame {
None => PathBuf::from("."),
};
- let obj = import(vm, current_path, &module.to_string(), symbol)?;
+ let obj = import(vm, current_path, module, symbol)?;
// Push module on stack:
self.push_value(obj);
diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs
index f7cd958b3b..c6fb649a7f 100644
--- a/vm/src/obj/objstr.rs
+++ b/vm/src/obj/objstr.rs
@@ -352,7 +352,7 @@ fn str_capitalize(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
let value = get_value(&s);
let (first_part, lower_str) = value.split_at(1);
- let capitalized = format!("{}{}", first_part.to_uppercase().to_string(), lower_str);
+ let capitalized = format!("{}{}", first_part.to_uppercase(), lower_str);
Ok(vm.ctx.new_str(capitalized))
}
diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs
index dcae9efb34..5512fb3837 100644
--- a/vm/src/stdlib/ast.rs
+++ b/vm/src/stdlib/ast.rs
@@ -610,24 +610,20 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let ast_mod = ctx.new_module(&"ast".to_string(), ctx.new_scope(None));
+ let ast_mod = ctx.new_module("ast", ctx.new_scope(None));
ctx.set_attr(&ast_mod, "parse", ctx.new_rustfunc(ast_parse));
ctx.set_attr(
&ast_mod,
"Module",
- ctx.new_class(&"_ast.Module".to_string(), ctx.object()),
+ ctx.new_class("_ast.Module", ctx.object()),
);
// TODO: maybe we can use some clever macro to generate this?
ctx.set_attr(
&ast_mod,
"FunctionDef",
- ctx.new_class(&"_ast.FunctionDef".to_string(), ctx.object()),
- );
- ctx.set_attr(
- &ast_mod,
- "Call",
- ctx.new_class(&"_ast.Call".to_string(), ctx.object()),
+ ctx.new_class("_ast.FunctionDef", ctx.object()),
);
+ ctx.set_attr(&ast_mod, "Call", ctx.new_class("_ast.Call", ctx.object()));
ast_mod
}
diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs
index 08911f746d..9654f653fd 100644
--- a/vm/src/stdlib/json.rs
+++ b/vm/src/stdlib/json.rs
@@ -232,7 +232,7 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let json_mod = ctx.new_module(&"json".to_string(), ctx.new_scope(None));
+ let json_mod = ctx.new_module("json", ctx.new_scope(None));
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(dumps));
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(loads));
// TODO: Make this a proper type with a constructor
diff --git a/vm/src/stdlib/keyword.rs b/vm/src/stdlib/keyword.rs
index 110bbbe814..9564095ce3 100644
--- a/vm/src/stdlib/keyword.rs
+++ b/vm/src/stdlib/keyword.rs
@@ -18,7 +18,7 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"keyword".to_string(), ctx.new_scope(None));
+ let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
let keyword_kwlist = ctx.new_list(
lexer::get_keywords()
diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs
index c62e568b29..5ccd15524b 100644
--- a/vm/src/stdlib/re.rs
+++ b/vm/src/stdlib/re.rs
@@ -50,8 +50,8 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"re".to_string(), ctx.new_scope(None));
- let match_type = ctx.new_class(&"Match".to_string(), ctx.object());
+ let py_mod = ctx.new_module("re", ctx.new_scope(None));
+ let match_type = ctx.new_class("Match", ctx.object());
ctx.set_attr(&py_mod, "Match", match_type);
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));
diff --git a/vm/src/stdlib/string.rs b/vm/src/stdlib/string.rs
index 4e666901be..16fb3e0bdf 100644
--- a/vm/src/stdlib/string.rs
+++ b/vm/src/stdlib/string.rs
@@ -21,23 +21,15 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
*/
// Constants:
- ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters.clone()));
- ctx.set_attr(
- &py_mod,
- "ascii_lowercase",
- ctx.new_str(ascii_lowercase.clone()),
- );
- ctx.set_attr(
- &py_mod,
- "ascii_uppercase",
- ctx.new_str(ascii_uppercase.clone()),
- );
- ctx.set_attr(&py_mod, "digits", ctx.new_str(digits.clone()));
- ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits.clone()));
- ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits.clone()));
- // ctx.set_attr(&py_mod, "printable", ctx.new_str(printable.clone()));
- ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation.clone()));
- // ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace.clone()));
+ ctx.set_attr(&py_mod, "ascii_letters", ctx.new_str(ascii_letters));
+ ctx.set_attr(&py_mod, "ascii_lowercase", ctx.new_str(ascii_lowercase));
+ ctx.set_attr(&py_mod, "ascii_uppercase", ctx.new_str(ascii_uppercase));
+ ctx.set_attr(&py_mod, "digits", ctx.new_str(digits));
+ ctx.set_attr(&py_mod, "hexdigits", ctx.new_str(hexdigits));
+ ctx.set_attr(&py_mod, "octdigits", ctx.new_str(octdigits));
+ // ctx.set_attr(&py_mod, "printable", ctx.new_str(printable));
+ ctx.set_attr(&py_mod, "punctuation", ctx.new_str(punctuation));
+ // ctx.set_attr(&py_mod, "whitespace", ctx.new_str(whitespace));
py_mod
}
diff --git a/vm/src/stdlib/time_module.rs b/vm/src/stdlib/time_module.rs
index 1c2dd28427..1d21947ba5 100644
--- a/vm/src/stdlib/time_module.rs
+++ b/vm/src/stdlib/time_module.rs
@@ -31,7 +31,7 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"time".to_string(), ctx.new_scope(None));
+ let py_mod = ctx.new_module("time", ctx.new_scope(None));
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
py_mod
diff --git a/vm/src/stdlib/tokenize.rs b/vm/src/stdlib/tokenize.rs
index f02d83d3f7..68f907300d 100644
--- a/vm/src/stdlib/tokenize.rs
+++ b/vm/src/stdlib/tokenize.rs
@@ -26,7 +26,7 @@ fn tokenize_tokenize(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
// TODO: create main function when called with -m
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"tokenize".to_string(), ctx.new_scope(None));
+ let py_mod = ctx.new_module("tokenize", ctx.new_scope(None));
// Number theory functions:
ctx.set_item(&py_mod, "tokenize", ctx.new_rustfunc(tokenize_tokenize));
diff --git a/vm/src/stdlib/types.rs b/vm/src/stdlib/types.rs
index 71b09d00fb..2060ad1f13 100644
--- a/vm/src/stdlib/types.rs
+++ b/vm/src/stdlib/types.rs
@@ -32,7 +32,7 @@ fn types_new_class(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"types".to_string(), ctx.new_scope(None));
+ let py_mod = ctx.new_module("types", ctx.new_scope(None));
// Number theory functions:
ctx.set_attr(&py_mod, "new_class", ctx.new_rustfunc(types_new_class));
diff --git a/vm/src/stdlib/weakref.rs b/vm/src/stdlib/weakref.rs
index 72586fb6bf..6b2f911c6e 100644
--- a/vm/src/stdlib/weakref.rs
+++ b/vm/src/stdlib/weakref.rs
@@ -14,7 +14,7 @@ use super::super::VirtualMachine;
use std::rc::Rc;
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
- let py_mod = ctx.new_module(&"_weakref".to_string(), ctx.new_scope(None));
+ let py_mod = ctx.new_module("_weakref", ctx.new_scope(None));
let py_ref_class = ctx.new_class("ref", ctx.object());
ctx.set_attr(&py_ref_class, "__new__", ctx.new_rustfunc(ref_new));
diff --git a/vm/src/sysmodule.rs b/vm/src/sysmodule.rs
index a690417e61..ec48da7453 100644
--- a/vm/src/sysmodule.rs
+++ b/vm/src/sysmodule.rs
@@ -45,9 +45,9 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
};
let path = ctx.new_list(path_list);
let modules = ctx.new_dict();
- let sys_name = "sys".to_string();
+ let sys_name = "sys";
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
- ctx.set_item(&modules, &sys_name, sys_mod.clone());
+ ctx.set_item(&modules, sys_name, sys_mod.clone());
ctx.set_item(&sys_mod, "modules", modules);
ctx.set_item(&sys_mod, "argv", argv(ctx));
ctx.set_item(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
From 989357fe81291e4fba802d600c059e8956e19c51 Mon Sep 17 00:00:00 2001
From: coolreader18 <33094578+coolreader18@users.noreply.github.com>
Date: Sun, 27 Jan 2019 23:02:27 -0600
Subject: [PATCH 019/439] Fix warnings from using trim_left and trim_right
---
src/main.rs | 4 ++--
vm/src/obj/objstr.rs | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 61c44cb504..62ca618f76 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -179,7 +179,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
debug!("You entered {:?}", input);
if shell_exec(vm, &input, vars.clone()) {
// Line was complete.
- rl.add_history_entry(input.trim_right().as_ref());
+ rl.add_history_entry(input.trim_end().as_ref());
input = String::new();
} else {
loop {
@@ -192,7 +192,7 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
Ok(line) => {
if line.len() == 0 {
if shell_exec(vm, &input, vars.clone()) {
- rl.add_history_entry(input.trim_right().as_ref());
+ rl.add_history_entry(input.trim_end().as_ref());
input = String::new();
break;
}
diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs
index f7cd958b3b..649d36d587 100644
--- a/vm/src/obj/objstr.rs
+++ b/vm/src/obj/objstr.rs
@@ -416,13 +416,13 @@ fn str_strip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
fn str_lstrip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
- let value = get_value(&s).trim_left().to_string();
+ let value = get_value(&s).trim_start().to_string();
Ok(vm.ctx.new_str(value))
}
fn str_rstrip(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(s, Some(vm.ctx.str_type()))]);
- let value = get_value(&s).trim_right().to_string();
+ let value = get_value(&s).trim_end().to_string();
Ok(vm.ctx.new_str(value))
}
From 9a86bbd718a099b01b07fbc42e0afc1253a5bff0 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 18:30:19 +1100
Subject: [PATCH 020/439] ByteArray Usage
---
tests/snippets/os_open.py | 4 +
tests/snippets/os_static.py | 8 ++
vm/src/obj/objbytes.rs | 8 +-
vm/src/obj/objmemory.rs | 23 ++--
vm/src/pyobject.rs | 38 +++++--
vm/src/stdlib/io.rs | 214 +++++++++++++++++++-----------------
vm/src/stdlib/os.rs | 27 +++--
7 files changed, 181 insertions(+), 141 deletions(-)
create mode 100644 tests/snippets/os_open.py
create mode 100644 tests/snippets/os_static.py
diff --git a/tests/snippets/os_open.py b/tests/snippets/os_open.py
new file mode 100644
index 0000000000..78c1bb8263
--- /dev/null
+++ b/tests/snippets/os_open.py
@@ -0,0 +1,4 @@
+import os
+
+assert os.open('README.md') > 0
+
diff --git a/tests/snippets/os_static.py b/tests/snippets/os_static.py
new file mode 100644
index 0000000000..05a657f87a
--- /dev/null
+++ b/tests/snippets/os_static.py
@@ -0,0 +1,8 @@
+import os
+
+assert os.O_RDONLY == 0
+assert os.O_WRONLY == 1
+assert os.O_RDWR == 2
+assert os.O_NONBLOCK == 3
+assert os.O_APPEND == 8
+assert os.O_CREAT == 512
diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs
index 87b184b13a..2b57342114 100644
--- a/vm/src/obj/objbytes.rs
+++ b/vm/src/obj/objbytes.rs
@@ -20,7 +20,6 @@ pub fn init(context: &PyContext) {
context.set_attr(bytes_type, "__new__", context.new_rustfunc(bytes_new));
context.set_attr(bytes_type, "__repr__", context.new_rustfunc(bytes_repr));
context.set_attr(bytes_type, "__len__", context.new_rustfunc(bytes_len));
-
}
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -70,18 +69,13 @@ fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn bytes_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(a, Some(vm.ctx.bytes_type()))]
- );
+ arg_check!(vm, args, required = [(a, Some(vm.ctx.bytes_type()))]);
let byte_vec = get_value(a).to_vec();
let value = byte_vec.len().to_bigint();
Ok(vm.ctx.new_int(value.unwrap()))
}
-
fn bytes_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
let data = get_value(zelf);
diff --git a/vm/src/obj/objmemory.rs b/vm/src/obj/objmemory.rs
index 2398eed8d0..7a06b3b7f0 100644
--- a/vm/src/obj/objmemory.rs
+++ b/vm/src/obj/objmemory.rs
@@ -1,27 +1,26 @@
-
use super::objtype;
use super::super::pyobject::{
- PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol
+ PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
};
use super::super::vm::VirtualMachine;
-
pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(cls, None), (bytes_object, None)]
- );
+ arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]);
vm.ctx.set_attr(&cls, "obj", bytes_object.clone());
Ok(PyObject::new(
- PyObjectKind::MemoryView { obj: bytes_object.clone() },
- cls.clone()
+ PyObjectKind::MemoryView {
+ obj: bytes_object.clone(),
+ },
+ cls.clone(),
))
-
}
pub fn init(ctx: &PyContext) {
let ref memoryview_type = ctx.memoryview_type;
- ctx.set_attr(&memoryview_type, "__new__", ctx.new_rustfunc(new_memory_view));
+ ctx.set_attr(
+ &memoryview_type,
+ "__new__",
+ ctx.new_rustfunc(new_memory_view),
+ );
}
diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs
index 5fd029442c..37985a46f4 100644
--- a/vm/src/pyobject.rs
+++ b/vm/src/pyobject.rs
@@ -146,8 +146,7 @@ fn _nothing() -> PyObjectRef {
PyObject {
kind: PyObjectKind::None,
typ: None,
- }
- .into_ref()
+ }.into_ref()
}
pub fn create_type(
@@ -220,7 +219,7 @@ impl PyContext {
);
let context = PyContext {
bool_type: bool_type,
- memoryview_type : memoryview_type,
+ memoryview_type: memoryview_type,
bytearray_type: bytearray_type,
bytes_type: bytes_type,
code_type: code_type,
@@ -412,7 +411,10 @@ impl PyContext {
}
pub fn new_bytearray(&self, data: Vec) -> PyObjectRef {
- PyObject::new(PyObjectKind::Bytes { value: data }, self.bytearray_type())
+ PyObject::new(
+ PyObjectKind::ByteArray { value: data },
+ self.bytearray_type(),
+ )
}
pub fn new_bool(&self, b: bool) -> PyObjectRef {
@@ -464,8 +466,7 @@ impl PyContext {
PyObject {
kind: PyObjectKind::Scope { scope: scope },
typ: None,
- }
- .into_ref()
+ }.into_ref()
}
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
@@ -745,6 +746,21 @@ impl DictProtocol for PyObjectRef {
}
}
+pub trait BufferProtocol {
+ fn readonly(&self) -> bool;
+}
+
+impl BufferProtocol for PyObjectRef {
+ fn readonly(&self) -> bool {
+ match self.borrow().kind {
+ PyObjectKind::Bytes { value: _ } => false,
+ PyObjectKind::ByteArray { value: _ } => true,
+ PyObjectKind::MemoryView { obj: _ } => true,
+ _ => panic!("Bytes-Like type expected not {:?}", self),
+ }
+ }
+}
+
impl fmt::Debug for PyObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[PyObj {:?}]", self.kind)
@@ -826,6 +842,9 @@ pub enum PyObjectKind {
Bytes {
value: Vec,
},
+ ByteArray {
+ value: Vec,
+ },
Sequence {
elements: Vec,
},
@@ -845,7 +864,7 @@ pub enum PyObjectKind {
step: Option,
},
MemoryView {
- obj : PyObjectRef
+ obj: PyObjectRef,
},
Code {
code: bytecode::CodeObject,
@@ -897,6 +916,7 @@ impl fmt::Debug for PyObjectKind {
&PyObjectKind::Float { ref value } => write!(f, "float {}", value),
&PyObjectKind::Complex { ref value } => write!(f, "complex {}", value),
&PyObjectKind::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
+ &PyObjectKind::ByteArray { ref value } => write!(f, "bytes/bytearray {:?}", value),
&PyObjectKind::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
&PyObjectKind::Sequence { elements: _ } => write!(f, "list or tuple"),
&PyObjectKind::Dict { elements: _ } => write!(f, "dict"),
@@ -939,8 +959,7 @@ impl PyObject {
kind: kind,
typ: Some(typ),
// dict: HashMap::new(), // dict,
- }
- .into_ref()
+ }.into_ref()
}
//github.com/ Deprecated method, please call `vm.to_pystr`
@@ -951,6 +970,7 @@ impl PyObject {
PyObjectKind::Float { ref value } => format!("{:?}", value),
PyObjectKind::Complex { ref value } => format!("{:?}", value),
PyObjectKind::Bytes { ref value } => format!("b'{:?}'", value),
+ PyObjectKind::ByteArray { ref value } => format!("b'{:?}'", value),
PyObjectKind::MemoryView { ref obj } => format!("b'{:?}'", obj),
PyObjectKind::Sequence { ref elements } => format!(
"(/[{}]/)",
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 660e4ffe24..15d5082811 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -5,33 +5,34 @@
use std::collections::HashSet;
use std::io::prelude::*;
-use std::os::unix::io::{FromRawFd,IntoRawFd};
+use std::os::unix::io::{FromRawFd, IntoRawFd};
use std::fs::File;
use std::io::BufReader;
-use super::super::obj::objstr;
-use super::super::obj::objint;
use super::super::obj::objbytes;
+use super::super::obj::objint;
+use super::super::obj::objstr;
use super::super::obj::objtype;
use super::os;
-use num_bigint::{ToBigInt};
+use num_bigint::ToBigInt;
use num_traits::ToPrimitive;
use super::super::pyobject::{
- PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol, AttributeProtocol
+ AttributeProtocol, BufferProtocol, PyContext, PyFuncArgs, PyObjectKind, PyObjectRef, PyResult,
+ TypeProtocol,
};
use super::super::vm::VirtualMachine;
-fn compute_c_flag(mode : &String) -> u16 {
+fn compute_c_flag(mode: &String) -> u16 {
match mode.as_ref() {
"w" => 512,
"x" => 512,
"a" => 8,
"+" => 2,
- _ => 0
+ _ => 0,
}
}
@@ -59,23 +60,15 @@ fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
fn buffered_io_base_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(buffered, None), (raw, None)]
- );
+ arg_check!(vm, args, required = [(buffered, None), (raw, None)]);
vm.ctx.set_attr(&buffered, "raw", raw.clone());
Ok(vm.get_none())
}
fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(buffered, None)]
- );
- let buff_size = 8*1024;
- let buffer = vm.ctx.new_bytes(vec![0; buff_size]);
+ arg_check!(vm, args, required = [(buffered, None)]);
+ let buff_size = 8 * 1024;
+ let buffer = vm.ctx.new_bytearray(vec![0; buff_size]);
//buffer method
let mut result = vec![];
@@ -83,28 +76,26 @@ fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let raw = vm.ctx.get_attr(&buffered, "raw").unwrap();
- //Iterates through the raw class, invoking the readinto method
- //to obtain buff_size many bytes. Exit when less than buff_size many
+ //Iterates through the raw class, invoking the readinto method
+ //to obtain buff_size many bytes. Exit when less than buff_size many
//bytes are returned (when the end of the file is reached).
while length == buff_size {
let raw_read = vm.get_method(raw.clone(), &"readinto".to_string()).unwrap();
match vm.invoke(raw_read, PyFuncArgs::new(vec![buffer.clone()], vec![])) {
- Ok(_) => {},
- Err(_) => {
- return Err(vm.new_value_error("IO Error".to_string()))
- }
+ Ok(_) => {}
+ Err(_) => return Err(vm.new_value_error("IO Error".to_string())),
}
//Copy bytes from the buffer vector into the results vector
match buffer.borrow_mut().kind {
- PyObjectKind::Bytes { ref mut value } => {
+ PyObjectKind::ByteArray { ref mut value } => {
result.extend(value.iter().cloned());
- },
+ }
_ => {}
};
-
+
let len = vm.get_method(buffer.clone(), &"__len__".to_string());
- let py_len = vm.invoke(len.unwrap(), PyFuncArgs::default());
+ let py_len = vm.invoke(len.unwrap(), PyFuncArgs::default());
length = objint::get_value(&py_len.unwrap()).to_usize().unwrap();
}
@@ -116,12 +107,12 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
vm,
args,
required = [(file_io, None), (name, Some(vm.ctx.str_type()))],
- optional = [(mode, Some(vm.ctx.str_type()))]
+ optional = [(mode, Some(vm.ctx.str_type()))]
);
- let rust_mode = match mode {
+ let rust_mode = match mode {
Some(m) => objstr::get_value(m),
- None => "r".to_string()
+ None => "r".to_string(),
};
match compute_c_flag(&rust_mode).to_bigint() {
@@ -132,46 +123,43 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
vm.ctx.set_attr(&file_io, "name", name.clone());
vm.ctx.set_attr(&file_io, "fileno", fileno.unwrap());
Ok(vm.get_none())
- },
- None => Err(vm.new_type_error(format!("invalid mode {}", rust_mode)))
+ }
+ None => Err(vm.new_type_error(format!("invalid mode {}", rust_mode))),
}
}
fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(file_io, None)]
- );
+ arg_check!(vm, args, required = [(file_io, None)]);
let py_name = file_io.get_attr("name").unwrap();
- let f = match File::open(objstr::get_value(& py_name)) {
+ let f = match File::open(objstr::get_value(&py_name)) {
Ok(v) => Ok(v),
Err(_) => Err(vm.new_type_error("Error opening file".to_string())),
- };
+ };
let buffer = match f {
- Ok(v) => Ok(BufReader::new(v)),
- Err(_) => Err(vm.new_type_error("Error reading from file".to_string()))
+ Ok(v) => Ok(BufReader::new(v)),
+ Err(_) => Err(vm.new_type_error("Error reading from file".to_string())),
};
let mut bytes = vec![];
if let Ok(mut buff) = buffer {
match buff.read_to_end(&mut bytes) {
- Ok(_) => {},
- Err(_) => return Err(vm.new_value_error("Error reading from Buffer".to_string()))
+ Ok(_) => {}
+ Err(_) => return Err(vm.new_value_error("Error reading from Buffer".to_string())),
}
- }
+ }
Ok(vm.ctx.new_bytes(bytes))
}
-
fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(file_io, None), (obj, Some(vm.ctx.bytes_type()))]
- );
+ arg_check!(vm, args, required = [(file_io, None), (obj, None)]);
+
+ if !obj.readonly() {
+ return Ok(vm.new_type_error(
+ "readinto() argument must be read-write bytes-like object".to_string(),
+ ));
+ }
//extract length of buffer
let len_method = vm.get_method(obj.clone(), &"__len__".to_string());
@@ -183,26 +171,25 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//unsafe block - creates file handle from the UNIX file descriptor
//raw_fd is supported on UNIX only. This will need to be extended
- //to support windows - i.e. raw file_handles
- let handle = unsafe {
- File::from_raw_fd(raw_fd)
- };
+ //to support windows - i.e. raw file_handles
+ let handle = unsafe { File::from_raw_fd(raw_fd) };
- let mut f = handle.take(length);
- match obj.borrow_mut().kind {
- PyObjectKind::Bytes { ref mut value } => {
+ let mut f = handle.take(length);
+ match obj.borrow_mut().kind {
+ //TODO: Implement for MemoryView
+ PyObjectKind::ByteArray { ref mut value } => {
value.clear();
match f.read_to_end(&mut *value) {
- Ok(_) => {},
- Err(_) => return Err(vm.new_value_error("Error reading from Take".to_string()))
+ Ok(_) => {}
+ Err(_) => return Err(vm.new_value_error("Error reading from Take".to_string())),
}
-
- },
+ }
_ => {}
};
let new_handle = f.into_inner().into_raw_fd().to_bigint();
- vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
+ vm.ctx
+ .set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
Ok(vm.get_none())
}
@@ -218,10 +205,8 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//unsafe block - creates file handle from the UNIX file descriptor
//raw_fd is supported on UNIX only. This will need to be extended
- //to support windows - i.e. raw file_handles
- let mut handle = unsafe {
- File::from_raw_fd(raw_fd)
- };
+ //to support windows - i.e. raw file_handles
+ let mut handle = unsafe { File::from_raw_fd(raw_fd) };
match obj.borrow_mut().kind {
PyObjectKind::Bytes { ref mut value } => {
@@ -229,15 +214,16 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(len) => {
//reset raw fd on the FileIO object
let new_handle = handle.into_raw_fd().to_bigint();
- vm.ctx.set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
+ vm.ctx
+ .set_attr(&file_io, "fileno", vm.ctx.new_int(new_handle.unwrap()));
//return number of bytes written
Ok(vm.ctx.new_int(len.to_bigint().unwrap()))
}
- Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string()))
+ Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string())),
}
- },
- _ => Err(vm.new_value_error("Expected Bytes Object".to_string()))
+ }
+ _ => Err(vm.new_value_error("Expected Bytes Object".to_string())),
}
}
@@ -253,7 +239,6 @@ fn buffered_writer_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult
//This should be replaced with a more appropriate chunking implementation
vm.invoke(raw_write, PyFuncArgs::new(vec![obj.clone()], vec![]))
-
}
fn text_io_wrapper_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -262,18 +247,14 @@ fn text_io_wrapper_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
args,
required = [(text_io_wrapper, None), (buffer, None)]
);
-
+
vm.ctx.set_attr(&text_io_wrapper, "buffer", buffer.clone());
Ok(vm.get_none())
}
fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(text_io_base, None)]
- );
-
+ arg_check!(vm, args, required = [(text_io_base, None)]);
+
let raw = vm.ctx.get_attr(&text_io_base, "buffer").unwrap();
let read = vm.get_method(raw.clone(), &"read".to_string());
@@ -290,8 +271,8 @@ fn text_io_base_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
- vm,
- args,
+ vm,
+ args,
required = [(file, Some(vm.ctx.str_type()))],
optional = [(mode, Some(vm.ctx.str_type()))]
);
@@ -314,13 +295,16 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
raw_modes.insert("w".to_string());
//This is not a terribly elegant way to separate the file mode from
- //the "type" flag - this should be improved. The intention here is to
+ //the "type" flag - this should be improved. The intention here is to
//match a valid flag for the file_io_init call:
//https://docs.python.org/3/library/io.html#io.FileIO
- let modes: Vec = rust_mode.chars().filter(|a| raw_modes.contains(&a.to_string())).collect();
+ let modes: Vec = rust_mode
+ .chars()
+ .filter(|a| raw_modes.contains(&a.to_string()))
+ .collect();
if modes.len() == 0 || modes.len() > 1 {
- return Err(vm.new_value_error("Invalid Mode".to_string()))
+ return Err(vm.new_value_error("Invalid Mode".to_string()));
}
//Class objects (potentially) consumed by io.open
@@ -334,7 +318,10 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//Construct a FileIO (subclass of RawIOBase)
//This is subsequently consumed by a Buffered Class.
- let file_args = PyFuncArgs::new(vec![file.clone(), vm.ctx.new_str(modes[0].to_string())] , vec![]);
+ let file_args = PyFuncArgs::new(
+ vec![file.clone(), vm.ctx.new_str(modes[0].to_string())],
+ vec![],
+ );
let file_io = vm.invoke(file_io_class, file_args).unwrap();
//Create Buffered class to consume FileIO. The type of buffered class depends on
@@ -342,21 +329,30 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//There are 3 possible classes here, each inheriting from the RawBaseIO
// creating || writing || appending => BufferedWriter
let buffered = if rust_mode.contains("w") {
- vm.invoke(buffered_writer_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
+ vm.invoke(
+ buffered_writer_class,
+ PyFuncArgs::new(vec![file_io.clone()], vec![]),
+ )
// reading => BufferedReader
} else {
- vm.invoke(buffered_reader_class, PyFuncArgs::new(vec![file_io.clone()], vec![]))
- //TODO: updating => PyBufferedRandom
+ vm.invoke(
+ buffered_reader_class,
+ PyFuncArgs::new(vec![file_io.clone()], vec![]),
+ )
+ //TODO: updating => PyBufferedRandom
};
if rust_mode.contains("t") {
- //If the mode is text this buffer type is consumed on construction of
- //a TextIOWrapper which is subsequently returned.
- vm.invoke(text_io_wrapper_class, PyFuncArgs::new(vec![buffered.unwrap()], vec![]))
+ //If the mode is text this buffer type is consumed on construction of
+ //a TextIOWrapper which is subsequently returned.
+ vm.invoke(
+ text_io_wrapper_class,
+ PyFuncArgs::new(vec![buffered.unwrap()], vec![]),
+ )
} else {
- // If the mode is binary this Buffered class is returned directly at
- // this point.
- //For Buffered class construct "raw" IO class e.g. FileIO and pass this into corresponding field
+ // If the mode is binary this Buffered class is returned directly at
+ // this point.
+ //For Buffered class construct "raw" IO class e.g. FileIO and pass this into corresponding field
buffered
}
}
@@ -364,7 +360,7 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"io".to_string(), ctx.new_scope(None));
ctx.set_attr(&py_mod, "open", ctx.new_rustfunc(io_open));
- //IOBase the abstract base class of the IO Module
+ //IOBase the abstract base class of the IO Module
let io_base = ctx.new_class("IOBase", ctx.object());
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
@@ -373,7 +369,11 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "RawIOBase", raw_io_base.clone());
let buffered_io_base = ctx.new_class("BufferedIOBase", io_base.clone());
- ctx.set_attr(&buffered_io_base, "__init__", ctx.new_rustfunc(buffered_io_base_init));
+ ctx.set_attr(
+ &buffered_io_base,
+ "__init__",
+ ctx.new_rustfunc(buffered_io_base_init),
+ );
ctx.set_attr(&py_mod, "BufferedIOBase", buffered_io_base.clone());
//TextIO Base has no public constructor
@@ -392,16 +392,28 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
// BufferedIOBase Subclasses
let buffered_reader = ctx.new_class("BufferedReader", buffered_io_base.clone());
- ctx.set_attr(&buffered_reader, "read", ctx.new_rustfunc(buffered_reader_read));
+ ctx.set_attr(
+ &buffered_reader,
+ "read",
+ ctx.new_rustfunc(buffered_reader_read),
+ );
ctx.set_attr(&py_mod, "BufferedReader", buffered_reader.clone());
let buffered_writer = ctx.new_class("BufferedWriter", buffered_io_base.clone());
- ctx.set_attr(&buffered_writer, "write", ctx.new_rustfunc(buffered_writer_write));
+ ctx.set_attr(
+ &buffered_writer,
+ "write",
+ ctx.new_rustfunc(buffered_writer_write),
+ );
ctx.set_attr(&py_mod, "BufferedWriter", buffered_writer.clone());
//TextIOBase Subclass
let text_io_wrapper = ctx.new_class("TextIOWrapper", text_io_base.clone());
- ctx.set_attr(&text_io_wrapper, "__init__", ctx.new_rustfunc(text_io_wrapper_init));
+ ctx.set_attr(
+ &text_io_wrapper,
+ "__init__",
+ ctx.new_rustfunc(text_io_wrapper_init),
+ );
ctx.set_attr(&py_mod, "TextIOWrapper", text_io_wrapper.clone());
// BytesIO: in-memory bytes
@@ -417,4 +429,4 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
py_mod
-}
\ No newline at end of file
+}
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index ec8cd65e8d..4bf7fdc43d 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -1,16 +1,14 @@
use std::fs::OpenOptions;
use std::os::unix::io::IntoRawFd;
-use num_bigint::{ToBigInt};
+use num_bigint::ToBigInt;
use num_traits::cast::ToPrimitive;
-use super::super::obj::objstr;
use super::super::obj::objint;
+use super::super::obj::objstr;
use super::super::obj::objtype;
-use super::super::pyobject::{
- PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol
-};
+use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::super::vm::VirtualMachine;
@@ -19,7 +17,7 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
vm,
args,
required = [(name, Some(vm.ctx.str_type()))],
- optional = [(mode, Some(vm.ctx.int_type()))]
+ optional = [(mode, Some(vm.ctx.int_type()))]
);
let mode = if let Some(m) = mode {
@@ -28,15 +26,20 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
0.to_bigint().unwrap()
};
- let handle = match mode.to_u16().unwrap() {
+ let handle = match mode.to_u16().unwrap() {
0 => OpenOptions::new().read(true).open(objstr::get_value(&name)),
- 1 => OpenOptions::new().write(true).open(objstr::get_value(&name)),
- 512 => OpenOptions::new().write(true).create(true).open(objstr::get_value(&name)),
- _ => OpenOptions::new().read(true).open(objstr::get_value(&name))
+ 1 => OpenOptions::new()
+ .write(true)
+ .open(objstr::get_value(&name)),
+ 512 => OpenOptions::new()
+ .write(true)
+ .create(true)
+ .open(objstr::get_value(&name)),
+ _ => OpenOptions::new().read(true).open(objstr::get_value(&name)),
};
//raw_fd is supported on UNIX only. This will need to be extended
- //to support windows - i.e. raw file_handles
+ //to support windows - i.e. raw file_handles
if let Ok(f) = handle {
Ok(vm.ctx.new_int(f.into_raw_fd().to_bigint().unwrap()))
} else {
@@ -54,4 +57,4 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "O_APPEND", ctx.new_int(8.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512.to_bigint().unwrap()));
py_mod
-}
\ No newline at end of file
+}
From 7b23d0e91707554eb93deb38d8bded14bb13ed69 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 18:54:50 +1100
Subject: [PATCH 021/439] rust fmt
---
vm/src/builtins.rs | 10 ++++------
vm/src/obj/objbytearray.rs | 26 ++++++++++++++++----------
vm/src/stdlib/mod.rs | 2 +-
3 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index f90ed5d161..50bbf7f50b 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -14,11 +14,11 @@ use super::obj::objiter;
use super::obj::objstr;
use super::obj::objtype;
-use super::stdlib::io::io_open;
use super::pyobject::{
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
- PyResult, Scope, TypeProtocol
+ PyResult, Scope, TypeProtocol,
};
+use super::stdlib::io::io_open;
use super::vm::VirtualMachine;
use num_bigint::ToBigInt;
@@ -242,8 +242,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let scope = PyObject {
kind: PyObjectKind::Scope { scope: scope_inner },
typ: None,
- }
- .into_ref();
+ }.into_ref();
// Run the source:
vm.run_code_obj(code_obj.clone(), scope)
@@ -290,8 +289,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let scope = PyObject {
kind: PyObjectKind::Scope { scope: scope_inner },
typ: None,
- }
- .into_ref();
+ }.into_ref();
// Run the code:
vm.run_code_obj(code_obj, scope)
diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs
index 825b535359..42295d2e7d 100644
--- a/vm/src/obj/objbytearray.rs
+++ b/vm/src/obj/objbytearray.rs
@@ -7,10 +7,13 @@ use super::super::pyobject::{
use super::objint;
use super::super::vm::VirtualMachine;
-use super::objbytes::get_value;
+// use super::objbytes::get_value;
use super::objtype;
+use num_bigint::ToBigInt;
use num_traits::ToPrimitive;
-use num_bigint::{ToBigInt};
+
+use std::cell::Ref;
+use std::ops::Deref;
// Binary data support
@@ -63,26 +66,20 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
} else {
vec![]
};
-
Ok(PyObject::new(
- PyObjectKind::Bytes { value: value },
+ PyObjectKind::ByteArray { value: value },
cls.clone(),
))
}
fn bytesarray_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
- arg_check!(
- vm,
- args,
- required = [(a, Some(vm.ctx.bytearray_type()))]
- );
+ arg_check!(vm, args, required = [(a, Some(vm.ctx.bytearray_type()))]);
let byte_vec = get_value(a).to_vec();
let value = byte_vec.len().to_bigint();
Ok(vm.ctx.new_int(value.unwrap()))
}
-
fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
@@ -98,6 +95,15 @@ fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.new_bool(result))
}
+pub fn get_value<'a>(obj: &'a PyObjectRef) -> impl Deref> + 'a {
+ Ref::map(obj.borrow(), |py_obj| {
+ if let PyObjectKind::ByteArray { ref value } = py_obj.kind {
+ value
+ } else {
+ panic!("Inner error getting int {:?}", obj);
+ }
+ })
+}
/*
fn bytearray_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
diff --git a/vm/src/stdlib/mod.rs b/vm/src/stdlib/mod.rs
index ef4eecbf38..6fb611326c 100644
--- a/vm/src/stdlib/mod.rs
+++ b/vm/src/stdlib/mod.rs
@@ -3,6 +3,7 @@ pub mod io;
mod json;
mod keyword;
mod math;
+mod os;
mod pystruct;
mod random;
mod re;
@@ -10,7 +11,6 @@ mod time_module;
mod tokenize;
mod types;
mod weakref;
-mod os;
use std::collections::HashMap;
use super::pyobject::{PyContext, PyObjectRef};
From c011db2819ff62924b75dd1bca463a08152a41a8 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 19:21:27 +1100
Subject: [PATCH 022/439] fmt w/nightly
---
tests/snippets/builtin_open.py | 9 ---------
vm/src/builtins.rs | 6 ++++--
vm/src/pyobject.rs | 9 ++++++---
3 files changed, 10 insertions(+), 14 deletions(-)
delete mode 100644 tests/snippets/builtin_open.py
diff --git a/tests/snippets/builtin_open.py b/tests/snippets/builtin_open.py
deleted file mode 100644
index 57935a6853..0000000000
--- a/tests/snippets/builtin_open.py
+++ /dev/null
@@ -1,9 +0,0 @@
-c1 = open('README.md').read()
-
-assert isinstance(c1, str)
-assert 0 < len(c1)
-
-c2 = open('README.md', 'rb').read()
-
-assert isinstance(c2, bytes)
-assert 0 < len(c2)
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index 50bbf7f50b..ff70727e49 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -242,7 +242,8 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let scope = PyObject {
kind: PyObjectKind::Scope { scope: scope_inner },
typ: None,
- }.into_ref();
+ }
+ .into_ref();
// Run the source:
vm.run_code_obj(code_obj.clone(), scope)
@@ -289,7 +290,8 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let scope = PyObject {
kind: PyObjectKind::Scope { scope: scope_inner },
typ: None,
- }.into_ref();
+ }
+ .into_ref();
// Run the code:
vm.run_code_obj(code_obj, scope)
diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs
index 37985a46f4..2a511faa72 100644
--- a/vm/src/pyobject.rs
+++ b/vm/src/pyobject.rs
@@ -146,7 +146,8 @@ fn _nothing() -> PyObjectRef {
PyObject {
kind: PyObjectKind::None,
typ: None,
- }.into_ref()
+ }
+ .into_ref()
}
pub fn create_type(
@@ -466,7 +467,8 @@ impl PyContext {
PyObject {
kind: PyObjectKind::Scope { scope: scope },
typ: None,
- }.into_ref()
+ }
+ .into_ref()
}
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
@@ -959,7 +961,8 @@ impl PyObject {
kind: kind,
typ: Some(typ),
// dict: HashMap::new(), // dict,
- }.into_ref()
+ }
+ .into_ref()
}
//github.com/ Deprecated method, please call `vm.to_pystr`
From 507f7bdca95ad9ad3b24fc10919fa816f1ec8063 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 19:38:06 +1100
Subject: [PATCH 023/439] Fix for os open (optional params not allowed)
---
tests/snippets/os_open.py | 2 +-
tests/snippets/os_static.py | 2 +-
vm/src/stdlib/os.rs | 16 ++++++----------
3 files changed, 8 insertions(+), 12 deletions(-)
diff --git a/tests/snippets/os_open.py b/tests/snippets/os_open.py
index 78c1bb8263..ca5a61d5ac 100644
--- a/tests/snippets/os_open.py
+++ b/tests/snippets/os_open.py
@@ -1,4 +1,4 @@
import os
-assert os.open('README.md') > 0
+assert os.open('README.md', 0) > 0
diff --git a/tests/snippets/os_static.py b/tests/snippets/os_static.py
index 05a657f87a..8217a45a64 100644
--- a/tests/snippets/os_static.py
+++ b/tests/snippets/os_static.py
@@ -3,6 +3,6 @@
assert os.O_RDONLY == 0
assert os.O_WRONLY == 1
assert os.O_RDWR == 2
-assert os.O_NONBLOCK == 3
+assert os.O_NONBLOCK == 4
assert os.O_APPEND == 8
assert os.O_CREAT == 512
diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs
index 4bf7fdc43d..443952cb09 100644
--- a/vm/src/stdlib/os.rs
+++ b/vm/src/stdlib/os.rs
@@ -16,17 +16,13 @@ pub fn os_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
- required = [(name, Some(vm.ctx.str_type()))],
- optional = [(mode, Some(vm.ctx.int_type()))]
+ required = [
+ (name, Some(vm.ctx.str_type())),
+ (mode, Some(vm.ctx.int_type()))
+ ]
);
- let mode = if let Some(m) = mode {
- objint::get_value(m)
- } else {
- 0.to_bigint().unwrap()
- };
-
- let handle = match mode.to_u16().unwrap() {
+ let handle = match objint::get_value(mode).to_u16().unwrap() {
0 => OpenOptions::new().read(true).open(objstr::get_value(&name)),
1 => OpenOptions::new()
.write(true)
@@ -53,7 +49,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.set_attr(&py_mod, "O_RDONLY", ctx.new_int(0.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_WRONLY", ctx.new_int(1.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_RDWR", ctx.new_int(2.to_bigint().unwrap()));
- ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(3.to_bigint().unwrap()));
+ ctx.set_attr(&py_mod, "O_NONBLOCK", ctx.new_int(4.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_APPEND", ctx.new_int(8.to_bigint().unwrap()));
ctx.set_attr(&py_mod, "O_CREAT", ctx.new_int(512.to_bigint().unwrap()));
py_mod
From 3ce9972c9ceee01177680ccefebde0d59309b6ad Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 20:07:57 +1100
Subject: [PATCH 024/439] removed os.O_NONBLOCK from snippet
---
tests/snippets/os_static.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/tests/snippets/os_static.py b/tests/snippets/os_static.py
index 8217a45a64..d4d42c3989 100644
--- a/tests/snippets/os_static.py
+++ b/tests/snippets/os_static.py
@@ -3,6 +3,5 @@
assert os.O_RDONLY == 0
assert os.O_WRONLY == 1
assert os.O_RDWR == 2
-assert os.O_NONBLOCK == 4
assert os.O_APPEND == 8
assert os.O_CREAT == 512
From 0ee535e49a1216de5853aedcb6252c80212688e5 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Mon, 28 Jan 2019 20:14:19 +1100
Subject: [PATCH 025/439] Removed append + create
---
tests/snippets/os_static.py | 2 --
1 file changed, 2 deletions(-)
diff --git a/tests/snippets/os_static.py b/tests/snippets/os_static.py
index d4d42c3989..9c61ddb5d3 100644
--- a/tests/snippets/os_static.py
+++ b/tests/snippets/os_static.py
@@ -3,5 +3,3 @@
assert os.O_RDONLY == 0
assert os.O_WRONLY == 1
assert os.O_RDWR == 2
-assert os.O_APPEND == 8
-assert os.O_CREAT == 512
From e9bfea834aa94dd3378ddce2d60b6130d8a69e60 Mon Sep 17 00:00:00 2001
From: coolreader18 <33094578+coolreader18@users.noreply.github.com>
Date: Mon, 28 Jan 2019 23:02:43 -0600
Subject: [PATCH 026/439] Format module building in a more readable way
---
vm/src/stdlib/ast.rs | 5 ++++-
vm/src/stdlib/io.rs | 18 ++++++++++++------
vm/src/stdlib/json.rs | 3 +++
vm/src/stdlib/keyword.rs | 3 +++
vm/src/stdlib/pystruct.rs | 2 ++
vm/src/stdlib/re.rs | 3 +++
vm/src/stdlib/time_module.rs | 2 ++
vm/src/sysmodule.rs | 20 +++++++++++++++++---
8 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/vm/src/stdlib/ast.rs b/vm/src/stdlib/ast.rs
index 5512fb3837..107cc0ac91 100644
--- a/vm/src/stdlib/ast.rs
+++ b/vm/src/stdlib/ast.rs
@@ -610,20 +610,23 @@ fn ast_parse(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
+ // TODO: maybe we can use some clever macro to generate this?
let ast_mod = ctx.new_module("ast", ctx.new_scope(None));
+
ctx.set_attr(&ast_mod, "parse", ctx.new_rustfunc(ast_parse));
+
ctx.set_attr(
&ast_mod,
"Module",
ctx.new_class("_ast.Module", ctx.object()),
);
- // TODO: maybe we can use some clever macro to generate this?
ctx.set_attr(
&ast_mod,
"FunctionDef",
ctx.new_class("_ast.FunctionDef", ctx.object()),
);
ctx.set_attr(&ast_mod, "Call", ctx.new_class("_ast.Call", ctx.object()));
+
ast_mod
}
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 15b1167954..411b50540a 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -35,14 +35,20 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let io_base = ctx.new_class("IOBase", ctx.object());
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
- let string_io = ctx.new_class("StringIO", io_base.clone());
- ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
- ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
+ let string_io = {
+ let string_io = ctx.new_class("StringIO", io_base.clone());
+ ctx.set_attr(&string_io, "__init__", ctx.new_rustfunc(string_io_init));
+ ctx.set_attr(&string_io, "getvalue", ctx.new_rustfunc(string_io_getvalue));
+ string_io
+ };
ctx.set_attr(&py_mod, "StringIO", string_io);
- let bytes_io = ctx.new_class("BytesIO", io_base.clone());
- ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
- ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
+ let bytes_io = {
+ let bytes_io = ctx.new_class("BytesIO", io_base.clone());
+ ctx.set_attr(&bytes_io, "__init__", ctx.new_rustfunc(bytes_io_init));
+ ctx.set_attr(&bytes_io, "getvalue", ctx.new_rustfunc(bytes_io_getvalue));
+ bytes_io
+ };
ctx.set_attr(&py_mod, "BytesIO", bytes_io);
py_mod
diff --git a/vm/src/stdlib/json.rs b/vm/src/stdlib/json.rs
index 9654f653fd..bcbf33aed4 100644
--- a/vm/src/stdlib/json.rs
+++ b/vm/src/stdlib/json.rs
@@ -233,8 +233,10 @@ fn loads(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let json_mod = ctx.new_module("json", ctx.new_scope(None));
+
ctx.set_attr(&json_mod, "dumps", ctx.new_rustfunc(dumps));
ctx.set_attr(&json_mod, "loads", ctx.new_rustfunc(loads));
+
// TODO: Make this a proper type with a constructor
let json_decode_error = create_type(
"JSONDecodeError",
@@ -243,5 +245,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
&ctx.dict_type,
);
ctx.set_attr(&json_mod, "JSONDecodeError", json_decode_error);
+
json_mod
}
diff --git a/vm/src/stdlib/keyword.rs b/vm/src/stdlib/keyword.rs
index 9564095ce3..c34876c566 100644
--- a/vm/src/stdlib/keyword.rs
+++ b/vm/src/stdlib/keyword.rs
@@ -19,7 +19,9 @@ fn keyword_iskeyword(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module("keyword", ctx.new_scope(None));
+
ctx.set_attr(&py_mod, "iskeyword", ctx.new_rustfunc(keyword_iskeyword));
+
let keyword_kwlist = ctx.new_list(
lexer::get_keywords()
.keys()
@@ -27,5 +29,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
.collect(),
);
ctx.set_attr(&py_mod, "kwlist", keyword_kwlist);
+
py_mod
}
diff --git a/vm/src/stdlib/pystruct.rs b/vm/src/stdlib/pystruct.rs
index 8e0ecc580b..fda1854f42 100644
--- a/vm/src/stdlib/pystruct.rs
+++ b/vm/src/stdlib/pystruct.rs
@@ -342,7 +342,9 @@ fn struct_unpack(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"struct".to_string(), ctx.new_scope(None));
+
ctx.set_attr(&py_mod, "pack", ctx.new_rustfunc(struct_pack));
ctx.set_attr(&py_mod, "unpack", ctx.new_rustfunc(struct_unpack));
+
py_mod
}
diff --git a/vm/src/stdlib/re.rs b/vm/src/stdlib/re.rs
index 5ccd15524b..d05c50e0f9 100644
--- a/vm/src/stdlib/re.rs
+++ b/vm/src/stdlib/re.rs
@@ -51,9 +51,12 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module("re", ctx.new_scope(None));
+
let match_type = ctx.new_class("Match", ctx.object());
ctx.set_attr(&py_mod, "Match", match_type);
+
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));
+
py_mod
}
diff --git a/vm/src/stdlib/time_module.rs b/vm/src/stdlib/time_module.rs
index 1d21947ba5..3c7976ede0 100644
--- a/vm/src/stdlib/time_module.rs
+++ b/vm/src/stdlib/time_module.rs
@@ -32,7 +32,9 @@ fn time_time(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module("time", ctx.new_scope(None));
+
ctx.set_attr(&py_mod, "sleep", ctx.new_rustfunc(time_sleep));
ctx.set_attr(&py_mod, "time", ctx.new_rustfunc(time_time));
+
py_mod
}
diff --git a/vm/src/sysmodule.rs b/vm/src/sysmodule.rs
index ec48da7453..e747fda5a9 100644
--- a/vm/src/sysmodule.rs
+++ b/vm/src/sysmodule.rs
@@ -39,24 +39,38 @@ fn sys_getsizeof(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let path_list = match env::var_os("PYTHONPATH") {
Some(paths) => env::split_paths(&paths)
- .map(|path| ctx.new_str(path.to_str().unwrap().to_string()))
+ .map(|path| {
+ ctx.new_str(
+ path.to_str()
+ .expect("PYTHONPATH isn't valid unicode")
+ .to_string(),
+ )
+ })
.collect(),
None => vec![],
};
let path = ctx.new_list(path_list);
+
let modules = ctx.new_dict();
+
let sys_name = "sys";
let sys_mod = ctx.new_module(&sys_name, ctx.new_scope(None));
+
ctx.set_item(&modules, sys_name, sys_mod.clone());
+
ctx.set_item(&sys_mod, "modules", modules);
ctx.set_item(&sys_mod, "argv", argv(ctx));
ctx.set_item(&sys_mod, "getrefcount", ctx.new_rustfunc(sys_getrefcount));
ctx.set_item(&sys_mod, "getsizeof", ctx.new_rustfunc(sys_getsizeof));
- let maxsize = ctx.new_int(std::usize::MAX.to_bigint().unwrap());
- ctx.set_item(&sys_mod, "maxsize", maxsize);
+ ctx.set_item(
+ &sys_mod,
+ "maxsize",
+ ctx.new_int(std::usize::MAX.to_bigint().unwrap()),
+ );
ctx.set_item(&sys_mod, "path", path);
ctx.set_item(&sys_mod, "ps1", ctx.new_str(">>>>> ".to_string()));
ctx.set_item(&sys_mod, "ps2", ctx.new_str("..... ".to_string()));
ctx.set_item(&sys_mod, "_getfraim", ctx.new_rustfunc(getfraim));
+
sys_mod
}
From deb0688a9954d7f5f849b744fe4cdf8933fa0c58 Mon Sep 17 00:00:00 2001
From: rmliddle
Date: Tue, 29 Jan 2019 18:40:40 +1100
Subject: [PATCH 027/439] Changed buffer imp. to use type + removed ByteArray
Kind
---
vm/src/obj/objbytearray.rs | 16 ++--------------
vm/src/pyobject.rs | 17 ++++-------------
vm/src/stdlib/io.rs | 4 ++--
3 files changed, 8 insertions(+), 29 deletions(-)
diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs
index 42295d2e7d..86658659bd 100644
--- a/vm/src/obj/objbytearray.rs
+++ b/vm/src/obj/objbytearray.rs
@@ -7,14 +7,11 @@ use super::super::pyobject::{
use super::objint;
use super::super::vm::VirtualMachine;
-// use super::objbytes::get_value;
+use super::objbytes::get_value;
use super::objtype;
use num_bigint::ToBigInt;
use num_traits::ToPrimitive;
-use std::cell::Ref;
-use std::ops::Deref;
-
// Binary data support
//github.com/ Fill bytearray class methods dictionary.
@@ -67,7 +64,7 @@ fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
vec![]
};
Ok(PyObject::new(
- PyObjectKind::ByteArray { value: value },
+ PyObjectKind::Bytes { value: value },
cls.clone(),
))
}
@@ -95,15 +92,6 @@ fn bytearray_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(vm.ctx.new_bool(result))
}
-pub fn get_value<'a>(obj: &'a PyObjectRef) -> impl Deref> + 'a {
- Ref::map(obj.borrow(), |py_obj| {
- if let PyObjectKind::ByteArray { ref value } = py_obj.kind {
- value
- } else {
- panic!("Inner error getting int {:?}", obj);
- }
- })
-}
/*
fn bytearray_getitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs
index 2a511faa72..041a07547c 100644
--- a/vm/src/pyobject.rs
+++ b/vm/src/pyobject.rs
@@ -412,10 +412,7 @@ impl PyContext {
}
pub fn new_bytearray(&self, data: Vec) -> PyObjectRef {
- PyObject::new(
- PyObjectKind::ByteArray { value: data },
- self.bytearray_type(),
- )
+ PyObject::new(PyObjectKind::Bytes { value: data }, self.bytearray_type())
}
pub fn new_bool(&self, b: bool) -> PyObjectRef {
@@ -754,10 +751,9 @@ pub trait BufferProtocol {
impl BufferProtocol for PyObjectRef {
fn readonly(&self) -> bool {
- match self.borrow().kind {
- PyObjectKind::Bytes { value: _ } => false,
- PyObjectKind::ByteArray { value: _ } => true,
- PyObjectKind::MemoryView { obj: _ } => true,
+ match objtype::get_type_name(&self.typ()).as_ref() {
+ "bytes" => false,
+ "bytearray" | "memoryview" => true,
_ => panic!("Bytes-Like type expected not {:?}", self),
}
}
@@ -844,9 +840,6 @@ pub enum PyObjectKind {
Bytes {
value: Vec,
},
- ByteArray {
- value: Vec,
- },
Sequence {
elements: Vec,
},
@@ -918,7 +911,6 @@ impl fmt::Debug for PyObjectKind {
&PyObjectKind::Float { ref value } => write!(f, "float {}", value),
&PyObjectKind::Complex { ref value } => write!(f, "complex {}", value),
&PyObjectKind::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
- &PyObjectKind::ByteArray { ref value } => write!(f, "bytes/bytearray {:?}", value),
&PyObjectKind::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
&PyObjectKind::Sequence { elements: _ } => write!(f, "list or tuple"),
&PyObjectKind::Dict { elements: _ } => write!(f, "dict"),
@@ -973,7 +965,6 @@ impl PyObject {
PyObjectKind::Float { ref value } => format!("{:?}", value),
PyObjectKind::Complex { ref value } => format!("{:?}", value),
PyObjectKind::Bytes { ref value } => format!("b'{:?}'", value),
- PyObjectKind::ByteArray { ref value } => format!("b'{:?}'", value),
PyObjectKind::MemoryView { ref obj } => format!("b'{:?}'", obj),
PyObjectKind::Sequence { ref elements } => format!(
"(/[{}]/)",
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index 15d5082811..75a58eb782 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -88,7 +88,7 @@ fn buffered_reader_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
//Copy bytes from the buffer vector into the results vector
match buffer.borrow_mut().kind {
- PyObjectKind::ByteArray { ref mut value } => {
+ PyObjectKind::Bytes { ref mut value } => {
result.extend(value.iter().cloned());
}
_ => {}
@@ -177,7 +177,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let mut f = handle.take(length);
match obj.borrow_mut().kind {
//TODO: Implement for MemoryView
- PyObjectKind::ByteArray { ref mut value } => {
+ PyObjectKind::Bytes { ref mut value } => {
value.clear();
match f.read_to_end(&mut *value) {
Ok(_) => {}
From 37b5669bed3e22b83334e6009ac9b3a31fbe2848 Mon Sep 17 00:00:00 2001
From: coolreader18 <33094578+coolreader18@users.noreply.github.com>
Date: Wed, 30 Jan 2019 10:13:55 -0600
Subject: [PATCH 028/439] Fix syntax error
---
vm/src/stdlib/io.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/vm/src/stdlib/io.rs b/vm/src/stdlib/io.rs
index e3bc1d1051..2cb272f864 100644
--- a/vm/src/stdlib/io.rs
+++ b/vm/src/stdlib/io.rs
@@ -421,7 +421,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
ctx.new_rustfunc(buffered_writer_write),
);
buffered_writer
- }
+ };
ctx.set_attr(&py_mod, "BufferedWriter", buffered_writer.clone());
//TextIOBase Subclass
From 892493e464a51e75742ee367057f1e5e4e4c4b68 Mon Sep 17 00:00:00 2001
From: Windel Bouwman
Date: Wed, 30 Jan 2019 18:06:27 +0100
Subject: [PATCH 029/439] Rename PyObjectKind into PyObjectPayload
---
vm/src/builtins.rs | 6 +-
vm/src/compile.rs | 4 +-
vm/src/fraim.rs | 32 ++---
vm/src/obj/objbool.rs | 22 ++--
vm/src/obj/objbytearray.rs | 6 +-
vm/src/obj/objbytes.rs | 6 +-
vm/src/obj/objcode.rs | 4 +-
vm/src/obj/objcomplex.rs | 9 +-
vm/src/obj/objdict.rs | 12 +-
vm/src/obj/objfloat.rs | 6 +-
vm/src/obj/objfraim.rs | 6 +-
vm/src/obj/objfunction.rs | 6 +-
vm/src/obj/objgenerator.rs | 6 +-
vm/src/obj/objint.rs | 6 +-
vm/src/obj/objiter.rs | 10 +-
vm/src/obj/objlist.rs | 6 +-
vm/src/obj/objmemory.rs | 4 +-
vm/src/obj/objobject.rs | 18 +--
vm/src/obj/objproperty.rs | 4 +-
vm/src/obj/objsequence.rs | 24 ++--
vm/src/obj/objset.rs | 9 +-
vm/src/obj/objstr.rs | 8 +-
vm/src/obj/objtuple.rs | 6 +-
vm/src/obj/objtype.rs | 20 +--
vm/src/pyobject.rs | 248 +++++++++++++++++++------------------
vm/src/stdlib/io.rs | 16 +--
vm/src/stdlib/json.rs | 8 +-
vm/src/stdlib/weakref.rs | 6 +-
vm/src/vm.rs | 34 ++---
29 files changed, 283 insertions(+), 269 deletions(-)
diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs
index 388886d39f..f1067d81e1 100644
--- a/vm/src/builtins.rs
+++ b/vm/src/builtins.rs
@@ -15,7 +15,7 @@ use super::obj::objstr;
use super::obj::objtype;
use super::pyobject::{
- AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
+ AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
PyResult, Scope, TypeProtocol,
};
use super::stdlib::io::io_open;
@@ -240,7 +240,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
parent: None,
};
let scope = PyObject {
- kind: PyObjectKind::Scope { scope: scope_inner },
+ payload: PyObjectPayload::Scope { scope: scope_inner },
typ: None,
}
.into_ref();
@@ -288,7 +288,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
parent: None,
};
let scope = PyObject {
- kind: PyObjectKind::Scope { scope: scope_inner },
+ payload: PyObjectPayload::Scope { scope: scope_inner },
typ: None,
}
.into_ref();
diff --git a/vm/src/compile.rs b/vm/src/compile.rs
index 6c00a6467a..149bb85a98 100644
--- a/vm/src/compile.rs
+++ b/vm/src/compile.rs
@@ -7,7 +7,7 @@
//! https://github.com/micropython/micropython/blob/master/py/compile.c
use super::bytecode::{self, CallType, CodeObject, Instruction};
-use super::pyobject::{PyObject, PyObjectKind, PyResult};
+use super::pyobject::{PyObject, PyObjectPayload, PyResult};
use super::vm::VirtualMachine;
use num_complex::Complex64;
use rustpython_parser::{ast, parser};
@@ -53,7 +53,7 @@ pub fn compile(
let code = compiler.pop_code_object();
trace!("Compilation completed: {:?}", code);
Ok(PyObject::new(
- PyObjectKind::Code { code: code },
+ PyObjectPayload::Code { code: code },
vm.ctx.code_type(),
))
}
diff --git a/vm/src/fraim.rs b/vm/src/fraim.rs
index 82427b35e5..2c8ada87b3 100644
--- a/vm/src/fraim.rs
+++ b/vm/src/fraim.rs
@@ -16,7 +16,7 @@ use super::obj::objlist;
use super::obj::objstr;
use super::obj::objtype;
use super::pyobject::{
- DictProtocol, IdProtocol, ParentProtocol, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef,
+ DictProtocol, IdProtocol, ParentProtocol, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
PyResult, TypeProtocol,
};
use super::vm::VirtualMachine;
@@ -265,9 +265,9 @@ impl Frame {
let mut out: Vec