Skip to content

Commit e6164b9

Browse files
youknowoneSnowapril
authored andcommitted
vm.new_tuple to convert rust tuple to python tuple
1 parent c3e2d69 commit e6164b9

37 files changed

+258
-260
lines changed

stdlib/src/json.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ mod _json {
119119
($s:literal, $val:expr) => {
120120
if s.starts_with($s) {
121121
return Ok(PyIterReturn::Return(
122-
vm.ctx.new_tuple(vec![$val, vm.new_pyobj(idx + $s.len())]),
122+
vm.new_tuple(($val, idx + $s.len())).into(),
123123
));
124124
}
125125
};
@@ -130,19 +130,19 @@ mod _json {
130130
parse_const!("false", vm.ctx.new_bool(false));
131131

132132
if let Some((res, len)) = self.parse_number(s, vm) {
133-
return Ok(PyIterReturn::Return(
134-
vm.ctx
135-
.new_tuple(vec![res?, vm.ctx.new_int(idx + len).into()]),
136-
));
133+
return Ok(PyIterReturn::Return(vm.new_tuple((res?, idx + len)).into()));
137134
}
138135

139136
macro_rules! parse_constant {
140137
($s:literal) => {
141138
if s.starts_with($s) {
142-
return Ok(PyIterReturn::Return(vm.ctx.new_tuple(vec![
143-
vm.invoke(&self.parse_constant, ($s.to_owned(),))?,
144-
vm.new_pyobj(idx + $s.len()),
145-
])));
139+
return Ok(PyIterReturn::Return(
140+
vm.new_tuple((
141+
vm.invoke(&self.parse_constant, ($s.to_owned(),))?,
142+
idx + $s.len(),
143+
))
144+
.into(),
145+
));
146146
}
147147
};
148148
}

stdlib/src/re.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ mod re {
207207
vm.ctx.new_utf8_str(s)
208208
})
209209
.collect();
210-
vm.ctx.new_tuple(out)
210+
vm.ctx.new_tuple(out).into()
211211
}
212212
})
213213
.collect();
@@ -427,13 +427,13 @@ mod re {
427427
.into_iter()
428428
.map(|id| self.get_group(id, vm).map(|g| g.into_pyobject(vm)))
429429
.collect();
430-
Ok(vm.ctx.new_tuple(output?))
430+
Ok(vm.ctx.new_tuple(output?)).into()
431431
}
432432
}
433433
}
434434

435435
#[pymethod]
436-
fn groups(&self, default: OptionalArg, vm: &VirtualMachine) -> PyObjectRef {
436+
fn groups(&self, default: OptionalArg, vm: &VirtualMachine) -> PyTupleRef {
437437
let default = default.into_option();
438438
let groups = self
439439
.captures

stdlib/src/scproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod _scproxy {
6565
.into_pyobject(vm)
6666
})
6767
.collect();
68-
result.set_item("exceptions", vm.ctx.new_tuple(v), vm)?;
68+
result.set_item("exceptions", vm.ctx.new_tuple(v).into(), vm)?;
6969
}
7070

7171
Ok(Some(result))

