IMAP4.Literal 属性有什么作用,以及它是如何使用的?

huangapple go评论66阅读模式
英文:

What purpose did the IMAP4.Literal attribute serve and how was it used?

问题

Literal属性在imaplib库中用于处理IMAP协议中的文字字面量(literal)。文字字面量是一种用于发送邮件数据的方法,通常用于发送邮件的内容。在源代码中,Literal属性是一个正则表达式对象,用于匹配文字字面量的标识符和大小。正则表达式re.compile(b'.*{(?P<size>\\d+)}$', re.ASCII)用于匹配这些文字字面量。

在imaplib库中,Literal属性主要用于IMAP协议的底层实现,用于解析和处理接收到的邮件数据。当你使用IMAP协议从邮件服务器检索邮件时,这个属性可能用于解析邮件的文本内容。

尽管Literal属性在源代码中定义了,但根据注释,它已经不再使用,并且只是为了向后兼容性而保留。因此,通常情况下,你不需要直接操作或使用Literal属性。在imaplib库的后续版本中,可能已经有其他方式或工具取代了它,以更有效地处理邮件数据。

如果你想了解关于Literal属性的更多信息以及它是否被其他内容取代了,你可以查看imaplib库的更新文档或相关的资源,以获取更详细的信息。

英文:

I am handling connections to an IMAP server using the Python standard imaplib library.

I create an IMAP4 object like this:

import imaplib

