From 45eb7d8d91069ad2ed0a8dc69da43cf364696806 Mon Sep 17 00:00:00 2001 From: kingler <68145845+kingiler@users.noreply.github.com> Date: Fri, 9 Feb 2024 11:38:21 +0000 Subject: [PATCH 1/2] Fix bug in binascii, passes more unit tests. --- Lib/test/test_binascii.py | 6 ------ stdlib/src/binascii.rs | 16 ++++++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index fd52b9895c..4ae89837cc 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -38,8 +38,6 @@ def test_functions(self): self.assertTrue(hasattr(getattr(binascii, name), '__call__')) self.assertRaises(TypeError, getattr(binascii, name)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_returned_value(self): # Limit to the minimum of all limits (b2a_uu) MAX_ALL = 45 @@ -186,8 +184,6 @@ def assertInvalidLength(data): assertInvalidLength(b'a' * (4 * 87 + 1)) assertInvalidLength(b'A\tB\nC ??DE') # only 5 valid characters - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_uu(self): MAX_UU = 45 for backtick in (True, False): @@ -404,8 +400,6 @@ def test_unicode_b2a(self): # crc_hqx needs 2 arguments self.assertRaises(TypeError, binascii.crc_hqx, "test", 0) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_unicode_a2b(self): # Unicode strings are accepted by a2b_* functions. MAX_ALL = 45 diff --git a/stdlib/src/binascii.rs b/stdlib/src/binascii.rs index d7467fc8a5..d348049f4c 100644 --- a/stdlib/src/binascii.rs +++ b/stdlib/src/binascii.rs @@ -289,7 +289,7 @@ mod decl { if [b'\r', b'\n'].contains(c) { return Ok(0); } - return Err(vm.new_value_error("Illegal char".to_string())); + return Err(super::new_binascii_error("Illegal char".to_owned(), vm)); } Ok((*c - b' ') & 0x3f) } @@ -645,7 +645,8 @@ mod decl { // Allocate the buffer let mut res = Vec::::with_capacity(length); - let trailing_garbage_error = || Err(vm.new_value_error("Trailing garbage".to_string())); + let trailing_garbage_error = + || Err(super::new_binascii_error("Trailing garbage".to_owned(), vm)); for chunk in b.get(1..).unwrap_or_default().chunks(4) { let (char_a, char_b, char_c, char_d) = { @@ -666,7 +667,7 @@ mod decl { } if res.len() < length { - res.push((char_b & 0xf) | char_c >> 2); + res.push((char_b & 0xf) << 4 | char_c >> 2); } else if char_c != 0 { return trailing_garbage_error(); } @@ -688,7 +689,7 @@ mod decl { #[derive(FromArgs)] struct BacktickArg { - #[pyarg(named, default = "true")] + #[pyarg(named, default = "false")] backtick: bool, } @@ -700,7 +701,7 @@ mod decl { ) -> PyResult> { #[inline] fn uu_b2a(num: u8, backtick: bool) -> u8 { - if backtick && num != 0 { + if backtick && num == 0 { 0x60 } else { b' ' + num @@ -710,7 +711,10 @@ mod decl { data.with_ref(|b| { let length = b.len(); if length > 45 { - return Err(vm.new_value_error("At most 45 bytes at once".to_string())); + return Err(super::new_binascii_error( + "At most 45 bytes at once".to_owned(), + vm, + )); } let mut res = Vec::::with_capacity(2 + ((length + 2) / 3) * 4); res.push(uu_b2a(length as u8, backtick)); From fd66f11dbaa90e164d946c089cace31bd63b1b76 Mon Sep 17 00:00:00 2001 From: kingler <68145845+kingiler@users.noreply.github.com> Date: Fri, 9 Feb 2024 12:37:29 +0000 Subject: [PATCH 2/2] Pass more additional tests due to this PR. --- Lib/test/test_codecs.py | 6 ------ Lib/test/test_uu.py | 14 -------------- 2 files changed, 20 deletions(-) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 3a9c6d2741..0b972a58a5 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -2946,8 +2946,6 @@ def test_seek0(self): class TransformCodecTest(unittest.TestCase): - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_basics(self): binput = bytes(range(256)) for encoding in bytes_transform_encodings: @@ -2959,8 +2957,6 @@ def test_basics(self): self.assertEqual(size, len(o)) self.assertEqual(i, binput) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_read(self): for encoding in bytes_transform_encodings: with self.subTest(encoding=encoding): @@ -2969,8 +2965,6 @@ def test_read(self): sout = reader.read() self.assertEqual(sout, b"\x80") - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_readline(self): for encoding in bytes_transform_encodings: with self.subTest(encoding=encoding): diff --git a/Lib/test/test_uu.py b/Lib/test/test_uu.py index 6b0b2f24f5..f71d877365 100644 --- a/Lib/test/test_uu.py +++ b/Lib/test/test_uu.py @@ -75,8 +75,6 @@ def test_encode(self): with self.assertRaises(TypeError): uu.encode(inp, out, "t1", 0o644, True) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_decode(self): for backtick in True, False: inp = io.BytesIO(encodedtextwrapped(0o666, "t1", backtick=backtick)) @@ -110,8 +108,6 @@ def test_missingbegin(self): except uu.Error as e: self.assertEqual(str(e), "No valid begin line found in input file") - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_garbage_padding(self): # Issue #22406 encodedtext1 = ( @@ -163,8 +159,6 @@ def tearDown(self): sys.stdin = self.stdin sys.stdout = self.stdout - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_encode(self): sys.stdin = FakeIO(plaintext.decode("ascii")) sys.stdout = FakeIO() @@ -172,8 +166,6 @@ def test_encode(self): self.assertEqual(sys.stdout.getvalue(), encodedtextwrapped(0o666, "t1").decode("ascii")) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_decode(self): sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii")) sys.stdout = FakeIO() @@ -192,8 +184,6 @@ def setUp(self): self.addCleanup(os_helper.unlink, self.tmpin) self.addCleanup(os_helper.unlink, self.tmpout) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_encode(self): with open(self.tmpin, 'wb') as fin: fin.write(plaintext) @@ -212,8 +202,6 @@ def test_encode(self): s = fout.read() self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_decode(self): with open(self.tmpin, 'wb') as f: f.write(encodedtextwrapped(0o644, self.tmpout)) @@ -226,8 +214,6 @@ def test_decode(self): self.assertEqual(s, plaintext) # XXX is there an xp way to verify the mode? - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_decode_filename(self): with open(self.tmpin, 'wb') as f: f.write(encodedtextwrapped(0o644, self.tmpout))