sys
- 增加了
isinterned()
函数用于检测字符串是否被内部化。 此函数不保证在所有的 Python 实现中均存在。 (由 Serhiy Storchaka 在 gh-78573 [https://github.com/python/cpython/issues/78573] 中贡献。)
tempfile
- 在 Windows 上,
tempfile.mkdtemp()
所使用的默认模式0o700
由于os.mkdir()
的更改现在将限制对新目录的访问。 这是对 CVE 2024-4030 [https://www.cve.org/CVERecord?id=CVE-2024-4030] 的缓解措施。 (由 Steve Dower 在 gh-118486 [https://github.com/python/cpython/issues/118486] 中贡献。)
time
在 Windows 上,
monotonic()
现在将使用精度为 1 微秒的QueryPerformanceCounter()
时钟,而不是精度只有 15.6 毫秒的GetTickCount64()
时钟。 (由 Victor Stinner 在 gh-88494 [https://github.com/python/cpython/issues/88494] 中贡献。)在 Windows 上,
time()
现在将使用精度为 1 微秒的GetSystemTimePreciseAsFileTime()
时钟,代替精度为 15.6 毫秒的GetSystemTimeAsFileTime()
时钟。 (由 Victor Stinner 在 gh-63207 [https://github.com/python/cpython/issues/63207] 中贡献。)
tkinter
增加了
tkinter
控件方法:tk_busy_hold()
,tk_busy_configure()
,tk_busy_cget()
,tk_busy_forget()
,tk_busy_current()
和tk_busy_status()
。 (由 Miguel, klappnase 和 Serhiy Storchaka 在 gh-72684 [https://github.com/python/cpython/issues/72684] 中贡献。)现在
tkinter
控件wm_attributes()
接受不带负号前缀的属性名称来获取窗口属性,例如w.wm_attributes('alpha')
并允许指定属性和值以关键字参数形式来设置,例如w.wm_attributes(alpha=0.5)
。 (由 Serhiy Storchaka 在 gh-43457 [https://github.com/python/cpython/issues/43457] 中贡献。)通过使用新的可选关键字形参 return_python_dict,现在
wm_attributes()
可将属性作为dict
返回。 (由 Serhiy Storchaka 在 gh-43457 [https://github.com/python/cpython/issues/43457] 中贡献。)现在当使用新的可选仅限关键字形参 return_ints 时
Text.count()
可以返回一个简单的int
。 在其他情况下,将以 1 个元素的元组形式返回单个计数值或者None
。 (由 Serhiy Storchaka 在 gh-97928 [https://github.com/python/cpython/issues/97928] 中贡献。)在
tkinter.ttk.Style
的element_create()
方法中增加了对 "vsapi" 元素类型的支持。 (由 Serhiy Storchaka 在 gh-68166 [https://github.com/python/cpython/issues/68166] 中贡献。)为 Tkinter 的控件增加了
after_info()
方法。 (由 Cheryl Sabella 在 gh-77020 [https://github.com/python/cpython/issues/77020] 中贡献。)为
PhotoImage
新增copy_replace()
方法用于将一个图像的某个区域拷贝到另一个图像,可能带有像素缩放、子采样,或两者皆有。 (由 Serhiy Storchaka 在 gh-118225 [https://github.com/python/cpython/issues/118225] 中贡献。)为
PhotoImage
的方法copy()
,zoom()
和subsample()
增加了 from_coords 形参。 为PhotoImage
的方法copy()
增加了 zoom 和 subsample 形参。 (由 Serhiy Storchaka 在 gh-118225 [https://github.com/python/cpython/issues/118225] 中贡献。)增加了
PhotoImage
方法read()
用于从文件读取图像以及data()
用于获取图像数据。 为write()
方法增加了 background 和 grayscale 形参。 (由 Serhiy Storchaka 在 gh-118271 [https://github.com/python/cpython/issues/118271] 中贡献。).)
回溯
为
TracebackException
增加了exc_type_str
属性,它用于保存 exc_type 字符串表示。 弃用了exc_type
属性,它用于保存类型对象本身。 增加了 save_exc_type 形参 (默认值为True
) 用于指明exc_type
是否应当被保存。 (由 Irit Katriel 在 gh-112332 [https://github.com/python/cpython/issues/112332] 中贡献。)为
TracebackException.format_exception_only()
增加了新的 show_group 仅限关键字形参用于(递归地)格式化BaseExceptionGroup
实例中嵌套的异常。 (由 Irit Katriel 在 gh-105292 [https://github.com/python/cpython/issues/105292] 中贡献。)
types
- 现在
SimpleNamespace
可以接受单个位置参数来初始化命名空间的各个参数值。 该参数必须为映射或键值对的可迭代对象。 (由 Serhiy Storchaka 在 gh-108191 [https://github.com/python/cpython/issues/108191] 中贡献。)
typing
PEP 705 [https://peps.python.org/pep-0705/]: 增加
ReadOnly
,一个针对类型检查器的特殊类型结构,用于将TypedDict
的项标记为只读。PEP 742 [https://peps.python.org/pep-0742/]: 增加
TypeIs
,一个可被用于指示类型检查器如何细化类型的类型结构。增加
NoDefault
,一个用于代表typing
模块中某些形参的默认值的哨兵对象。 (由 Jelle Zijlstra 在 gh-116126 [https://github.com/python/cpython/issues/116126] 中贡献。)增加
get_protocol_members()
用于返回定义一个typing.Protocol
的成员的集合。 (由 Jelle Zijlstra 在 gh-104873 [https://github.com/python/cpython/issues/104873] 中贡献。)增加
is_protocol()
用于检查一个类是否属于Protocol
。 (由 Jelle Zijlstra 在 gh-104873 [https://github.com/python/cpython/issues/104873] 中贡献。)现在
ClassVar
可以被嵌套在Final
中,反之亦然。 (由 Mehdi Drissi 在 gh-89547 [https://github.com/python/cpython/issues/89547] 中贡献。)
unicodedata
- 将 Unicode 数据库更新到 15.1.0 版 [https://www.unicode.org/versions/Unicode15.1.0/]。 (由 James Gerity 在 gh-109559 [https://github.com/python/cpython/issues/109559] 中贡献。)
venv
- 增加了对在虚拟环境目录中添加源码控制管理 (SCM) 忽略文件的支持。 在默认情况下,Git 已受到支持。 此特性是以可被扩展为支持其他 SCM 的通过 API 选择启用 (
EnvBuilder
和create()
),并通过 CLI 使用--without-scm-ignore-files
选择禁用的方式实现的。 (由 Brett Cannon 在 gh-108125 [https://github.com/python/cpython/issues/108125] 中贡献。)
warnings
- PEP 702 [https://peps.python.org/pep-0702/]: 新的
warnings.deprecated()
装饰器提供了一种将弃用消息传送给 static type checker 并在使用已弃用的类和函数时发出警告的方式。 当被装饰的函数或类在运行时被使用时也可以发出DeprecationWarning
。 (由 Jelle Zijlstra 在 gh-104003 [https://github.com/python/cpython/issues/104003] 中贡献。)
xml
通过添加五个新方法来允许控制 Expat >=2.6.0 重解析延迟 ( CVE 2023-52425 [https://www.cve.org/CVERecord?id=CVE-2023-52425]):
(由 Sebastian Pipping 在 gh-115623 [https://github.com/python/cpython/issues/115623] 中贡献。)
- 为
iterparse()
所返回的迭代器增加了close()
方法用于执行显式的清理。 (由 Serhiy Storchaka 在 gh-69893 [https://github.com/python/cpython/issues/69893] 中贡献。)
zipimport
- 增加了对 ZIP64#ZIP64) [https://en.wikipedia.org/wiki/Zip_(file_format)#ZIP64] 格式的文件的支持。 大家都喜欢更庞大的数据,对吧? (由 Tim Hatch 在 gh-94146 [https://github.com/python/cpython/issues/94146] 中贡献。).)
性能优化
一些标准库模块的导入时间得到了显著改善。 例如,
typing
模块的导入时间通过移除对re
和contextlib
的依赖而减少了大约三分之一。 其他获得导入时间加速的模块包括email.utils
,enum
,functools
,importlib.metadata
和threading
。 (由 Alex Waygood, Shantanu Jain, Adam Turner, Daniel Hollas 等人在 gh-109653 [https://github.com/python/cpython/issues/109653] 中贡献。)现在对于大量输入
textwrap.indent()
相比之前可提速大约 30%。 (由 Inada Naoki 在 gh-107369 [https://github.com/python/cpython/issues/107369] 中贡献。)现在
subprocess
模块会在更多场合下使用posix_spawn()
函数,包括在许多现代系统平台上当 close_fds 为True
(默认值) 的时候。 当在 FreeBSD 和 Solaris 上启动进程时这应该能提供显著的性能提升。 请参阅上面的 subprocess 小节了解详情。 (由 Jakub Kulik 在 gh-113117 [https://github.com/python/cpython/issues/113117] 中贡献。)
被移除的模块与 API
PEP 594: 从标准库中移除“死电池”
PEP 594 [https://peps.python.org/pep-0594/] 提议从标准库移除 19 个模块,它们因其古旧、过时或不安全的状态而被非正式地称呼为‘死电池’。 下列所有模块在 Python 3.11 中被弃用,现在已被移除:
aifc
- standard-aifc [https://pypi.org/project/standard-aifc/]: 使用 PyPI 上的重新分发版
aifc
库。
- standard-aifc [https://pypi.org/project/standard-aifc/]: 使用 PyPI 上的重新分发版
audioop
- audioop-lts [https://pypi.org/project/audioop-lts/]: 使用 PyPI 上的
audioop-lts
库。
- audioop-lts [https://pypi.org/project/audioop-lts/]: 使用 PyPI 上的
chunk
- standard-chunk [https://pypi.org/project/standard-chunk/]: 使用 PyPI 上的重新分发版
chunk
库。
- standard-chunk [https://pypi.org/project/standard-chunk/]: 使用 PyPI 上的重新分发版
cgi
和cgitb
对于
GET
和HEAD
请求cgi.FieldStorage
通常可以用urllib.parse.parse_qsl()
来替换,而对于POST
和PUT
请求则可以用email.message
模块或 multipart [https://pypi.org/project/multipart/] 库。cgi.parse()
可被替换为在想要的查询字符串上直接调用urllib.parse.parse_qs()
,除非输入为multipart/form-data
,它应当如下文针对cgi.parse_multipart()
所描述的那样被替换。cgi.parse_header()
可被email
包中的功能所替换,它实现了相同的 MIME RFC。 例如,使用email.message.EmailMessage
:
- from email.message import EmailMessage
- msg = EmailMessage()
- msg['content-type'] = 'application/json; charset="utf8"'
- main, params = msg.get_content_type(), msg['content-type'].params
cgi.parse_multipart()
可以用email
包中的功能来替换,它实现了相同的 MIME RFC,也可以用 multipart [https://pypi.org/project/multipart/] 库。 例如,email.message.EmailMessage
和email.message.Message
类。standard-cgi [https://pypi.org/project/standard-cgi/]: 和 standard-cgitb [https://pypi.org/project/standard-cgitb/]: 使用 PyPI 上的重新分发版
cgi
和cgitb
库。crypt
以及私有的_crypt
扩展。hashlib
模块在仅需对值执行哈希时是一个适当的替代物。 在其他情况下,可以使用 PyPI 上的一些第三方库:
bcrypt [https://pypi.org/project/bcrypt/]: 用于软件和服务器的现代密码哈希算法。
passlib [https://pypi.org/project/passlib/]: 支持超过 over 30 种方案的综合密码哈希算法框架。
argon2-cffi [https://pypi.org/project/argon2-cffi/]: 安全的 Argon2 密码哈希算法。
legacycrypt [https://pypi.org/project/legacycrypt/]: 针对 POSIX 加密库调用和相关功能的
ctypes
包装器。crypt_r [https://pypi.org/project/crypt_r/]:
crypt
模块的分叉,针对 crypt_r(3)) [https://manpages.debian.org/crypt_r(3)] 库调用和相关功能和包装器。standard-crypt [https://pypi.org/project/standard-crypt/] and deprecated-crypt-alternative [https://pypi.org/project/deprecated-crypt-alternative/]: Use the redistribution of
crypt
and reimplementation of_crypt
libraries from PyPI.
imghdr
: 应当使用 filetype [https://pypi.org/project/filetype/], puremagic [https://pypi.org/project/puremagic/] 或 python-magic [https://pypi.org/project/python-magic/] 等库作为替代。 例如,对于imghdr
所支持的所有文件格式puremagic.what()
函数可被用来替代imghdr.what()
函数。
- standard-imghdr [https://pypi.org/project/standard-imghdr/]: Use the redistribution of
imghdr
library from PyPI.
mailcap
: 改用mimetypes
模块。
- standard-mailcap [https://pypi.org/project/standard-mailcap/]: Use the redistribution of
mailcap
library from PyPI.
msilib
nis
nntplib
: 改用 PyPI 上的 pynntp [https://pypi.org/project/pynntp/] 库。
- standard-nntplib [https://pypi.org/project/standard-nntplib/]: Use the redistribution of
nntplib
library from PyPI.
ossaudiodev
: 对于音频回放,改用 PyPI 上的 pygame [https://pypi.org/project/pygame/] 库。pipes
: 改用subprocess
模块。 使用shlex.quote()
来替代未写入文档的pipes.quote
函数。
- standard-pipes [https://pypi.org/project/standard-pipes/]: Use the redistribution of
pipes
library from PyPI.
sndhdr
: 应当使用 filetype [https://pypi.org/project/filetype/], puremagic [https://pypi.org/project/puremagic/] 或 python-magic [https://pypi.org/project/python-magic/] 库作为替代。
- standard-sndhdr [https://pypi.org/project/standard-sndhdr/]: Use the redistribution of
sndhdr
library from PyPI.
spwd
: 改用 PyPI 上的 python-pam [https://pypi.org/project/python-pam/] 库。sunau
- standard-sunau [https://pypi.org/project/standard-sunau/]: Use the redistribution of
sunau
library from PyPI.
telnetlib
,改用 PyPI 上的 telnetlib3 [https://pypi.org/project/telnetlib3/] 或 Exscript [https://pypi.org/project/Exscript/] 库。
- standard-telnetlib [https://pypi.org/project/standard-telnetlib/]: Use the redistribution of
telnetlib
library from PyPI.
uu
: 改用 thebase64
模块,作为一款现代化的替代。
- standard-uu [https://pypi.org/project/standard-uu/]: Use the redistribution of
uu
library from PyPI.
xdrlib
- standard-xdrlib [https://pypi.org/project/standard-xdrlib/]: Use the redistribution of
xdrlib
library from PyPI.
(由 Victor Stinner 和 Zachary Ware 在 gh-104773 [https://github.com/python/cpython/issues/104773] 和 gh-104780 [https://github.com/python/cpython/issues/104780] 中贡献。)
2to3
- 移除 2to3 程序和
lib2to3
模块,此前已在 Python 3.11 中被弃用。 (由 Victor Stinner 在 gh-104780 [https://github.com/python/cpython/issues/104780] 中贡献。)
builtins
移除了对串联
classmethod
描述器的支持(在 gh-63272 [https://github.com/python/cpython/issues/63272] 中引入)。 它们不能再被用来包装其他描述器,如property
。 此特性的核心设计存在缺陷并导致了一些问题。 要“穿过”一个classmethod
,请考虑使用在 Python 3.10 中增加的__wrapped__
属性。 (由 Raymond Hettinger 在 gh-89519 [https://github.com/python/cpython/issues/89519] 中贡献。).)当在被挂起的帧上调用
frame.clear()
时将引发RuntimeError
。 (就如正在执行的帧一直以来的情况那样)。 (由 Irit Katriel 在 gh-79932 [https://github.com/python/cpython/issues/79932] 中贡献。)
configparser
- 移除了未写入文档的
LegacyInterpolation
类,它自 Python 3.2 起已在文档字符串中声明弃用,并且自 Python 3.11 起在运行时也已弃用。 (由 Hugo van Kemenade 在 gh-104886 [https://github.com/python/cpython/issues/104886] 中贡献。)
importlib.metadata
- 移除了已弃用的针对 EntryPoint 对象的下标 (
__getitem__()
) 访问。 (由 Jason R. Coombs 在 gh-113175 [https://github.com/python/cpython/issues/113175] 中贡献。)
locale
- 移除了
locale.resetlocale()
函数,它已在 Python 3.11 中被弃用。 请改用locale.setlocale(locale.LC_ALL, "")
。 (由 Victor Stinner 在 gh-104783 [https://github.com/python/cpython/issues/104783] 中贡献。)
opcode
将
opcode.ENABLE_SPECIALIZATION
移至opcode.ENABLESPECIALIZATION
。 这个字段在是 3.12 中增加的,它从未被写入文档,也无意开放给外部使用。 (由 Irit Katriel 在 gh-105481 [https://github.com/python/cpython/issues/105481] 中贡献。)移除了
opcode.is_pseudo()
,opcode.MIN_PSEUDO_OPCODE
和opcode.MAX_PSEUDO_OPCODE
,它们是在 Python 3.12 中增加了,但从未被写入文档也未通过dis
对外公开,并且不应当在外部被使用。 (由 Irit Katriel 在 gh-105481 [https://github.com/python/cpython/issues/105481] 中贡献。)
optparse
- This module is no longer considered soft deprecated. While
argparse
remains preferred for new projects that aren't using a third party command line argument processing library, there are aspects of the wayargparse
works that mean the lower leveloptparse
module may provide a better foundation for writing argument processing libraries, and for implementing command line applications which adhere more strictly thanargparse
does to various Unix command line processing conventions that originate in the behaviour of the Cgetopt()
function . (Contributed by Alyssa Coghlan and Serhiy Storchaka in gh-126180 [https://github.com/python/cpython/issues/126180].)
pathlib
- 移除了使用
Path
对象作为上下文管理器的能力。 此功能自 Python 3.9 起已被弃用并设为空操作。 (由 Barney Gale 在 gh-83863 [https://github.com/python/cpython/issues/83863] 中贡献。)
re
- 移除了未被写入文档、已被弃用且已不能工作的
re.template()
函数和re.TEMPLATE
/re.T
旗标。 (由 Serhiy Storchaka 和 Nikita Sobolev 在 gh-105687 [https://github.com/python/cpython/issues/105687] 中贡献。)
tkinter.tix
- 移除了
tkinter.tix
模块,它在 Python 3.6 中已被弃用。 该模块所包装的第三方库 Tix 已不再维护。 (由 Zachary Ware 在 gh-75552 [https://github.com/python/cpython/issues/75552] 中贡献。).)
turtle
- 移除了
RawTurtle.settiltangle()
方法,它自 Python 3.1 起已在文档中声明弃用并自 Python 3.11 起在运行时声明弃用。 (由 Hugo van Kemenade 在 gh-104876 [https://github.com/python/cpython/issues/104876] 中贡献。)