10
10
import uu
11
11
import io
12
12
13
- plaintext = b"The smooth-scaled python crept over the sleeping dog \n "
13
+ plaintext = b"The symbols on top of your keyboard are !@#$%^&*()_+|~ \n "
14
14
15
15
encodedtext = b"""\
16
- M5&AE('-M;V]T: \" US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
17
- (:6YG(&1O9PH """
16
+ M5&AE('-Y;6)O;',@;VX@=&]P(&]F('EO=7(@:V5Y8F]A<F0@87)E("% (R0E
17
+ *7B8J*"E?*WQ^"@ """
18
18
19
19
# Stolen from io.py
20
20
class FakeIO (io .TextIOWrapper ):
@@ -44,9 +44,14 @@ def getvalue(self):
44
44
return self .buffer .getvalue ().decode (self ._encoding , self ._errors )
45
45
46
46
47
- def encodedtextwrapped (mode , filename ):
48
- return (bytes ("begin %03o %s\n " % (mode , filename ), "ascii" ) +
49
- encodedtext + b"\n \n end\n " )
47
+ def encodedtextwrapped (mode , filename , backtick = False ):
48
+ if backtick :
49
+ res = (bytes ("begin %03o %s\n " % (mode , filename ), "ascii" ) +
50
+ encodedtext .replace (b' ' , b'`' ) + b"\n `\n end\n " )
51
+ else :
52
+ res = (bytes ("begin %03o %s\n " % (mode , filename ), "ascii" ) +
53
+ encodedtext + b"\n \n end\n " )
54
+ return res
50
55
51
56
class UUTest (unittest .TestCase ):
52
57
@@ -59,20 +64,27 @@ def test_encode(self):
59
64
out = io .BytesIO ()
60
65
uu .encode (inp , out , "t1" , 0o644 )
61
66
self .assertEqual (out .getvalue (), encodedtextwrapped (0o644 , "t1" ))
67
+ inp = io .BytesIO (plaintext )
68
+ out = io .BytesIO ()
69
+ uu .encode (inp , out , "t1" , backtick = True )
70
+ self .assertEqual (out .getvalue (), encodedtextwrapped (0o666 , "t1" , True ))
71
+ with self .assertRaises (TypeError ):
72
+ uu .encode (inp , out , "t1" , 0o644 , True )
62
73
63
74
def test_decode (self ):
64
- inp = io .BytesIO (encodedtextwrapped (0o666 , "t1" ))
65
- out = io .BytesIO ()
66
- uu .decode (inp , out )
67
- self .assertEqual (out .getvalue (), plaintext )
68
- inp = io .BytesIO (
69
- b"UUencoded files may contain many lines,\n " +
70
- b"even some that have 'begin' in them.\n " +
71
- encodedtextwrapped (0o666 , "t1" )
72
- )
73
- out = io .BytesIO ()
74
- uu .decode (inp , out )
75
- self .assertEqual (out .getvalue (), plaintext )
75
+ for backtick in True , False :
76
+ inp = io .BytesIO (encodedtextwrapped (0o666 , "t1" , backtick = backtick ))
77
+ out = io .BytesIO ()
78
+ uu .decode (inp , out )
79
+ self .assertEqual (out .getvalue (), plaintext )
80
+ inp = io .BytesIO (
81
+ b"UUencoded files may contain many lines,\n " +
82
+ b"even some that have 'begin' in them.\n " +
83
+ encodedtextwrapped (0o666 , "t1" , backtick = backtick )
84
+ )
85
+ out = io .BytesIO ()
86
+ uu .decode (inp , out )
87
+ self .assertEqual (out .getvalue (), plaintext )
76
88
77
89
def test_truncatedinput (self ):
78
90
inp = io .BytesIO (b"begin 644 t1\n " + encodedtext )
@@ -94,25 +106,33 @@ def test_missingbegin(self):
94
106
95
107
def test_garbage_padding (self ):
96
108
# Issue #22406
97
- encodedtext = (
109
+ encodedtext1 = (
98
110
b"begin 644 file\n "
99
111
# length 1; bits 001100 111111 111111 111111
100
112
b"\x21 \x2C \x5F \x5F \x5F \n "
101
113
b"\x20 \n "
102
114
b"end\n "
103
115
)
116
+ encodedtext2 = (
117
+ b"begin 644 file\n "
118
+ # length 1; bits 001100 111111 111111 111111
119
+ b"\x21 \x2C \x5F \x5F \x5F \n "
120
+ b"\x60 \n "
121
+ b"end\n "
122
+ )
104
123
plaintext = b"\x33 " # 00110011
105
124
106
- with self .subTest ("uu.decode()" ):
107
- inp = io .BytesIO (encodedtext )
108
- out = io .BytesIO ()
109
- uu .decode (inp , out , quiet = True )
110
- self .assertEqual (out .getvalue (), plaintext )
125
+ for encodedtext in encodedtext1 , encodedtext2 :
126
+ with self .subTest ("uu.decode()" ):
127
+ inp = io .BytesIO (encodedtext )
128
+ out = io .BytesIO ()
129
+ uu .decode (inp , out , quiet = True )
130
+ self .assertEqual (out .getvalue (), plaintext )
111
131
112
- with self .subTest ("uu_codec" ):
113
- import codecs
114
- decoded = codecs .decode (encodedtext , "uu_codec" )
115
- self .assertEqual (decoded , plaintext )
132
+ with self .subTest ("uu_codec" ):
133
+ import codecs
134
+ decoded = codecs .decode (encodedtext , "uu_codec" )
135
+ self .assertEqual (decoded , plaintext )
116
136
117
137
class UUStdIOTest (unittest .TestCase ):
118
138
@@ -250,11 +270,6 @@ def test_decodetwice(self):
250
270
finally :
251
271
self ._kill (f )
252
272
253
- def test_main ():
254
- support .run_unittest (UUTest ,
255
- UUStdIOTest ,
256
- UUFileTest ,
257
- )
258
273
259
274
if __name__ == "__main__" :
260
- test_main ()
275
+ unittest . main ()
0 commit comments