Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c202d93

Browse files
committed
Use True/False everywhere, and other code cleanups.
1 parent f776e69 commit c202d93

2 files changed

Lines changed: 30 additions & 18 deletions

File tree

Lib/email/base64MIME.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
# See also Charset.py
4242
MISC_LEN = 7
4343

44+
try:
45+
True, False
46+
except NameError:
47+
True = 1
48+
False = 0
49+
4450

4551

4652
# Helpers
@@ -56,8 +62,8 @@ def base64_len(s):
5662

5763

5864

59-
def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
60-
eol=NL):
65+
def header_encode(header, charset='iso-8859-1', keep_eols=False,
66+
maxlinelen=76, eol=NL):
6167
"""Encode a single header line with Base64 encoding in a given charset.
6268
6369
Defined in RFC 2045, this Base64 encoding is identical to normal Base64
@@ -69,7 +75,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
6975
7076
End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
7177
to the canonical email line separator \\r\\n unless the keep_eols
72-
parameter is set to true (the default is false).
78+
parameter is True (the default is False).
7379
7480
Each line of the header will be terminated in the value of eol, which
7581
defaults to "\\n". Set this to "\\r\\n" if you are using the result of
@@ -106,7 +112,7 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
106112
lines = []
107113
for line in base64ed:
108114
# Ignore the last character of each line if it is a newline
109-
if line[-1] == NL:
115+
if line.endswith(NL):
110116
line = line[:-1]
111117
# Add the chrome
112118
lines.append('=?%s?b?%s?=' % (charset, line))
@@ -117,13 +123,13 @@ def header_encode(header, charset='iso-8859-1', keep_eols=0, maxlinelen=76,
117123

118124

119125

120-
def encode(s, binary=1, maxlinelen=76, eol=NL):
126+
def encode(s, binary=True, maxlinelen=76, eol=NL):
121127
"""Encode a string with base64.
122128
123129
Each line will be wrapped at, at most, maxlinelen characters (defaults to
124130
76 characters).
125131
126-
If binary is false, end-of-line characters will be converted to the
132+
If binary is False, end-of-line characters will be converted to the
127133
canonical email end-of-line sequence \\r\\n. Otherwise they will be left
128134
verbatim (this is the default).
129135
@@ -143,7 +149,7 @@ def encode(s, binary=1, maxlinelen=76, eol=NL):
143149
# BAW: should encode() inherit b2a_base64()'s dubious behavior in
144150
# adding a newline to the encoded string?
145151
enc = b2a_base64(s[i:i + max_unencoded])
146-
if enc[-1] == NL and eol <> NL:
152+
if enc.endswith(NL) and eol <> NL:
147153
enc = enc[:-1] + eol
148154
encvec.append(enc)
149155
return EMPTYSTRING.join(encvec)

Lib/email/quopriMIME.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@
3838
hqre = re.compile(r'[^-a-zA-Z0-9!*+/ ]')
3939
bqre = re.compile(r'[^ !-<>-~\t]')
4040

41+
try:
42+
True, False
43+
except NameError:
44+
True = 1
45+
False = 0
46+
4147

4248

4349
# Helpers
4450
def header_quopri_check(c):
45-
"""Return true if the character should be escaped with header quopri."""
46-
return hqre.match(c) and 1
51+
"""Return True if the character should be escaped with header quopri."""
52+
return hqre.match(c) and True
4753

4854

4955
def body_quopri_check(c):
50-
"""Return true if the character should be escaped with body quopri."""
51-
return bqre.match(c) and 1
56+
"""Return True if the character should be escaped with body quopri."""
57+
return bqre.match(c) and True
5258

5359

5460
def header_quopri_len(s):
@@ -92,8 +98,8 @@ def quote(c):
9298

9399

94100

95-
def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76,
96-
eol=NL):
101+
def header_encode(header, charset="iso-8859-1", keep_eols=False,
102+
maxlinelen=76, eol=NL):
97103
"""Encode a single header line with quoted-printable (like) encoding.
98104
99105
Defined in RFC 2045, this `Q' encoding is similar to quoted-printable, but
@@ -114,7 +120,7 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76,
114120
115121
End-of-line characters (\\r, \\n, \\r\\n) will be automatically converted
116122
to the canonical email line separator \\r\\n unless the keep_eols
117-
parameter is set to true (the default is false).
123+
parameter is True (the default is False).
118124
119125
Each line of the header will be terminated in the value of eol, which
120126
defaults to "\\n". Set this to "\\r\\n" if you are using the result of
@@ -151,10 +157,10 @@ def header_encode(header, charset="iso-8859-1", keep_eols=0, maxlinelen=76,
151157

152158

153159

154-
def encode(body, binary=0, maxlinelen=76, eol=NL):
160+
def encode(body, binary=False, maxlinelen=76, eol=NL):
155161
"""Encode with quoted-printable, wrapping at maxlinelen characters.
156162
157-
If binary is false (the default), end-of-line characters will be converted
163+
If binary is False (the default), end-of-line characters will be converted
158164
to the canonical email end-of-line sequence \\r\\n. Otherwise they will
159165
be left verbatim.
160166
@@ -213,7 +219,7 @@ def encode(body, binary=0, maxlinelen=76, eol=NL):
213219
# Now at end of line..
214220
if prev and prev in ' \t':
215221
# Special case for whitespace at end of file
216-
if lineno+1 == len(lines):
222+
if lineno + 1 == len(lines):
217223
prev = quote(prev)
218224
if len(encoded_line) + len(prev) > maxlinelen:
219225
encoded_body += encoded_line + '=' + eol + prev
@@ -283,7 +289,7 @@ def decode(encoded, eol=NL):
283289
if i == n:
284290
decoded += eol
285291
# Special case if original string did not end with eol
286-
if encoded[-1] <> eol and decoded[-1] == eol:
292+
if not encoded.endswith(eol) and decoded.endswith(eol):
287293
decoded = decoded[:-1]
288294
return decoded
289295

0 commit comments

Comments
 (0)