typing

unittest

  • 移除了下列 unittest 函数,它们在 Python 3.11 中已被弃用:

    • unittest.findTestCases()

    • unittest.makeSuite()

    • unittest.getTestCaseNames()

请改用 TestLoader 方法:

(由 Hugo van Kemenade 在 gh-104835 [https://github.com/python/cpython/issues/104835] 中贡献。)

urllib

webbrowser

新的弃用

计划在 Python 3.14 中移除

  • argparse: argparse.BooleanOptionalAction 的 type, choices 和 metavar 形参已被弃用并将在 3.14 中移除。 (由 Nikita Sobolev 在 gh-92248 [https://github.com/python/cpython/issues/92248] 中贡献。)

  • ast: 以下特性自 Python 3.8 起已在文档中声明弃用,现在当运行时如果它们被访问或使用时将发出 DeprecationWarning,并将在 Python 3.14 中移除:

    • ast.Num

    • ast.Str

    • ast.Bytes

    • ast.NameConstant

    • ast.Ellipsis

请改用 ast.Constant。 (由 Serhiy Storchaka 在 gh-90953 [https://github.com/python/cpython/issues/90953] 中贡献。)

使用 importlib.resources.abc 类代替:

(由 Jason R. Coombs 和 Hugo van Kemenade 在 gh-93963 [https://github.com/python/cpython/issues/93963] 中贡献。)

Python 3.15 中的待移除功能

计划在 Python 3.16 中移除

  • 导入系统:

    • 当设置 __spec__.loader 失败时在模块上设置 __loader__ 的做法已被弃用。 在 Python 3.16 中,__loader__ 将不会再被设置或是被导入系统或标准库纳入考虑。
  • array:

    • 'u' 格式代码 (wchar_t) 自 Python 3.3 起已在文档中弃用并自 Python 3.13 起在运行时弃用。 对于 Unicode 字符请改用 'w' 格式代码 (Py_UCS4)。
  • asyncio:

  • builtins:

    • 对布尔类型 ~True~False 执行按位取反的操作自 Python 3.12 起已被弃用,因为它会产生奇怪和不直观的结果 (-2 and -1)。 请改用 not x 来对布尔值执行逻辑否操作。 对于需要对下层整数执行按位取反操作的少数场合,请显式地将其转换为 int (~int(x))。
  • shutil:

    • ExecError 异常自 Python 3.14 起已被弃用。 它自 Python 3.4 起就未被 shutil 中的任何函数所使用,现在是 RuntimeError 的一个别名。
  • symtable:

  • sys:

  • tarfile:

    • 未写入文档也未被使用的 TarFile.tarfile 属性自 Python 3.13 起被弃用。

计划在未来版本中移除

以下API将会被移除,尽管具体时间还未确定。

CPython 字节码的变化

C API 的变化

新的特性

(由 Irit Katriel 在 gh-111997 [https://github.com/python/cpython/issues/111997] 中贡献。)

(由 Victor Stinner 和 Petr Viktorin 在 gh-110850 [https://github.com/python/cpython/issues/110850] 中贡献。)

(由 Mark Shannon 和 Tian Gao 在 gh-74929 [https://github.com/python/cpython/issues/74929] 中贡献。)

这些新函数将返回 -1 表示错误而返回标准的 1 表示真值以及 0 表示假值。

(由 Serhiy Storchaka 在 gh-108511 [https://github.com/python/cpython/issues/108511] 中贡献。)

被改变的 C API

请参阅 PEP 737 [https://peps.python.org/pep-0737/] 了解详情。 (由 Victor Stinner 在 gh-111696 [https://github.com/python/cpython/issues/111696] 中贡献。)

受限 C API 的改变

(由 Victor Stinner 在 gh-85283 [https://github.com/python/cpython/issues/85283], gh-85283 [https://github.com/python/cpython/issues/85283] 和 gh-116936 [https://github.com/python/cpython/issues/116936] 中贡献。)

被移除的 C API

  1. Py_buffer view;
  2. if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) < 0) { return NULL;
  3. }
  4. // Use `view.buf` and `view.len` to read from the buffer.
  5. // You may need to cast buf as `(const char*)view.buf`.
  6. PyBuffer_Release(&view);
  1. Py_buffer view;
  2. if (PyObject_GetBuffer(obj, &view, PyBUF_WRITABLE) < 0) { return NULL;
  3. }
  4. // Use `view.buf` and `view.len` to write to the buffer.
  5. PyBuffer_Release(&view);

(由 Inada Naoki 在 gh-85275 [https://github.com/python/cpython/issues/85275] 中贡献。)

  • 删除了在 Python 3.9 中弃用的各种函数:

警告

In PyObject_Call(), positional arguments must be a tuple and must not be NULL, and keyword arguments must be a dict or NULL, whereas the removed functions checked argument types and accepted NULL positional and keyword arguments. To replace PyEval_CallObjectWithKeywords(func, NULL, kwargs) with PyObject_Call(), pass an empty tuple as positional arguments using PyTuple_New(0).

(Contributed by Victor Stinner in gh-105107 [https://github.com/python/cpython/issues/105107].)

  • 移除了下列用于配置 Python 初始化的旧函数,它们在 Python 3.11 中已被弃用:

Use the new PyConfig API of the Python Initialization Configuration instead ( PEP 587 [https://peps.python.org/pep-0587/]), added to Python 3.8. (Contributed by Victor Stinner in gh-105145 [https://github.com/python/cpython/issues/105145].)

  • Remove PyEval_AcquireLock() and PyEval_ReleaseLock() functions, deprecated in Python 3.2. They didn't update the current thread state. They can be replaced with:

(Contributed by Victor Stinner in gh-105182 [https://github.com/python/cpython/issues/105182].)

已弃用的 C API

(由 Victor Stinner 在 gh-105145 [https://github.com/python/cpython/issues/105145] 中贡献。)

计划在 Python 3.14 中移除

Py_InitializeFromConfig() API 应与 PyConfig 一起使用。

Py_InitializeFromConfig() API 应与 PyConfig 一起使用。

Python 3.15 中的待移除功能

计划在未来版本中移除

以下 API 已被弃用,将被移除,但目前尚未确定移除日期。

构建变化

Porting to Python 3.13

本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.

Python API 的变化

与模块的其他“私有”属性一样,任何提供自定义 _thread 模块的库或应用都必须提供 ismain_interpreter()。(gh-112826 [https://github.com/python/cpython/issues/112826]。)

C API 的变化

  • Python.h no longer includes the standard header. It was included for the finite() function which is now provided by the header. It should now be included explicitly if needed. Remove also the HAVE_IEEEFP_H macro. (Contributed by Victor Stinner in gh-108765 [https://github.com/python/cpython/issues/108765].)

  • Python.h no longer includes these standard header files: , and . If needed, they should now be included explicitly. For example, provides the clock() and gmtime() functions, provides the select() function, and provides the futimes(), gettimeofday() and setitimer() functions. (Contributed by Victor Stinner in gh-108765 [https://github.com/python/cpython/issues/108765].)

  • On Windows, Python.h no longer includes the standard header file. If needed, it should now be included explicitly. For example, it provides offsetof() function, and size_t and ptrdiff_t types. Including explicitly was already needed by all other platforms, the HAVE_STDDEF_H macro is only defined on Windows. (Contributed by Victor Stinner in gh-108765 [https://github.com/python/cpython/issues/108765].)

  • If the Py_LIMITED_API macro is defined, Py_BUILD_CORE, Py_BUILD_CORE_BUILTIN and Py_BUILD_CORE_MODULE macros are now undefined by . (Contributed by Victor Stinner in gh-85283 [https://github.com/python/cpython/issues/85283].)

  • The old trashcan macros Py_TRASHCAN_SAFE_BEGIN and Py_TRASHCAN_SAFE_END were removed. They should be replaced by the new macros Py_TRASHCAN_BEGIN and Py_TRASHCAN_END.

A tp_dealloc function that has the old macros, such as:

  1. static void
  2. mytype_dealloc(mytype *p)
  3. {
  4. PyObject_GC_UnTrack(p);
  5. Py_TRASHCAN_SAFE_BEGIN(p);
  6. ...
  7. Py_TRASHCAN_SAFE_END
  8. }

应当按照以下方式迁移到新版宏:

  1. static void
  2. mytype_dealloc(mytype *p)
  3. {
  4. PyObject_GC_UnTrack(p);
  5. Py_TRASHCAN_BEGIN(p, mytype_dealloc)
  6. ...
  7. Py_TRASHCAN_END
  8. }

Note that Py_TRASHCAN_BEGIN has a second argument which should be the deallocation function it is in. The new macros were added in Python 3.8 and the old macros were deprecated in Python 3.11. (Contributed by Irit Katriel in gh-105111 [https://github.com/python/cpython/issues/105111].)

The pythoncapi-compat project [https://github.com/python/pythoncapi-compat/] can be used to get most of these new functions on Python 3.12 and older.

回归测试的变化

Notable changes in 3.13.1

sys

  • The previously undocumented special function sys.getobjects(), which only exists in specialized builds of Python, may now return objects from other interpreters than the one it's called in.