8000 Fix bug in `binascii` `uu` encoding. Pass more related unit tests. by kingiler · Pull Request #5160 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Fix bug in binascii uu encoding. Pass more related unit tests. #5160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand Down
14 changes: 0 additions & 14 deletions Lib/test/test_uu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -163,17 +159,13 @@ 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()
uu.encode("-", "-", "t1", 0o666)
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()
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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))
Expand Down
16 changes: 10 additions & 6 deletions stdlib/src/binascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -645,7 +645,8 @@ mod decl {

// Allocate the buffer
let mut res = Vec::<u8>::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) = {
Expand All @@ -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();
}
Expand All @@ -688,7 +689,7 @@ mod decl {

#[derive(FromArgs)]
struct BacktickArg {
#[pyarg(named, default = "true")]
#[pyarg(named, default = "false")]
backtick: bool,
}

Expand All @@ -700,7 +701,7 @@ mod decl {
) -> PyResult<Vec<u8>> {
#[inline]
fn uu_b2a(num: u8, backtick: bool) -> u8 {
if backtick && num != 0 {
if backtick && num == 0 {
0x60
} else {
b' ' + num
Expand All @@ -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::<u8>::with_capacity(2 + ((length + 2) / 3) * 4);
res.push(uu_b2a(length as u8, backtick));
Expand Down
0