Skip to content

Commit 9a86bbd

Browse files
committed
ByteArray Usage
1 parent c2e73cc commit 9a86bbd

File tree

7 files changed

+181
-141
lines changed

7 files changed

+181
-141
lines changed

tests/snippets/os_open.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
assert os.open('README.md') > 0
4+

tests/snippets/os_static.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
assert os.O_RDONLY == 0
4+
assert os.O_WRONLY == 1
5+
assert os.O_RDWR == 2
6+
assert os.O_NONBLOCK == 3
7+
assert os.O_APPEND == 8
8+
assert os.O_CREAT == 512

vm/src/obj/objbytes.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ pub fn init(context: &PyContext) {
2020
context.set_attr(bytes_type, "__new__", context.new_rustfunc(bytes_new));
2121
context.set_attr(bytes_type, "__repr__", context.new_rustfunc(bytes_repr));
2222
context.set_attr(bytes_type, "__len__", context.new_rustfunc(bytes_len));
23-
2423
}
2524

2625
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -70,18 +69,13 @@ fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
7069
}
7170

7271
fn bytes_len(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
73-
arg_check!(
74-
vm,
75-
args,
76-
required = [(a, Some(vm.ctx.bytes_type()))]
77-
);
72+
arg_check!(vm, args, required = [(a, Some(vm.ctx.bytes_type()))]);
7873

7974
let byte_vec = get_value(a).to_vec();
8075
let value = byte_vec.len().to_bigint();
8176
Ok(vm.ctx.new_int(value.unwrap()))
8277
}
8378

84-
8579
fn bytes_hash(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8680
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
8781
let data = get_value(zelf);

vm/src/obj/objmemory.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
21
use super::objtype;
32

43
use super::super::pyobject::{
5-
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol
4+
PyContext, PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol,
65
};
76
use super::super::vm::VirtualMachine;
87

9-
108
pub fn new_memory_view(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
11-
arg_check!(
12-
vm,
13-
args,
14-
required = [(cls, None), (bytes_object, None)]
15-
);
9+
arg_check!(vm, args, required = [(cls, None), (bytes_object, None)]);
1610
vm.ctx.set_attr(&cls, "obj", bytes_object.clone());
1711
Ok(PyObject::new(
18-
PyObjectKind::MemoryView { obj: bytes_object.clone() },
19-
cls.clone()
12+
PyObjectKind::MemoryView {
13+
obj: bytes_object.clone(),
14+
},
15+
cls.clone(),
2016
))
21-
2217
}
2318

2419
pub fn init(ctx: &PyContext) {
2520
let ref memoryview_type = ctx.memoryview_type;
26-
ctx.set_attr(&memoryview_type, "__new__", ctx.new_rustfunc(new_memory_view));
21+
ctx.set_attr(
22+
&memoryview_type,
23+
"__new__",
24+
ctx.new_rustfunc(new_memory_view),
25+
);
2726
}

vm/src/pyobject.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ fn _nothing() -> PyObjectRef {
146146
PyObject {
147147
kind: PyObjectKind::None,
148148
typ: None,
149-
}
150-
.into_ref()
149+
}.into_ref()
151150
}
152151

153152
pub fn create_type(
@@ -220,7 +219,7 @@ impl PyContext {
220219
);
221220
let context = PyContext {
222221
bool_type: bool_type,
223-
memoryview_type : memoryview_type,
222+
memoryview_type: memoryview_type,
224223
bytearray_type: bytearray_type,
225224
bytes_type: bytes_type,
226225
code_type: code_type,
@@ -412,7 +411,10 @@ impl PyContext {
412411
}
413412

414413
pub fn new_bytearray(&self, data: Vec<u8>) -> PyObjectRef {
415-
PyObject::new(PyObjectKind::Bytes { value: data }, self.bytearray_type())
414+
PyObject::new(
415+
PyObjectKind::ByteArray { value: data },
416+
self.bytearray_type(),
417+
)
416418
}
417419

418420
pub fn new_bool(&self, b: bool) -> PyObjectRef {
@@ -464,8 +466,7 @@ impl PyContext {
464466
PyObject {
465467
kind: PyObjectKind::Scope { scope: scope },
466468
typ: None,
467-
}
468-
.into_ref()
469+
}.into_ref()
469470
}
470471

471472
pub fn new_module(&self, name: &str, scope: PyObjectRef) -> PyObjectRef {
@@ -745,6 +746,21 @@ impl DictProtocol for PyObjectRef {
745746
}
746747
}
747748

749+
pub trait BufferProtocol {
750+
fn readonly(&self) -> bool;
751+
}
752+
753+
impl BufferProtocol for PyObjectRef {
754+
fn readonly(&self) -> bool {
755+
match self.borrow().kind {
756+
PyObjectKind::Bytes { value: _ } => false,
757+
PyObjectKind::ByteArray { value: _ } => true,
758+
PyObjectKind::MemoryView { obj: _ } => true,
759+
_ => panic!("Bytes-Like type expected not {:?}", self),
760+
}
761+
}
762+
}
763+
748764
impl fmt::Debug for PyObject {
749765
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
750766
write!(f, "[PyObj {:?}]", self.kind)
@@ -826,6 +842,9 @@ pub enum PyObjectKind {
826842
Bytes {
827843
value: Vec<u8>,
828844
},
845+
ByteArray {
846+
value: Vec<u8>,
847+
},
829848
Sequence {
830849
elements: Vec<PyObjectRef>,
831850
},
@@ -845,7 +864,7 @@ pub enum PyObjectKind {
845864
step: Option<i32>,
846865
},
847866
MemoryView {
848-
obj : PyObjectRef
867+
obj: PyObjectRef,
849868
},
850869
Code {
851870
code: bytecode::CodeObject,
@@ -897,6 +916,7 @@ impl fmt::Debug for PyObjectKind {
897916
&PyObjectKind::Float { ref value } => write!(f, "float {}", value),
898917
&PyObjectKind::Complex { ref value } => write!(f, "complex {}", value),
899918
&PyObjectKind::Bytes { ref value } => write!(f, "bytes/bytearray {:?}", value),
919+
&PyObjectKind::ByteArray { ref value } => write!(f, "bytes/bytearray {:?}", value),
900920
&PyObjectKind::MemoryView { ref obj } => write!(f, "bytes/bytearray {:?}", obj),
901921
&PyObjectKind::Sequence { elements: _ } => write!(f, "list or tuple"),
902922
&PyObjectKind::Dict { elements: _ } => write!(f, "dict"),
@@ -939,8 +959,7 @@ impl PyObject {
939959
kind: kind,
940960
typ: Some(typ),
941961
// dict: HashMap::new(), // dict,
942-
}
943-
.into_ref()
962+
}.into_ref()
944963
}
945964

946965
/// Deprecated method, please call `vm.to_pystr`
@@ -951,6 +970,7 @@ impl PyObject {
951970
PyObjectKind::Float { ref value } => format!("{:?}", value),
952971
PyObjectKind::Complex { ref value } => format!("{:?}", value),
953972
PyObjectKind::Bytes { ref value } => format!("b'{:?}'", value),
973+
PyObjectKind::ByteArray { ref value } => format!("b'{:?}'", value),
954974
PyObjectKind::MemoryView { ref obj } => format!("b'{:?}'", obj),
955975
PyObjectKind::Sequence { ref elements } => format!(
956976
"(/[{}]/)",

0 commit comments

Comments
 (0)