Skip to content

Commit 0c10730

Browse files
committed
refactor: simplify code
1 parent 6fc4226 commit 0c10730

File tree

3 files changed

+8
-40
lines changed

3 files changed

+8
-40
lines changed

Lib/email/_policybase.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -307,29 +307,7 @@ def header_source_parse(self, sourcelines):
307307
308308
"""
309309
name, value = sourcelines[0].split(':', 1)
310-
311-
# Fixed: https://github.com/python/cpython/issues/124452
312-
#
313-
# Root cause: The function '_refold_parse_tree' in '_header_value_parse.py'.
314-
# If there is no WSP, it can't figure out how to wrap the text.
315-
# Therefore, it places the entire value directly after '\n', and because
316-
# there is a WSP after '<HeaderName>:', the WSP will be moved to the front
317-
# of the value according to RFC5322, section 2.2.3.
318-
#
319-
# However, the WSP is not part of the value; therefore, we must
320-
# remove it.
321-
322-
no_first_value = value.strip() == '' and len(sourcelines) > 1
323-
324-
# When using the compat32 policy, the value is '\n'. Therefore,
325-
# use an empty string if there is no value (without WSP and CRLF)
326-
# on the first line
327-
value = '' if no_first_value else value.lstrip(' \t')
328-
329-
if no_first_value and sourcelines[1][0] in ' \t':
330-
sourcelines[1] = sourcelines[1][1:]
331-
332-
value += ''.join(sourcelines[1:])
310+
value = ''.join((value, *sourcelines[1:])).lstrip(' \t\r\n')
333311
return (name, value.rstrip('\r\n'))
334312

335313
def header_store_parse(self, name, value):

Lib/email/policy.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,7 @@ def header_source_parse(self, sourcelines):
125125
126126
"""
127127
name, value = sourcelines[0].split(':', 1)
128-
no_first_value = not value.strip() and len(sourcelines) > 1
129-
130-
value = '' if no_first_value else value.lstrip(' \t')
131-
132-
if no_first_value and sourcelines[1][0] in ' \t':
133-
sourcelines[1] = sourcelines[1][1:]
134-
135-
value += ''.join(sourcelines[1:])
128+
value = ''.join((value, *sourcelines[1:])).lstrip(' \t\r\n')
136129
return (name, value.rstrip('\r\n'))
137130

138131
def header_store_parse(self, name, value):

Lib/test/test_email/test_message.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -958,15 +958,14 @@ def test_folding_with_utf8_encoding_8(self):
958958
b'123456789-123456789\n 123456789 Hello '
959959
b'=?utf-8?q?W=C3=B6rld!?= 123456789 123456789\n\n')
960960

961-
962961
def test_folding_with_short_nospace_1(self):
963962
# bpo-36520
964963
#
965964
# Fold a line that contains a long whitespace after
966965
# the fold point.
967966

968967
m = EmailMessage(policy.default)
969-
m['Message-ID'] = '12345678912345678123456789123456789123456789'
968+
m['Message-ID'] = '123456789'*3
970969
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
971970
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])
972971

@@ -977,12 +976,10 @@ def test_folding_with_long_nospace_default_policy_1(self):
977976
# to its original form without any modifications.
978977

979978
m = EmailMessage(policy.default)
980-
m['Message-ID'] = '12345678912345678123456789123456789123456789'\
981-
'12345678912345678123456789123456789123456789'
979+
message = '123456789' * 10
980+
m['Message-ID'] = message
982981
self.assertEqual(m.as_bytes(),
983-
b'Message-ID:\n 12345678912345678123456789123456'\
984-
b'78912345678912345678912345678123456789123456789'\
985-
b'123456789\n\n')
982+
f'Message-ID:\n {message}\n\n'.encode())
986983
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
987984
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])
988985

@@ -993,8 +990,8 @@ def test_folding_with_long_nospace_compat32_policy_1(self):
993990
# to its original form without any modifications.
994991

995992
m = EmailMessage(policy.compat32)
996-
m['Message-ID'] = '12345678912345678123456789123456789123456789'\
997-
'12345678912345678123456789123456789123456789'
993+
message = '123456789' * 10
994+
m['Message-ID'] = message
998995
parsed_msg = message_from_bytes(m.as_bytes(), policy=policy.default)
999996
self.assertEqual(parsed_msg['Message-ID'], m['Message-ID'])
1000997

0 commit comments

Comments
 (0)