Skip to content

Commit 379cf2d

Browse files
committed
new_bytes return PyRef
1 parent ef6c066 commit 379cf2d

File tree

22 files changed

+80
-82
lines changed

22 files changed

+80
-82
lines changed

stdlib/src/fcntl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod fcntl {
4343
if ret < 0 {
4444
return Err(os::errno_err(vm));
4545
}
46-
return Ok(vm.ctx.new_bytes(buf[..arg_len].to_vec()));
46+
return Ok(vm.ctx.new_bytes(buf[..arg_len].to_vec()).into());
4747
}
4848
OptionalArg::Present(Either::B(i)) => i.as_u32_mask(),
4949
OptionalArg::Missing => 0,
@@ -96,7 +96,7 @@ mod fcntl {
9696
if ret < 0 {
9797
return Err(os::errno_err(vm));
9898
}
99-
Ok(vm.ctx.new_bytes(buf[..buf_len].to_vec()))
99+
Ok(vm.ctx.new_bytes(buf[..buf_len].to_vec()).into())
100100
}
101101
Either::B(i) => {
102102
let ret = unsafe { libc::ioctl(fd, request as _, i) };

stdlib/src/socket.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,9 @@ impl PySocket {
839839
)
840840
};
841841
if ret < 0 {
842-
Err(crate::vm::stdlib::os::errno_err(vm))
843-
} else {
844-
Ok(vm.ctx.new_int(flag).into())
842+
return Err(crate::vm::stdlib::os::errno_err(vm));
845843
}
844+
Ok(vm.ctx.new_int(flag).into())
846845
} else {
847846
if buflen <= 0 || buflen > 1024 {
848847
return Err(vm.new_os_error("getsockopt buflen out of range".to_owned()));
@@ -853,10 +852,9 @@ impl PySocket {
853852
unsafe { c::getsockopt(fd, level, name, buf.as_mut_ptr() as *mut _, &mut buflen) };
854853
buf.truncate(buflen as usize);
855854
if ret < 0 {
856-
Err(crate::vm::stdlib::os::errno_err(vm))
857-
} else {
858-
Ok(vm.ctx.new_bytes(buf))
855+
return Err(crate::vm::stdlib::os::errno_err(vm));
859856
}
857+
Ok(vm.ctx.new_bytes(buf).into())
860858
}
861859
}
862860

@@ -1024,7 +1022,7 @@ fn get_addr_tuple(addr: &socket2::SockAddr, vm: &VirtualMachine) -> PyObjectRef
10241022
{
10251023
let abstractaddrlen = addr_len - sun_path_offset;
10261024
let abstractpath = &path_u8[..abstractaddrlen];
1027-
vm.ctx.new_bytes(abstractpath.to_vec())
1025+
vm.ctx.new_bytes(abstractpath.to_vec()).into()
10281026
} else {
10291027
let len = memchr::memchr(b'\0', path_u8).unwrap_or_else(|| path_u8.len());
10301028
let path = &path_u8[..len];
@@ -1304,24 +1302,24 @@ fn _socket_gethostbyname(name: PyStrRef, vm: &VirtualMachine) -> PyResult<String
13041302
}
13051303
}
13061304

