8000 Fix bug in `binascii` `uu` encoding. Pass more related unit tests. (#… · RustPython/RustPython@bdf228e · GitHub
[go: up one dir, main page]

Skip to content

Commit bdf228e

Browse files
authored
Fix bug in binascii uu encoding. Pass more related unit tests. (#5160)
* Fix bug in binascii, passes more unit tests. * Pass more additional tests due to this PR.
1 parent c4b1cc6 commit bdf228e

File tree

4 files changed

+10
-32
lines changed

4 files changed

+10
-32
lines changed

Lib/test/test_binascii.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ def test_functions(self):
3838
self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
3939
self.assertRaises(TypeError, getattr(binascii, name))
4040

41-
# TODO: RUSTPYTHON
42-
@unittest.expectedFailure
4341
def test_returned_value(self):
4442
# Limit to the minimum of all limits (b2a_uu)
4543
MAX_ALL = 45
@@ -186,8 +184,6 @@ def assertInvalidLength(data):
186184
assertInvalidLength(b'a' * (4 * 87 + 1))
187185
10000 assertInvalidLength(b'A\tB\nC ??DE') # only 5 valid characters
188186

189-
# TODO: RUSTPYTHON
190-
@unittest.expectedFailure
191187
def test_uu(self):
192188
MAX_UU = 45
193189
for backtick in (True, False):
@@ -404,8 +400,6 @@ def test_unicode_b2a(self):
404400
# crc_hqx needs 2 arguments
405401
self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
406402

407-
# TODO: RUSTPYTHON
408-
@unittest.expectedFailure
409403
def test_unicode_a2b(self):
410404
# Unicode strings are accepted by a2b_* functions.
411405
MAX_ALL = 45

Lib/test/test_codecs.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,8 +2946,6 @@ def test_seek0(self):
29462946

29472947
class TransformCodecTest(unittest.TestCase):
29482948

2949-
# TODO: RUSTPYTHON
2950-
@unittest.expectedFailure
29512949
def test_basics(self):
29522950
binput = bytes(range(256))
29532951
for encoding in bytes_transform_encodings:
@@ -2959,8 +2957,6 @@ def test_basics(self):
29592957
self.assertEqual(size, len(o))
29602958
self.assertEqual(i, binput)
29612959

2962-
# TODO: RUSTPYTHON
2963-
@unittest.expectedFailure
29642960
def test_read(self):
29652961
for encoding in bytes_transform_encodings:
29662962
with self.subTest(encoding=encoding):
@@ -2969,8 +2965,6 @@ def test_read(self):
29692965
sout = reader.read()
29702966
self.assertEqual(sout, b"\x80")
29712967

2972-
# TODO: RUSTPYTHON
2973-
@unittest.expectedFailure
29742968
def test_readline(self):
29752969
for encoding in bytes_transform_encodings:
29762970
with self.subTest(encoding=encoding):

Lib/test/test_uu.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ def test_encode(self):
7575
with self.assertRaises(TypeError):
7676
uu.encode(inp, out, "t1", 0o644, True)
7777

78-
# TODO: RUSTPYTHON
79-
@unittest.expectedFailure
8078
def test_decode(self):
8179
for backtick in True, False:
8280
inp = io.BytesIO(encodedtextwrapped(0o666, "t1", backtick=backtick))
@@ -110,8 +108,6 @@ def test_missingbegin(self):
110108
except uu.Error as e:
111109
self.assertEqual(str(e), "No valid begin line found in input file")
112110

113-
# TODO: RUSTPYTHON
114-
@unittest.expectedFailure
115111
def test_garbage_padding(self):
116112
# Issue #22406
117113
encodedtext1 = (
@@ -163,17 +159,13 @@ def tearDown(self):
163159
sys.stdin = self.stdin
164160
sys.stdout = self.stdout
165161

166-
# TODO: RUSTPYTHON
167-
@unittest.expectedFailure
168162
def test_encode(self):
169163
sys.stdin = FakeIO(plaintext.decode("ascii"))
170164
sys.stdout = FakeIO()
171165
uu.encode("-", "-", "t1", 0o666)
172166
self.assertEqual(sys.stdout.getvalue(),
173167
encodedtextwrapped(0o666, "t1").decode("ascii"))
174168

175-
# TODO: RUSTPYTHON
176-
@unittest.expectedFailure
177169
def test_decode(self):
178170
sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
179171
sys.stdout = FakeIO()
@@ -192,8 +184,6 @@ def setUp(self):
192184
self.addCleanup(os_helper.unlink, self.tmpin)
193185
self.addCleanup(os_helper.unlink, self.tmpout)
194186

195-
# TODO: RUSTPYTHON
196-
@unittest.expectedFailure
197187
def test_encode(self):
198188
with open(self.tmpin, 'wb') as fin:
199189
fin.write(plaintext)
@@ -212,8 +202,6 @@ def test_encode(self):
212202
s = fout.read()
213203
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
214204

215-
# TODO: RUSTPYTHON
216-
@unittest.expectedFailure
217205
def test_decode(self):
218206
with open(self.tmpin, 'wb') as f:
219207
f.write(encodedtextwrapped(0o644, self.tmpout))
@@ -226,8 +214,6 @@ def test_decode(self):
226214
self.assertEqual(s, plaintext)
227215
# XXX is there an xp way to verify the mode?
228216

229-
# TODO: RUSTPYTHON
230-
@unittest.expectedFailure
231217
def test_decode_filename(self):
232218
with open(self.tmpin, 'wb') as f:
233219
f.write(encodedtextwrapped(0o644, self.tmpout))

stdlib/src/binascii.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ mod decl {
289289
if [b'\r', b'\n'].contains(c) {
290290
return Ok(0);
291291
}
292-
return Err(vm.new_value_error("Illegal char".to_string()));
292+
return Err(super::new_binascii_error("Illegal char".to_owned(), vm));
293293
}
294294
Ok((*c - b' ') & 0x3f)
295295
}
@@ -645,7 +645,8 @@ mod decl {
645645

646646
// Allocate the buffer
647647
let mut res = Vec::<u8>::with_capacity(length);
648-
let trailing_garbage_error = || Err(vm.new_value_error("Trailing garbage".to_string()));
648+
let trailing_garbage_error =
649+
|| Err(super::new_binascii_error("Trailing garbage".to_owned(), vm));
649650

650651
for chunk in b.get(1..).unwrap_or_default().chunks(4) {
651652
let (char_a, char_b, char_c, char_d) = {
@@ -666,7 +667,7 @@ mod decl {
666667
}
667668

668669
if res.len() < length {
669-
res.push((char_b & 0xf) | char_c >> 2);
670+
res.push((char_b & 0xf) << 4 | char_c >> 2);
670671
} else if char_c != 0 {
671672
return trailing_garbage_error();
672673
}
@@ -688,7 +689,7 @@ mod decl {
688689

689690
#[derive(FromArgs)]
690691
struct BacktickArg {
691-
#[pyarg(named, default = "true")]
692+
#[pyarg(named, default = "false")]
692693
backtick: bool,
693694
}
694695

@@ -700,7 +701,7 @@ mod decl {
700701
) -> PyResult<Vec<u8>> {
701702
#[inline]
702703
fn uu_b2a(num: u8, backtick: bool) -> u8 {
703-
if backtick && num != 0 {
704+
if backtick && num == 0 {
704705
0x60
705706
} else {
706707
b' ' + num
@@ -710,7 +711,10 @@ mod decl {
710711
data.with_ref(|b| {
711712
let length = b.len();
712713
if length > 45 {
713-
return Err(vm.new_value_error("At most 45 bytes at once".to_string()));
714+
return Err(super::new_binascii_error(
715+
"At most 45 bytes at once".to_owned(),
716+
vm,
717+
));
714718
}
715719
let mut res = Vec::<u8>::with_capacity(2 + ((length + 2) / 3) * 4);
716720
res.push(uu_b2a(length as u8, backtick));

0 commit comments

Comments
 (0)
0