[pypy-commit] pypy py3.7: Merged in py3.7-bpo-30103 (pull request #690)
arigo
pypy.commits at gmail.com
Mon Dec 30 04:15:15 EST 2019
Author: Armin Rigo <armin.rigo at gmail.com>
Branch: py3.7
Changeset: r98413:5ddc0990a778
Date: 2019-12-30 09:14 +0000
http://bitbucket.org/pypy/pypy/changeset/5ddc0990a778/
Log: Merged in py3.7-bpo-30103 (pull request #690)
Implement bpo-30103: Allow Uuencode in Python using backtick as zero
instead of space
diff --git a/pypy/module/binascii/interp_uu.py b/pypy/module/binascii/interp_uu.py
--- a/pypy/module/binascii/interp_uu.py
+++ b/pypy/module/binascii/interp_uu.py
@@ -66,25 +66,32 @@
return 0
_b2a_read._always_inline_ = True
- at unwrap_spec(bin='bufferstr')
-def b2a_uu(space, bin):
+def _b2a_write(res, num, backtick):
+ if backtick and not num:
+ res.append(chr(0x60))
+ else:
+ res.append(chr(0x20 + num))
+_b2a_write._always_inline_ = True
+
+ at unwrap_spec(bin='bufferstr', backtick=bool)
+def b2a_uu(space, bin, __kwonly__, backtick=False):
"Uuencode a line of data."
length = len(bin)
if length > 45:
raise_Error(space, 'At most 45 bytes at once')
res = StringBuilder(2 + ((length + 2) // 3) * 4)
- res.append(chr(0x20 + length))
+ _b2a_write(res, length, backtick)
for i in range(0, length, 3):
A = _b2a_read(bin, i)
B = _b2a_read(bin, i+1)
C = _b2a_read(bin, i+2)
#
- res.append(chr(0x20 + (A >> 2)))
- res.append(chr(0x20 + ((A & 0x3) << 4 | B >> 4)))
- res.append(chr(0x20 + ((B & 0xF) << 2 | C >> 6)))
- res.append(chr(0x20 + (C & 0x3F)))
+ _b2a_write(res, A >> 2, backtick)
+ _b2a_write(res, (A & 0x3) << 4 | B >> 4, backtick)
+ _b2a_write(res, (B & 0xF) << 2 | C >> 6, backtick)
+ _b2a_write(res, C & 0x3F , backtick)
res.append('\n')
return space.newbytes(res.build())
diff --git a/pypy/module/binascii/test/test_binascii.py b/pypy/module/binascii/test/test_binascii.py
--- a/pypy/module/binascii/test/test_binascii.py
+++ b/pypy/module/binascii/test/test_binascii.py
@@ -13,6 +13,7 @@
for input, expected in [
(b"!,_", b"3"),
(b" ", b""),
+ (b"`", b""),
(b"!", b"\x00"),
(b"!6", b"X"),
(b'"6', b"X\x00"),
@@ -30,12 +31,18 @@
(b')WAXR6UBA3#Q', b"\xde\x1e2[X\xa1L<@"),
(b'*WAXR6UBA3#Q!5', b"\xde\x1e2[X\xa1L<AT"),
(b'!,_', b'\x33'),
+ (b'$ $" P ', b'\x00\x01\x02\x03'),
+ (b'$``$"`P``', b'\x00\x01\x02\x03'),
]:
assert self.binascii.a2b_uu(input) == expected
assert self.binascii.a2b_uu(input + b' ') == expected
assert self.binascii.a2b_uu(input + b' ') == expected
assert self.binascii.a2b_uu(input + b' ') == expected
assert self.binascii.a2b_uu(input + b' ') == expected
+ assert self.binascii.a2b_uu(input + b'`') == expected
+ assert self.binascii.a2b_uu(input + b'``') == expected
+ assert self.binascii.a2b_uu(input + b'```') == expected
+ assert self.binascii.a2b_uu(input + b'````') == expected
assert self.binascii.a2b_uu(input + b'\n') == expected
assert self.binascii.a2b_uu(input + b'\r\n') == expected
assert self.binascii.a2b_uu(input + b' \r\n') == expected
@@ -84,7 +91,12 @@
(b"\xde\x1e2[X\xa1L<@", b')WAXR6UBA3#Q '),
(b"\xde\x1e2[X\xa1L<AT", b'*WAXR6UBA3#Q!5 '),
]:
- assert self.binascii.b2a_uu(input) == expected + b'\n'
+ for backtick in [None, True, False]:
+ if backtick is None:
+ assert self.binascii.b2a_uu(input) == expected + b'\n'
+ else:
+ really_expected = expected.replace(b' ', b'`') if backtick else expected
+ assert self.binascii.b2a_uu(input, backtick=backtick) == really_expected + b'\n'
def test_a2b_base64(self):
for input, expected in [
More information about the pypy-commit
mailing list