1307-
fn _socket_inet_pton(af_inet: i32, ip_string: PyStrRef, vm: &VirtualMachine) -> PyResult {
1308-
match af_inet {
1305+
fn _socket_inet_pton(af_inet: i32, ip_string: PyStrRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
1306+
static ERROR_MSG: &str = "illegal IP address string passed to inet_pton";
1307+
let ip_addr = match af_inet {
13091308
c::AF_INET => ip_string
13101309
.as_str()
13111310
.parse::<Ipv4Addr>()
1312-
.map(|ip_addr| vm.ctx.new_bytes(ip_addr.octets().to_vec()))
1313-
.map_err(|_| {
1314-
vm.new_os_error("illegal IP address string passed to inet_pton".to_owned())
1315-
}),
1311+
.map_err(|_| vm.new_os_error(ERROR_MSG.to_owned()))?
1312+
.octets()
1313+
.to_vec(),
13161314
c::AF_INET6 => ip_string
13171315
.as_str()
13181316
.parse::<Ipv6Addr>()
1319-
.map(|ip_addr| vm.ctx.new_bytes(ip_addr.octets().to_vec()))
1320-
.map_err(|_| {
1321-
vm.new_os_error("illegal IP address string passed to inet_pton".to_owned())
1322-
}),
1323-
_ => Err(vm.new_os_error("Address family not supported by protocol".to_owned())),
1324-
}
1317+
.map_err(|_| vm.new_os_error(ERROR_MSG.to_owned()))?
1318+
.octets()
1319+
.to_vec(),
1320+
_ => return Err(vm.new_os_error("Address family not supported by protocol".to_owned())),
1321+
};
1322+
Ok(ip_addr)
13251323
}
13261324

13271325
fn _socket_inet_ntop(

stdlib/src/ssl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ impl PySslSocket {
960960
Either::B(mut buf) => {
961961
buf.truncate(n);
962962
buf.shrink_to_fit();
963-
vm.ctx.new_bytes(buf)
963+
vm.ctx.new_bytes(buf).into()
964964
}
965965
};
966966
Ok(ret)
@@ -1064,7 +1064,7 @@ fn cipher_to_tuple(cipher: &ssl::SslCipherRef) -> CipherTuple {
10641064
fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
10651065
let r = if binary {
10661066
let b = cert.to_der().map_err(|e| convert_openssl_error(vm, e))?;
1067-
vm.ctx.new_bytes(b)
1067+
vm.ctx.new_bytes(b).into()
10681068
} else {
10691069
let dict = vm.ctx.new_dict();
10701070

stdlib/src/termios.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod termios {
3434
.enumerate()
3535
.map(|(i, &c)| match i {
3636
termios::VMIN | termios::VTIME if noncanon => vm.ctx.new_int(c).into(),
37-
_ => vm.ctx.new_bytes(vec![c as u8]),
37+
_ => vm.ctx.new_bytes(vec![c as u8]).into(),
3838
})
3939
.collect::<Vec<_>>();
4040
let out = vec![

stdlib/src/zlib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ mod zlib {
9696

9797
/// Returns a bytes object containing compressed data.
9898
#[pyfunction]
99-
fn compress(data: ArgBytesLike, level: OptionalArg<i32>, vm: &VirtualMachine) -> PyResult {
99+
fn compress(
100+
data: ArgBytesLike,
101+
level: OptionalArg<i32>,
102+
vm: &VirtualMachine,
103+
) -> PyResult<PyBytesRef> {
100104
let compression = compression_from_int(level.into_option())
101105
.ok_or_else(|| new_zlib_error("Bad compression level", vm))?;
102106

vm/src/builtins/bytearray.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ impl PyByteArray {
227227
}
228228

229229
#[pymethod]
230-
fn maketrans(from: PyBytesInner, to: PyBytesInner, vm: &VirtualMachine) -> PyResult {
231-
PyBytesInner::maketrans(from, to, vm)
230+
fn maketrans(from: PyBytesInner, to: PyBytesInner) -> PyResult<Vec<u8>> {
231+
PyBytesInner::maketrans(from, to)
232232
}
233233

234234
#[pymethod]
@@ -388,7 +388,7 @@ impl PyByteArray {
388388
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
389389
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
390390
let bytes = vm.ctx.new_bytes(bytes);
391-
Callable::call(&cls, vec![bytes].into(), vm)
391+
Callable::call(&cls, vec![bytes.into()].into(), vm)
392392
}
393393

394394
#[pymethod]

vm/src/builtins/bytes.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl From<PyBytesInner> for PyBytes {
5959

6060
impl IntoPyObject for Vec<u8> {
6161
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
62-
vm.ctx.new_bytes(self)
62+
vm.ctx.new_bytes(self).into()
6363
}
6464
}
6565

@@ -101,6 +101,12 @@ impl SlotConstructor for PyBytes {
101101
}
102102
}
103103

104+
impl PyBytes {
105+
pub fn new_ref(data: Vec<u8>, ctx: &PyContext) -> PyRef<Self> {
106+
PyRef::new_ref(Self::from(data), ctx.types.bytes_type.clone(), None)
107+
}
108+
}
109+
104110
#[pyimpl(
105111
flags(BASETYPE),
106112
with(AsMapping, Hashable, Comparable, AsBuffer, Iterable, SlotConstructor)
@@ -142,8 +148,8 @@ impl PyBytes {
142148
}
143149

144150
#[pymethod(magic)]
145-
fn add(&self, other: ArgBytesLike, vm: &VirtualMachine) -> PyObjectRef {
146-
vm.ctx.new_bytes(self.inner.add(&*other.borrow_buf()))
151+
fn add(&self, other: ArgBytesLike) -> Vec<u8> {
152+
self.inner.add(&*other.borrow_buf())
147153
}
148154

149155
#[pymethod(magic)]
@@ -156,8 +162,8 @@ impl PyBytes {
156162
}
157163

158164
#[pymethod]
159-
fn maketrans(from: PyBytesInner, to: PyBytesInner, vm: &VirtualMachine) -> PyResult {
160-
PyBytesInner::maketrans(from, to, vm)
165+
fn maketrans(from: PyBytesInner, to: PyBytesInner) -> PyResult<Vec<u8>> {
166+
PyBytesInner::maketrans(from, to)
161167
}
162168

163169
#[pymethod(magic)]
@@ -238,7 +244,7 @@ impl PyBytes {
238244
#[pyclassmethod]
239245
fn fromhex(cls: PyTypeRef, string: PyStrRef, vm: &VirtualMachine) -> PyResult {
240246
let bytes = PyBytesInner::fromhex(string.as_str(), vm)?;
241-
let bytes = vm.ctx.new_bytes(bytes);
247+
let bytes = vm.ctx.new_bytes(bytes).into();
242248
Callable::call(&cls, vec![bytes].into(), vm)
243249
}
244250

@@ -368,13 +374,13 @@ impl PyBytes {
368374
#[pymethod]
369375
fn split(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
370376
self.inner
371-
.split(options, |s, vm| vm.ctx.new_bytes(s.to_vec()), vm)
377+
.split(options, |s, vm| vm.ctx.new_bytes(s.to_vec()).into(), vm)
372378
}
373379

374380
#[pymethod]
375381
fn rsplit(&self, options: ByteInnerSplitOptions, vm: &VirtualMachine) -> PyResult {
376382
self.inner
377-
.rsplit(options, |s, vm| vm.ctx.new_bytes(s.to_vec()), vm)
383+
.rsplit(options, |s, vm| vm.ctx.new_bytes(s.to_vec()).into(), vm)
378384
}
379385

380386
#[pymethod]
@@ -386,7 +392,7 @@ impl PyBytes {
386392
if has_mid {
387393
sep
388394
} else {
389-
vm.ctx.new_bytes(Vec::new())
395+
vm.ctx.new_bytes(Vec::new()).into()
390396
},
391397
vm.ctx.new_bytes(back),
392398
)))
@@ -401,7 +407,7 @@ impl PyBytes {
401407
if has_mid {
402408
sep
403409
} else {
404-
vm.ctx.new_bytes(Vec::new())
410+
vm.ctx.new_bytes(Vec::new()).into()
405411
},
406412
vm.ctx.new_bytes(back),
407413
)))
@@ -416,7 +422,7 @@ impl PyBytes {
416422
fn splitlines(&self, options: anystr::SplitLinesArgs, vm: &VirtualMachine) -> PyObjectRef {
417423
let lines = self
418424
.inner
419-
.splitlines(options, |x| vm.ctx.new_bytes(x.to_vec()));
425+
.splitlines(options, |x| vm.ctx.new_bytes(x.to_vec()).into());
420426
vm.ctx.new_list(lines)
421427
}
422428

vm/src/builtins/code.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl ConstantBag for PyObjBag<'_> {
8787
vm.intern_string(value).into()
8888
}
8989
bytecode::ConstantData::Str { value } => vm.ctx.new_utf8_str(value),
90-
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()),
90+
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
9191
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(),
9292
bytecode::ConstantData::Code { code } => vm.new_code_object(code.map_bag(self)).into(),
9393
bytecode::ConstantData::Tuple { elements } => {
@@ -113,7 +113,7 @@ impl ConstantBag for PyObjBag<'_> {
113113
vm.intern_string(value).into()
114114
}
115115
bytecode::BorrowedConstant::Str { value } => vm.ctx.new_utf8_str(value),
116-
bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()),
116+
bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
117117
bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(),
118118
bytecode::BorrowedConstant::Code { code } => {
119119
vm.new_code_object(code.map_clone_bag(self)).into()

vm/src/bytesinner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl PyBytesInner {
358358
) -> PyResult {
359359
let obj = match self.elements.get_item(vm, needle, owner_type)? {
360360
Either::A(byte) => vm.new_pyobj(byte),
361-
Either::B(bytes) => vm.ctx.new_bytes(bytes),
361+
Either::B(bytes) => vm.ctx.new_bytes(bytes).into(),
362362
};
363363
Ok(obj)
364364
}
@@ -583,7 +583,7 @@ impl PyBytesInner {
583583
Ok(self.elements.py_find(&needle, range, find))
584584
}
585585

586-
pub fn maketrans(from: PyBytesInner, to: PyBytesInner, vm: &VirtualMachine) -> PyResult {
586+
pub fn maketrans(from: PyBytesInner, to: PyBytesInner) -> PyResult<Vec<u8>> {
587587
let mut res = vec![];
588588

589589
for i in 0..=255 {
@@ -594,7 +594,7 @@ impl PyBytesInner {
594594
});
595595
}
596596

597-
Ok(vm.ctx.new_bytes(res))
597+
Ok(res)
598598
}
599599

600600
pub fn translate(

vm/src/import.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
scope::Scope,
99
version::get_git_revision,
1010
vm::{InitParameter, VirtualMachine},
11-
ItemProtocol, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
11+
ItemProtocol, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
1212
};
1313
use rand::Rng;
1414

@@ -56,7 +56,8 @@ pub(crate) fn init_importlib(
5656
if magic.len() != 4 {
5757
magic = rand::thread_rng().gen::<[u8; 4]>().to_vec();
5858
}
59-
vm.set_attr(&importlib_external, "MAGIC_NUMBER", vm.ctx.new_bytes(magic))?;
59+
let magic: PyObjectRef = vm.ctx.new_bytes(magic).into();
60+
vm.set_attr(&importlib_external, "MAGIC_NUMBER", magic)?;
6061
let zipimport_res = (|| -> PyResult<()> {
6162
let zipimport = vm.import("zipimport", None, 0)?;
6263
let zipimporter = vm.get_attribute(zipimport, "zipimporter")?;

vm/src/py_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn file_readline(obj: &PyObjectRef, size: Option<usize>, vm: &VirtualMachine
8686
return Err(eof_err());
8787
}
8888
if buf.last() == Some(&b'\n') {
89-
vm.ctx.new_bytes(buf[..buf.len() - 1].to_owned())
89+
vm.ctx.new_bytes(buf[..buf.len() - 1].to_owned()).into()
9090
} else {
9191
b.into()
9292
}

vm/src/pyobject.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,8 @@ impl PyContext {
201201
)
202202
}
203203

204-
pub fn new_bytes(&self, data: Vec<u8>) -> PyObjectRef {
205-
PyObject::new(
206-
bytes::PyBytes::from(data),
207-
self.types.bytes_type.clone(),
208-
None,
209-
)
204+
pub fn new_bytes(&self, data: Vec<u8>) -> PyRef<bytes::PyBytes> {
205+
bytes::PyBytes::new_ref(data, self)
210206
}
211207

212208
#[inline]

vm/src/stdlib/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Node for ast::Constant {
190190
ast::Constant::None => vm.ctx.none(),
191191
ast::Constant::Bool(b) => vm.ctx.new_bool(b).into(),
192192
ast::Constant::Str(s) => vm.ctx.new_utf8_str(s),
193-
ast::Constant::Bytes(b) => vm.ctx.new_bytes(b),
193+
ast::Constant::Bytes(b) => vm.ctx.new_bytes(b).into(),
194194
ast::Constant::Int(i) => vm.ctx.new_int(i).into(),
195195
ast::Constant::Tuple(t) => vm
196196
.ctx

vm/src/stdlib/codecs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod _codecs {
157157
reason: &str,
158158
) -> PyResult<(PyStrRef, Option<PyBytesRef>, usize)> {
159159
let vm = self.vm;
160-
let data_bytes = vm.ctx.new_bytes(data.to_vec());
160+
let data_bytes: PyObjectRef = vm.ctx.new_bytes(data.to_vec()).into();
161161
let decode_exc = vm.new_exception(
162162
vm.ctx.exceptions.unicode_decode_error.clone(),
163163
vec![

vm/src/stdlib/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3237,7 +3237,7 @@ mod _io {
32373237

32383238
//Retrieves the entire bytes object value from the underlying buffer
32393239
#[pymethod]
3240-
fn getvalue(self, vm: &VirtualMachine) -> PyResult {
3240+
fn getvalue(self, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
32413241
Ok(vm.ctx.new_bytes(self.buffer(vm)?.getvalue()))
32423242
}
32433243

vm/src/stdlib/os.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ impl OutputMode {
3939
#[cfg(any(unix, target_os = "wasi"))]
4040
{
4141
use ffi_ext::OsStringExt;
42-
Ok(vm.ctx.new_bytes(path.into_os_string().into_vec()))
42+
Ok(vm.ctx.new_bytes(path.into_os_string().into_vec()).into())
4343
}
4444
#[cfg(windows)]
4545
{
46-
path_as_string(path).map(|s| vm.ctx.new_bytes(s.into_bytes()))
46+
path_as_string(path).map(|s| vm.ctx.new_bytes(s.into_bytes()).into())
4747
}
4848
}
4949
}
@@ -537,7 +537,7 @@ pub(super) mod _os {
537537
}
538538

539539
#[pyfunction]
540-
fn read(fd: i32, n: usize, vm: &VirtualMachine) -> PyResult {
540+
fn read(fd: i32, n: usize, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
541541
let mut buffer = vec![0u8; n];
542542
let mut file = Fd(fd);
543543
let n = file

0 commit comments

Comments
 (0)