stdlib/src/socket.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,13 +1266,14 @@ fn _socket_getaddrinfo(opts: GAIOptions, vm: &VirtualMachine) -> PyResult {
12661266
let list = addrs
12671267
.map(|ai| {
12681268
ai.map(|ai| {
1269-
vm.ctx.new_tuple(vec![
1270-
vm.ctx.new_int(ai.address).into(),
1271-
vm.ctx.new_int(ai.socktype).into(),
1272-
vm.ctx.new_int(ai.protocol).into(),
1273-
ai.canonname.into_pyobject(vm),
1269+
vm.new_tuple((
1270+
ai.address,
1271+
ai.socktype,
1272+
ai.protocol,
1273+
ai.canonname,
12741274
get_ip_addr_tuple(&ai.sockaddr, vm),
1275-
])
1275+
))
1276+
.into()
12761277
})
12771278
})
12781279
.collect::<io::Result<Vec<_>>>()

stdlib/src/ssl.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn _ssl_enum_certificates(store_name: PyStrRef, vm: &VirtualMachine) -> PyResult
200200
.into()
201201
}
202202
};
203-
Ok(vm.ctx.new_tuple(vec![cert, enc_type, usage]))
203+
Ok(vm.new_tuple((cert, enc_type, usage)).into())
204204
});
205205
let certs = certs
206206
.collect::<Result<Vec<_>, _>>()
@@ -1068,16 +1068,17 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
10681068
} else {
10691069
let dict = vm.ctx.new_dict();
10701070

1071-
let name_to_py = |name: &x509::X509NameRef| {
1072-
name.entries()
1071+
let name_to_py = |name: &x509::X509NameRef| -> PyResult {
1072+
let list = name
1073+
.entries()
10731074
.map(|entry| {
10741075
let txt = obj2txt(entry.object(), false).into_pyobject(vm);
10751076
let data = vm.ctx.new_utf8_str(entry.data().as_utf8()?.to_owned());
1076-
Ok(vm.ctx.new_tuple(vec![vm.ctx.new_tuple(vec![txt, data])]))
1077+
Ok(vm.new_tuple(((txt, data),)).into())
10771078
})
10781079
.collect::<Result<_, _>>()
1079-
.map(|list| vm.ctx.new_tuple(list))
1080-
.map_err(|e| convert_openssl_error(vm, e))
1080+
.map_err(|e| convert_openssl_error(vm, e))?;
1081+
Ok(vm.ctx.new_tuple(list).into())
10811082
};
10821083

10831084
dict.set_item("subject", name_to_py(cert.subject_name())?, vm)?;
@@ -1112,28 +1113,31 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
11121113
.iter()
11131114
.filter_map(|gen_name| {
11141115
if let Some(email) = gen_name.email() {
1115-
Some(vm.ctx.new_tuple(vec![
1116-
vm.ctx.new_ascii_literal(ascii!("email")),
1117-
vm.ctx.new_utf8_str(email),
1118-
]))
1116+
Some(
1117+
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("email")), email))
1118+
.into(),
1119+
)
11191120
} else if let Some(dnsname) = gen_name.dnsname() {
1120-
Some(vm.ctx.new_tuple(vec![
1121-
vm.ctx.new_ascii_literal(ascii!("DNS")),
1122-
vm.ctx.new_utf8_str(dnsname),
1123-
]))
1121+
Some(
1122+
vm.new_tuple((vm.ctx.new_ascii_literal(ascii!("DNS")), dnsname))
1123+
.into(),
1124+
)
11241125
} else if let Some(ip) = gen_name.ipaddress() {
1125-
Some(vm.ctx.new_tuple(vec![
1126-
vm.ctx.new_ascii_literal(ascii!("IP Address")),
1127-
vm.ctx.new_utf8_str(String::from_utf8_lossy(ip).into_owned()),
1128-
]))
1126+
Some(
1127+
vm.new_tuple((
1128+
vm.ctx.new_ascii_literal(ascii!("IP Address")),
1129+
String::from_utf8_lossy(ip).into_owned(),
1130+
))
1131+
.into(),
1132+
)
11291133
} else {
11301134
// TODO: convert every type of general name:
11311135
// https://github.com/python/cpython/blob/3.6/Modules/_ssl.c#L1092-L1231
11321136
None
11331137
}
11341138
})
11351139
.collect();
1136-
dict.set_item("subjectAltName", vm.ctx.new_tuple(san), vm)?;
1140+
dict.set_item("subjectAltName", vm.ctx.new_tuple(san).into(), vm)?;
11371141
};
11381142

11391143
dict.into()

