mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Fix byte pickle implement
This commit is contained in:
1 parent
d936b0c7d1
commit
d1f2179397
3 files changed
+22
-20
No files matched your search
@@ -1,4 +1,5 @@
|
||||
from testutils import assert_raises
|
||||
import pickle
|
||||
|
||||
# new
|
||||
assert bytearray([1, 2, 3])
|
||||
@@ -720,3 +721,15 @@ b = a.copy()
|
||||
assert a.__ne__(b) is False
|
||||
b = bytearray(b'my bytearray')
|
||||
assert a.__ne__(b) is True
|
||||
|
||||
# pickle
|
||||
a = bytearray(b'\xffab\x80\0\0\370\0\0')
|
||||
# TODO: Different output when pickle dumping a str if protocol is 0
|
||||
# CPython's output is like:
|
||||
# b'c__builtin__\nbytearray\np0\n(c_codecs\nencode\np1\n(V\xffab\x80\\u0000\\u0000\xf8\\u0000\\u0000\np2\nVlatin1\np3\ntp4\nRp5\ntp6\nRp7\n.'
|
||||
# We do have fully Interoperability between our output and CPython's
|
||||
assert pickle.dumps(a, 0) == b'c__builtin__\nbytearray\np0\n(c_codecs\nencode\np1\n(V\xffab\x80\x00\x00\xf8\x00\x00\np2\nVlatin1\np3\ntp4\nRp5\ntp6\nRp7\n.'
|
||||
assert pickle.dumps(a, 1) == b'c__builtin__\nbytearray\nq\x00(c_codecs\nencode\nq\x01(X\x0c\x00\x00\x00\xc3\xbfab\xc2\x80\x00\x00\xc3\xb8\x00\x00q\x02X\x06\x00\x00\x00latin1q\x03tq\x04Rq\x05tq\x06Rq\x07.'
|
||||
assert pickle.dumps(a, 2) == b'\x80\x02c__builtin__\nbytearray\nq\x00c_codecs\nencode\nq\x01X\x0c\x00\x00\x00\xc3\xbfab\xc2\x80\x00\x00\xc3\xb8\x00\x00q\x02X\x06\x00\x00\x00latin1q\x03\x86q\x04Rq\x05\x85q\x06Rq\x07.'
|
||||
assert pickle.dumps(a, 3) == b'\x80\x03cbuiltins\nbytearray\nq\x00C\t\xffab\x80\x00\x00\xf8\x00\x00q\x01\x85q\x02Rq\x03.'
|
||||
assert pickle.dumps(a, 4) == b'\x80\x04\x95*\x00\x00\x00\x00\x00\x00\x00\x8c\x08builtins\x94\x8c\tbytearray\x94\x93\x94C\t\xffab\x80\x00\x00\xf8\x00\x00\x94\x85\x94R\x94.'
|
||||
@@ -15,6 +15,7 @@ use crate::bytesinner::{
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::common::cell::{PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard};
|
||||
use crate::function::{OptionalArg, OptionalOption};
|
||||
use crate::obj::objbytes::PyBytes;
|
||||
use crate::obj::objtuple::PyTuple;
|
||||
use crate::pyobject::{
|
||||
BorrowValue, Either, IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext,
|
||||
@@ -566,20 +567,14 @@ impl PyByteArray {
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn reduce_ex(&self, _proto: usize, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
self.reduce(vm)
|
||||
fn reduce_ex(zelf: PyRef<Self>, _proto: usize, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
Self::reduce(zelf, vm)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn reduce(&self, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
let param: Vec<PyObjectRef> = self
|
||||
.borrow_value()
|
||||
.elements
|
||||
.iter()
|
||||
.map(|x| x.into_pyobject(vm))
|
||||
.collect();
|
||||
let param = PyTuple::from(vec![vm.ctx.new_tuple(param)]);
|
||||
(Self::class(vm), param)
|
||||
fn reduce(zelf: PyRef<Self>, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
let bytes = PyBytes::from(zelf.borrow_value().elements.clone()).into_pyobject(vm);
|
||||
(Self::class(vm), PyTuple::from(vec![bytes]))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::bytesinner::{
|
||||
};
|
||||
use crate::byteslike::PyBytesLike;
|
||||
use crate::function::{OptionalArg, OptionalOption};
|
||||
use crate::obj::objtuple::PyTuple;
|
||||
use crate::obj::objtuple::{PyTuple, PyTupleRef};
|
||||
use crate::pyobject::{
|
||||
BorrowValue, Either, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
|
||||
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
|
||||
@@ -466,20 +466,14 @@ impl PyBytes {
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn reduce_ex(&self, _proto: usize, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
self.reduce(vm)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn reduce(&self, vm: &VirtualMachine) -> (PyClassRef, PyTuple) {
|
||||
fn getnewargs(&self, vm: &VirtualMachine) -> PyTupleRef {
|
||||
let param: Vec<PyObjectRef> = self
|
||||
.inner
|
||||
.elements
|
||||
.iter()
|
||||
.map(|x| x.into_pyobject(vm))
|
||||
.collect();
|
||||
let param = PyTuple::from(vec![vm.ctx.new_tuple(param)]);
|
||||
(Self::class(vm), param)
|
||||
PyTuple::from(param).into_ref(vm)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user