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

Skip to content
Prev Previous commit
Next Next commit
Comments from PR review, remove inner function
  • Loading branch information
matthieucan committed Jul 20, 2025
commit b15c4045927baf9ba763b29df9acbcbc0d80a7aa
11 changes: 4 additions & 7 deletions Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2398,22 +2398,19 @@ def get_section(value):
The caller should already have dealt with leading CFWS.

"""
def is_allowed_digit(c):
# We don't use str.isdigit because only 0-9 are accepted, not
# super-script and other types of digits.
return c in {'0','1','2','3','4','5','6','7','8','9'}

section = Section()
if not value or value[0] != '*':
raise errors.HeaderParseError("Expected section but found {}".format(
value))
section.append(ValueTerminal('*', 'section-marker'))
value = value[1:]
if not value or not is_allowed_digit(value[0]):
# We don't use str.isdigit because only 0-9 are accepted, not super-script
# and other types of digits.
if not value or not '0' <= value[0] <= '9':
raise errors.HeaderParseError("Expected section number but "
"found {}".format(value))
digits = ''
while value and is_allowed_digit(value[0]):
while value and '0' <= value[0] <= '9':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while value and '0' <= value[0] <= '9':
while value and ('0' <= value[0] <= '9'):

It will a bit clearer. Or you can still use a separate function to make it even cleareer. The bottleneck won't be the function call IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that, but not the separate function. It was my understanding that @StanFromIreland was leaning towards not having an inner function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, I was against the function to check if it is in a dictionary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Just moved it to a separate function for extra-clarity

digits += value[0]
value = value[1:]
if digits[0] == '0' and digits != '0':
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2991,7 +2991,6 @@ def mime_parameters_as_value(self,
'foo*0=bar; foo*²=baz',
[('foo', 'bar')],
[errors.InvalidHeaderDefect]),

}

@parameterize
Expand Down
Loading