M = imaplib.IMAP4_SSL(&#39;imap.gmail.com&#39;) # Open imaplib connection

The resulting object has these methods and attributes:

&gt;&gt;&gt; dir(M)
[&#39;Literal&#39;,
 &#39;PROTOCOL_VERSION&#39;,
 &#39;Untagged_status&#39;,
 &#39;_CRAM_MD5_AUTH&#39;,
 &#39;__class__&#39;,
 &#39;__delattr__&#39;,
 &#39;__dict__&#39;,
 &#39;__dir__&#39;,
 &#39;__doc__&#39;,
 &#39;__enter__&#39;,
 &#39;__eq__&#39;,
 &#39;__exit__&#39;,
 &#39;__format__&#39;,
 &#39;__ge__&#39;,
 &#39;__getattr__&#39;,
 &#39;__getattribute__&#39;,
 &#39;__gt__&#39;,
 &#39;__hash__&#39;,
 &#39;__init__&#39;,
 &#39;__init_subclass__&#39;,
 &#39;__le__&#39;,
 &#39;__lt__&#39;,
 &#39;__module__&#39;,
 &#39;__ne__&#39;,
 &#39;__new__&#39;,
 &#39;__reduce__&#39;,
 &#39;__reduce_ex__&#39;,
 &#39;__repr__&#39;,
 &#39;__setattr__&#39;,
 &#39;__sizeof__&#39;,
 &#39;__str__&#39;,
 &#39;__subclasshook__&#39;,
 &#39;__weakref__&#39;,
 &#39;_append_untagged&#39;,
 &#39;_check_bye&#39;,
 &#39;_cmd_log&#39;,
 &#39;_cmd_log_idx&#39;,
 &#39;_cmd_log_len&#39;,
 &#39;_command&#39;,
 &#39;_command_complete&#39;,
 &#39;_connect&#39;,
 &#39;_create_socket&#39;,
 &#39;_dump_ur&#39;,
 &#39;_encoding&#39;,
 &#39;_get_capabilities&#39;,
 &#39;_get_line&#39;,
 &#39;_get_response&#39;,
 &#39;_get_tagged_response&#39;,
 &#39;_log&#39;,
 &#39;_match&#39;,
 &#39;_mesg&#39;,
 &#39;_mode_ascii&#39;,
 &#39;_mode_utf8&#39;,
 &#39;_new_tag&#39;,
 &#39;_quote&#39;,
 &#39;_simple_command&#39;,
 &#39;_tls_established&#39;,
 &#39;_untagged_response&#39;,
 &#39;abort&#39;,
 &#39;append&#39;,
 &#39;authenticate&#39;,
 &#39;capabilities&#39;,
 &#39;capability&#39;,
 &#39;certfile&#39;,
 &#39;check&#39;,
 &#39;close&#39;,
 &#39;continuation_response&#39;,
 &#39;copy&#39;,
 &#39;create&#39;,
 &#39;debug&#39;,
 &#39;delete&#39;,
 &#39;deleteacl&#39;,
 &#39;enable&#39;,
 &#39;error&#39;,
 &#39;expunge&#39;,
 &#39;fetch&#39;,
 &#39;file&#39;,
 &#39;getacl&#39;,
 &#39;getannotation&#39;,
 &#39;getquota&#39;,
 &#39;getquotaroot&#39;,
 &#39;host&#39;,
 &#39;is_readonly&#39;,
 &#39;keyfile&#39;,
 &#39;list&#39;,
 &#39;literal&#39;,
 &#39;login&#39;,
 &#39;login_cram_md5&#39;,
 &#39;logout&#39;,
 &#39;lsub&#39;,
 &#39;mo&#39;,
 &#39;myrights&#39;,
 &#39;namespace&#39;,
 &#39;noop&#39;,
 &#39;open&#39;,
 &#39;partial&#39;,
 &#39;port&#39;,
 &#39;print_log&#39;,
 &#39;proxyauth&#39;,
 &#39;read&#39;,
 &#39;readline&#39;,
 &#39;readonly&#39;,
 &#39;recent&#39;,
 &#39;rename&#39;,
 &#39;response&#39;,
 &#39;search&#39;,
 &#39;select&#39;,
 &#39;send&#39;,
 &#39;setacl&#39;,
 &#39;setannotation&#39;,
 &#39;setquota&#39;,
 &#39;shutdown&#39;,
 &#39;sock&#39;,
 &#39;socket&#39;,
 &#39;sort&#39;,
 &#39;ssl_context&#39;,
 &#39;starttls&#39;,
 &#39;state&#39;,
 &#39;status&#39;,
 &#39;store&#39;,
 &#39;subscribe&#39;,
 &#39;tagged_commands&#39;,
 &#39;tagnum&#39;,
 &#39;tagpre&#39;,
 &#39;tagre&#39;,
 &#39;thread&#39;,
 &#39;uid&#39;,
 &#39;unselect&#39;,
 &#39;unsubscribe&#39;,
 &#39;untagged_responses&#39;,
 &#39;utf8_enabled&#39;,
 &#39;welcome&#39;,
 &#39;xatom&#39;]

By default, the Literal attribute contains a re (regular expression) object:

&gt;&gt;&gt; type(M.Literal)
re.Pattern
&gt;&gt;&gt; print(M.Literal)
re.compile(b&#39;.*{(?P&lt;size&gt;\\d+)}$&#39;, re.ASCII)

In the source code for imaplib.py, the attribute is defined on lines 113-114:

# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br&#39;.*{(?P&lt;size&gt;\d+)}$&#39;, re.ASCII)

What was it used for and how, and what, if anything, replaced it?

答案1

得分: 1

这个注释是在提交a6429db4b837dc49eb1bee42617798aebd7b43d4中添加的,该提交定义了一个类似的常量_Literal,用于定义一个名为Literal的实例属性,现在该属性用于替代原始的全局Literal

因此,模式仍然在使用,只是不再通过模块全局变量。

之前:

$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4^
$ grep Literal Lib/imaplib.py
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
            while self._match(Literal, dat):

之后:

$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4
$ grep Literal Lib/imaplib.py
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
_Literal = br'.*{(?P<size>\d+)}$'
        self.Literal = re.compile(_Literal, re.ASCII)
        self.Literal = re.compile(_Literal)
            while self._match(self.Literal, dat):
英文:

That comment was added in commit a6429db4b837dc49eb1bee42617798aebd7b43d4, which defined a similar constant _Literal that was used to define an instance attribute named Literal that is now used in place of the original global Literal.

So the pattern is still used, just not via a module global.

Before:

$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4\^
$ grep Literal Lib/imaplib.py
Literal = re.compile(br&#39;.*{(?P&lt;size&gt;\d+)}$&#39;, re.ASCII)
            while self._match(Literal, dat):

After:

$ git checkout a6429db4b837dc49eb1bee42617798aebd7b43d4
$ grep Literal Lib/imaplib.py
# Literal is no longer used; kept for backward compatibility.
Literal = re.compile(br&#39;.*{(?P&lt;size&gt;\d+)}$&#39;, re.ASCII)
_Literal = br&#39;.*{(?P&lt;size&gt;\d+)}$&#39;
        self.Literal = re.compile(_Literal, re.ASCII)
        self.Literal = re.compile(_Literal)
            while self._match(self.Literal, dat):

huangapple
  • 本文由 发表于 2023年2月8日 20:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定