Re: Is email.message.get() case insensitive for the header name?

2021-02-15 Thread dn via Python-list
On 15/02/2021 09.50, Chris Green wrote:
> It isn't clear from the documentation. Does email.message.get() care
> about the case of the header it's getting?
> 
> I checking mailing list mails and the "List-Id:" header is a bit
> 'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will
> email.message.get("List-Id:", "unknown") find all of them?


Case is (should be) irrelevant (as it is for most (parts of) Internet
Protocols).

Rather than the "Zen of Python", when it comes to IPs, "Postel's Law"
applies (https://en.wikipedia.org/wiki/Robustness_principle).

The library docs say it follows such.


Web.Refs:

https://docs.python.org/3/library/email.html
<<<
email — An email and MIME handling package

Source code: Lib/email/__init__.py

The email package is a library for managing email messages. It is
specifically not designed to do any sending of email messages to SMTP
(RFC 2821), NNTP, or other servers; those are functions of modules such
as smtplib and nntplib. The email package attempts to be as
RFC-compliant as possible, supporting RFC 5233 and RFC 6532...
>>>


https://tools.ietf.org/html/rfc6532.html
<<<
3.  Changes to Message Header Fields

   To permit non-ASCII Unicode characters in field values, the header
   definition in [RFC5322] is extended to support the new format.  The
   following sections specify the necessary changes to RFC 5322's ABNF.

   The syntax rules not mentioned below remain defined as in [RFC5322].

   Note that this protocol does not change rules in RFC 5322 for
   defining header field names.  The bodies of header fields are allowed
   to contain Unicode characters, but the header field names themselves
   must consist of ASCII characters only.

   Also note that messages in this format require the use of the
   SMTPUTF8 extension [RFC6531] to be transferred via SMTP.
...
>>>


https://tools.ietf.org/html/rfc5322
<<<
RFC 5322Internet Message Format October 2008

2.2.  Header Fields

   Header fields are lines beginning with a field name, followed by a
   colon (":"), followed by a field body, and terminated by CRLF.  A
   field name MUST be composed of printable US-ASCII characters (i.e.,
   characters that have values between 33 and 126, inclusive), except
   colon.  A field body may be composed of printable US-ASCII characters
   as well as the space (SP, ASCII value 32) and horizontal tab (HTAB,
   ASCII value 9) characters (together known as the white space
   characters, WSP).  A field body MUST NOT include CR and LF except
   when used in "folding" and "unfolding", as described in section
   2.2.3.  All field bodies MUST conform to the syntax described in
   sections 3 and 4 of this specification.
...
>>>
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is email.message.get() case insensitive for the header name?

2021-02-15 Thread Chris Green
Peter Otten <__pete...@web.de> wrote:
> On 14/02/2021 21:50, Chris Green wrote:
> 
> > It isn't clear from the documentation. Does email.message.get() care
> > about the case of the header it's getting?
> >
> > I checking mailing list mails and the "List-Id:" header is a bit
> > 'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will
> > email.message.get("List-Id:", "unknown") find all of them?
> Let's have a look:
> 
> >>> import email.message
> >>> email.message.get
> Traceback (most recent call last):
>   File "", line 1, in 
> email.message.get
> AttributeError: module 'email.message' has no attribute 'get'
> 
> 
> OK, you probably meant
> 
> >>> email.message.Message.get
> 
> 
> 
> Enter the inspect module for a quick glance at the method's source:
> 
> >>> import inspect
> >>> print(inspect.getsource(email.message.Message.get))
> def get(self, name, failobj=None):
> """Get a header value.
> 
> Like __getitem__() but return failobj instead of None when the
> field
> is missing.
> """
> name = name.lower()
> for k, v in self._headers:
> if k.lower() == name:
> return self.policy.header_fetch_parse(k, v)
> return failobj
> 
> Both the `name` argument and the header keys are converted to lowercase
> before the comparison, so yes, the method is case-insensitive (whether
> it should be casefold() doesn't matter for ascii-strings).

Excellent, thank you.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is email.message.get() case insensitive for the header name?

2021-02-15 Thread Peter Otten

On 14/02/2021 21:50, Chris Green wrote:


It isn't clear from the documentation. Does email.message.get() care
about the case of the header it's getting?

I checking mailing list mails and the "List-Id:" header is a bit
'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will
email.message.get("List-Id:", "unknown") find all of them?

Let's have a look:

>>> import email.message
>>> email.message.get
Traceback (most recent call last):
  File "", line 1, in 
email.message.get
AttributeError: module 'email.message' has no attribute 'get'


OK, you probably meant

>>> email.message.Message.get



Enter the inspect module for a quick glance at the method's source:

>>> import inspect
>>> print(inspect.getsource(email.message.Message.get))
def get(self, name, failobj=None):
"""Get a header value.

Like __getitem__() but return failobj instead of None when the
field
is missing.
"""
name = name.lower()
for k, v in self._headers:
if k.lower() == name:
return self.policy.header_fetch_parse(k, v)
return failobj

Both the `name` argument and the header keys are converted to lowercase
before the comparison, so yes, the method is case-insensitive (whether
it should be casefold() doesn't matter for ascii-strings).
--
https://mail.python.org/mailman/listinfo/python-list


Re: Is email.message.get() case insensitive for the header name?

2021-02-14 Thread MRAB

On 2021-02-14 20:50, Chris Green wrote:

It isn't clear from the documentation. Does email.message.get() care
about the case of the header it's getting?

I checking mailing list mails and the "List-Id:" header is a bit
'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will
email.message.get("List-Id:", "unknown") find all of them?

According to the specification that it follows, the field names are not 
case-sensitive.


Incidentally, the colon is a separator and not part of the field name.
--
https://mail.python.org/mailman/listinfo/python-list


Is email.message.get() case insensitive for the header name?

2021-02-14 Thread Chris Green
It isn't clear from the documentation. Does email.message.get() care
about the case of the header it's getting?

I checking mailing list mails and the "List-Id:" header is a bit
'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will
email.message.get("List-Id:", "unknown") find all of them?

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list