vm/src/builtins/bytearray.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Implementation of the python bytearray object.
22
use super::{
3-
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTupleRef, PyTypeRef,
3+
PositionIterInternal, PyBytes, PyBytesRef, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef,
4+
PyTypeRef,
45
};
56
use crate::common::{
67
borrow::{BorrowedValue, BorrowedValueMut},
@@ -534,29 +535,29 @@ impl PyByteArray {
534535
}
535536

536537
#[pymethod]
537-
fn partition(&self, sep: PyBytesInner, vm: &VirtualMachine) -> PyResult {
538+
fn partition(&self, sep: PyBytesInner, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
538539
// sep ALWAYS converted to bytearray even it's bytes or memoryview
539540
// so its ok to accept PyBytesInner
540541
let value = self.inner();
541542
let (front, has_mid, back) = value.partition(&sep, vm)?;
542-
Ok(vm.ctx.new_tuple(vec![
543+
Ok(vm.new_tuple((
543544
vm.ctx.new_bytearray(front.to_vec()),
544545
vm.ctx
545546
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
546547
vm.ctx.new_bytearray(back.to_vec()),
547-
]))
548+
)))
548549
}
549550

550551
#[pymethod]
551-
fn rpartition(&self, sep: PyBytesInner, vm: &VirtualMachine) -> PyResult {
552+
fn rpartition(&self, sep: PyBytesInner, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
552553
let value = self.inner();
553554
let (back, has_mid, front) = value.rpartition(&sep, vm)?;
554-
Ok(vm.ctx.new_tuple(vec![
555+
Ok(vm.new_tuple((
555556
vm.ctx.new_bytearray(front.to_vec()),
556557
vm.ctx
557558
.new_bytearray(if has_mid { sep.elements } else { Vec::new() }),
558559
vm.ctx.new_bytearray(back.to_vec()),
559-
]))
560+
)))
560561
}
561562

562563
#[pymethod]
@@ -655,7 +656,7 @@ impl PyByteArray {
655656
let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).into_pyobject(vm);
656657
(
657658
zelf.as_object().clone_class(),
658-
PyTupleRef::with_elements(vec![bytes], &vm.ctx),
659+
PyTuple::new_ref(vec![bytes], &vm.ctx),
659660
zelf.as_object().dict(),
660661
)
661662
}
@@ -791,7 +792,7 @@ impl PyByteArrayIterator {
791792
self.internal.lock().length_hint(|obj| obj.len())
792793
}
793794
#[pymethod(magic)]
794-
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
795+
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
795796
self.internal
796797
.lock()
797798
.builtins_iter_reduce(|x| x.clone().into(), vm)

vm/src/builtins/bytes.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTupleRef, PyTypeRef};
1+
use super::{PositionIterInternal, PyDictRef, PyIntRef, PyStrRef, PyTuple, PyTupleRef, PyTypeRef};
22
use crate::{
33
anystr::{self, AnyStr},
44
bytesinner::{
@@ -377,33 +377,33 @@ impl PyBytes {
377377
}
378378

379379
#[pymethod]
380-
fn partition(&self, sep: PyObjectRef, vm: &VirtualMachine) -> PyResult {
380+
fn partition(&self, sep: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
381381
let sub = PyBytesInner::try_from_borrowed_object(vm, &sep)?;
382382
let (front, has_mid, back) = self.inner.partition(&sub, vm)?;
383-
Ok(vm.ctx.new_tuple(vec![
383+
Ok(vm.new_tuple((
384384
vm.ctx.new_bytes(front),
385385
if has_mid {
386386
sep
387387
} else {
388388
vm.ctx.new_bytes(Vec::new())
389389
},
390390
vm.ctx.new_bytes(back),
391-
]))
391+
)))
392392
}
393393

394394
#[pymethod]
395-
fn rpartition(&self, sep: PyObjectRef, vm: &VirtualMachine) -> PyResult {
395+
fn rpartition(&self, sep: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
396396
let sub = PyBytesInner::try_from_borrowed_object(vm, &sep)?;
397397
let (back, has_mid, front) = self.inner.rpartition(&sub, vm)?;
398-
Ok(vm.ctx.new_tuple(vec![
398+
Ok(vm.new_tuple((
399399
vm.ctx.new_bytes(front),
400400
if has_mid {
401401
sep
402402
} else {
403403
vm.ctx.new_bytes(Vec::new())
404404
},
405405
vm.ctx.new_bytes(back),
406-
]))
406+
)))
407407
}
408408

409409
#[pymethod]
@@ -491,7 +491,7 @@ impl PyBytes {
491491
.iter()
492492
.map(|x| x.into_pyobject(vm))
493493
.collect();
494-
PyTupleRef::with_elements(param, &vm.ctx)
494+
PyTuple::new_ref(param, &vm.ctx)
495495
}
496496

497497
#[pymethod(magic)]
@@ -511,7 +511,7 @@ impl PyBytes {
511511
let bytes = PyBytes::from(zelf.inner.elements.clone()).into_pyobject(vm);
512512
(
513513
zelf.as_object().clone_class(),
514-
PyTupleRef::with_elements(vec![bytes], &vm.ctx),
514+
PyTuple::new_ref(vec![bytes], &vm.ctx),
515515
zelf.as_object().dict(),
516516
)
517517
}
@@ -634,7 +634,7 @@ impl PyBytesIterator {
634634
}
635635

636636
#[pymethod(magic)]
637-
fn reduce(&self, vm: &VirtualMachine) -> PyObjectRef {
637+
fn reduce(&self, vm: &VirtualMachine) -> PyTupleRef {
638638
self.internal
639639
.lock()
640640
.builtins_iter_reduce(|x| x.clone().into(), vm)

vm/src/builtins/code.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
*/
44

5-
use super::{PyStrRef, PyTypeRef};
5+
use super::{PyStrRef, PyTupleRef, PyTypeRef};
66
use crate::{
77
bytecode::{self, BorrowedConstant, Constant, ConstantBag},
88
function::FuncArgs,
@@ -95,7 +95,7 @@ impl ConstantBag for PyObjBag<'_> {
9595
.into_iter()
9696
.map(|constant| self.make_constant(constant).0)
9797
.collect();
98-
ctx.new_tuple(elements)
98+
ctx.new_tuple(elements).into()
9999
}
100100
bytecode::ConstantData::None => ctx.none(),
101101
bytecode::ConstantData::Ellipsis => ctx.ellipsis(),
@@ -123,7 +123,7 @@ impl ConstantBag for PyObjBag<'_> {
123123
.into_iter()
124124
.map(|constant| self.make_constant_borrowed(constant).0)
125125
.collect();
126-
ctx.new_tuple(elements)
126+
ctx.new_tuple(elements).into()
127127
}
128128
bytecode::BorrowedConstant::None => ctx.none(),
129129
bytecode::BorrowedConstant::Ellipsis => ctx.ellipsis(),
@@ -233,7 +233,7 @@ impl PyRef<PyCode> {
233233
}
234234

235235
#[pyproperty]
236-
fn co_consts(self, vm: &VirtualMachine) -> PyObjectRef {
236+
fn co_consts(self, vm: &VirtualMachine) -> PyTupleRef {
237237
let consts = self.code.constants.iter().map(|x| x.0.clone()).collect();
238238
vm.ctx.new_tuple(consts)
239239
}
@@ -249,7 +249,7 @@ impl PyRef<PyCode> {
249249
}
250250

251251
#[pyproperty]
252-
fn co_varnames(self, vm: &VirtualMachine) -> PyObjectRef {
252+
fn co_varnames(self, vm: &VirtualMachine) -> PyTupleRef {
253253
let varnames = self
254254
.code
255255
.varnames

vm/src/builtins/complex.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,9 @@ impl PyComplex {
393393
}
394394

395395
#[pymethod(magic)]
396-
fn getnewargs(&self, vm: &VirtualMachine) -> PyObjectRef {
396+
fn getnewargs(&self) -> (f64, f64) {
397397
let Complex64 { re, im } = self.value;
398-
vm.ctx.new_tuple(vec![
399-
vm.ctx.new_float(re).into(),
400-
vm.ctx.new_float(im).into(),
401-
])
398+
(re, im)
402399
}
403400
}
404401

vm/src/builtins/dict.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,14 @@ impl PyDict {
373373
}
374374

375375
#[pymethod]
376-
fn popitem(&self, vm: &VirtualMachine) -> PyResult {
377-
if let Some((key, value)) = self.entries.pop_back() {
378-
Ok(vm.ctx.new_tuple(vec![key, value]))
379-
} else {
376+
fn popitem(&self, vm: &VirtualMachine) -> PyResult<(PyObjectRef, PyObjectRef)> {
377+
let (key, value) = self.entries.pop_back().ok_or_else(|| {
380378
let err_msg = vm
381379
.ctx
382380
.new_ascii_literal(ascii!("popitem(): dictionary is empty"));
383-
Err(vm.new_key_error(err_msg))
384-
}
381+
vm.new_key_error(err_msg)
382+
})?;
383+
Ok((key, value))
385384
}
386385

387386
pub fn from_attributes(attrs: PyAttributes, vm: &VirtualMachine) -> PyResult<Self> {
@@ -909,7 +908,7 @@ dict_view! {
909908
"dict_itemiterator",
910909
"dict_reverseitemiterator",
911910
|vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef|
912-
vm.ctx.new_tuple(vec![key, value])
911+
vm.new_tuple((key, value)).into()
913912
}
914913

915914
#[pyimpl(with(DictView, Comparable, Iterable))]

0 commit comments

Comments
 (0)