Skip to content

Commit 32a9b95

Browse files
authored
Merge pull request RustPython#3465 from 24seconds/fix_test_base64
Support tuple type also for memoryview shape arg
2 parents 00155ab + 65d9e2f commit 32a9b95

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

Lib/test/test_base64.py

-23
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ def test_decodestring_warns(self):
2626
with self.assertWarns(DeprecationWarning):
2727
base64.decodestring(b"d3d3LnB5dGhvbi5vcmc=\n")
2828

29-
# TODO: RUSTPYTHON
30-
@unittest.expectedFailure
3129
def test_encodebytes(self):
3230
eq = self.assertEqual
3331
eq(base64.encodebytes(b"www.python.org"), b"d3d3LnB5dGhvbi5vcmc=\n")
@@ -47,8 +45,6 @@ def test_encodebytes(self):
4745
eq(base64.encodebytes(array('B', b'abc')), b'YWJj\n')
4846
self.check_type_errors(base64.encodebytes)
4947

50-
# TODO: RUSTPYTHON
51-
@unittest.expectedFailure
5248
def test_decodebytes(self):
5349
eq = self.assertEqual
5450
eq(base64.decodebytes(b"d3d3LnB5dGhvbi5vcmc=\n"), b"www.python.org")
@@ -133,9 +129,6 @@ def check_nonbyte_element_format(self, f, data):
133129
int_data = memoryview(bytes_data).cast('I')
134130
self.assertEqual(f(int_data), f(bytes_data))
135131

136-
137-
# TODO: RUSTPYTHON
138-
@unittest.expectedFailure
139132
def test_b64encode(self):
140133
eq = self.assertEqual
141134
# Test default alphabet
@@ -186,8 +179,6 @@ def test_b64encode(self):
186179
b'\xd3V\xbeo\xf7\x1d', b'01a-b_cd')
187180
self.check_encode_type_errors(base64.urlsafe_b64encode)
188181

189-
# TODO: RUSTPYTHON
190-
@unittest.expectedFailure
191182
def test_b64decode(self):
192183
eq = self.assertEqual
193184

@@ -284,8 +275,6 @@ def test_b64decode_invalid_chars(self):
284275
self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
285276
self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res)
286277

287-
# TODO: RUSTPYTHON
288-
@unittest.expectedFailure
289278
def test_b32encode(self):
290279
eq = self.assertEqual
291280
eq(base64.b32encode(b''), b'')
@@ -299,8 +288,6 @@ def test_b32encode(self):
299288
self.check_other_types(base64.b32encode, b'abcd', b'MFRGGZA=')
300289
self.check_encode_type_errors(base64.b32encode)
301290

302-
# TODO: RUSTPYTHON
303-
@unittest.expectedFailure
304291
def test_b32decode(self):
305292
eq = self.assertEqual
306293
tests = {b'': b'',
@@ -375,8 +362,6 @@ def test_b32decode_error(self):
375362
with self.assertRaises(binascii.Error):
376363
base64.b32decode(data.decode('ascii'))
377364

378-
# TODO: RUSTPYTHON
379-
@unittest.expectedFailure
380365
def test_b16encode(self):
381366
eq = self.assertEqual
382367
eq(base64.b16encode(b'\x01\x02\xab\xcd\xef'), b'0102ABCDEF')
@@ -415,8 +400,6 @@ def test_b16decode(self):
415400
# Incorrect "padding"
416401
self.assertRaises(binascii.Error, base64.b16decode, '010')
417402

418-
# TODO: RUSTPYTHON
419-
@unittest.expectedFailure
420403
def test_a85encode(self):
421404
eq = self.assertEqual
422405

@@ -467,8 +450,6 @@ def test_a85encode(self):
467450
eq(base64.a85encode(b' '*6, foldspaces=True, adobe=False), b'y+<U')
468451
eq(base64.a85encode(b' '*5, foldspaces=True, adobe=False), b'y+9')
469452

470-
# TODO: RUSTPYTHON
471-
@unittest.expectedFailure
472453
def test_b85encode(self):
473454
eq = self.assertEqual
474455

@@ -503,8 +484,6 @@ def test_b85encode(self):
503484
self.check_other_types(base64.b85encode, b"www.python.org",
504485
b'cXxL#aCvlSZ*DGca%T')
505486

506-
# TODO: RUSTPYTHON
507-
@unittest.expectedFailure
508487
def test_a85decode(self):
509488
eq = self.assertEqual
510489

@@ -550,8 +529,6 @@ def test_a85decode(self):
550529
self.check_other_types(base64.a85decode, b'GB\\6`E-ZP=Df.1GEb>',
551530
b"www.python.org")
552531

553-
# TODO: RUSTPYTHON
554-
@unittest.expectedFailure
555532
def test_b85decode(self):
556533
eq = self.assertEqual
557534

vm/src/builtins/memory.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
bytesinner::bytes_to_hex,
1111
function::{FuncArgs, IntoPyObject, OptionalArg},
1212
protocol::{BufferDescriptor, BufferMethods, PyBuffer, PyMappingMethods, VecBuffer},
13+
sequence::SequenceOp,
1314
sliceable::wrap_index,
1415
stdlib::pystruct::FormatSpec,
1516
types::{AsBuffer, AsMapping, Comparable, Constructor, Hashable, PyComparisonOp},
@@ -629,8 +630,22 @@ impl PyMemoryView {
629630
));
630631
}
631632

632-
let shape_vec = shape.borrow_vec();
633-
let shape_ndim = shape_vec.len();
633+
let tup;
634+
let list;
635+
let list_borrow;
636+
let shape = match shape {
637+
Either::A(shape) => {
638+
tup = shape;
639+
tup.as_slice()
640+
}
641+
Either::B(shape) => {
642+
list = shape;
643+
list_borrow = list.borrow_vec();
644+
list_borrow.as_slice()
645+
}
646+
};
647+
648+
let shape_ndim = shape.len();
634649
// TODO: MAX_NDIM
635650
if self.desc.ndim() != 1 && shape_ndim != 1 {
636651
return Err(
@@ -651,7 +666,7 @@ impl PyMemoryView {
651666
let mut product_shape = itemsize;
652667
let mut dim_descriptor = Vec::with_capacity(shape_ndim);
653668

654-
for x in shape_vec.iter() {
669+
for x in shape.iter() {
655670
let x = usize::try_from_borrowed_object(vm, x)?;
656671

657672
if x > isize::MAX as usize / product_shape {
@@ -865,7 +880,7 @@ struct CastArgs {
865880
#[pyarg(any)]
866881
format: PyStrRef,
867882
#[pyarg(any, optional)]
868-
shape: OptionalArg<PyListRef>,
883+
shape: OptionalArg<Either<PyTupleRef, PyListRef>>,
869884
}
870885

871886
enum SubscriptNeedle {

0 commit comments

Comments
 (0)