弃用¶
Python 3.15 中的待移除功能¶
-
The undocumented
ctypes.SetPointerType()function has been deprecated since Python 3.13.
-
The obsolete and rarely used
CGIHTTPRequestHandlerhas been deprecated since Python 3.13. No direct replacement exists. Anything is better than CGI to interface a web server with a request handler.The
--cgiflag to the python -m http.server command-line interface has been deprecated since Python 3.13.
importlib:__package__和__cached__将不再被设置或是被导入系统纳入考量 (gh-97879)。-
The
getdefaultlocale()function has been deprecated since Python 3.11. Its removal was originally planned for Python 3.13 (gh-90817), but has been postponed to Python 3.15. Usegetlocale(),setlocale(), andgetencoding()instead. (Contributed by Hugo van Kemenade in gh-111187.)
-
PurePath.is_reserved()has been deprecated since Python 3.13. Useos.path.isreserved()to detect reserved paths on Windows.
-
java_ver()has been deprecated since Python 3.13. This function is only useful for Jython support, has a confusing API, and is largely untested.
-
RLock()will take no arguments in Python 3.15. Passing any arguments has been deprecated since Python 3.14, as the Python version does not permit any arguments, but the C version allows any number of positional or keyword arguments, ignoring every argument.
-
The undocumented keyword argument syntax for creating
NamedTupleclasses (e.g.Point = NamedTuple("Point", x=int, y=int)) has been deprecated since Python 3.13. Use the class-based syntax or the functional syntax instead.The
typing.no_type_check_decorator()decorator function has been deprecated since Python 3.13. After eight years in thetypingmodule, it has yet to be supported by any major type checker.
wave:The
getmark(),setmark(), andgetmarkers()methods of theWave_readandWave_writeclasses have been deprecated since Python 3.13.
计划在 Python 3.16 中移除¶
-
Bitwise inversion on boolean types,
~Trueor~Falsehas been deprecated since Python 3.12, as it produces surprising and unintuitive results (-2and-1). Usenot xinstead for the logical negation of a Boolean. In the rare case that you need the bitwise inversion of the underlying integer, convert tointexplicitly (~int(x)).
-
The
'u'format code (wchar_t) has been deprecated in documentation since Python 3.3 and at runtime since Python 3.13. Use the'w'format code (Py_UCS4) for Unicode characters instead.
-
The
ExecErrorexception has been deprecated since Python 3.14. It has not been used by any function inshutilsince Python 3.4, and is now an alias ofRuntimeError.
-
The
Class.get_methodsmethod has been deprecated since Python 3.14.
sys:The
_enablelegacywindowsfsencoding()function has been deprecated since Python 3.13. Use thePYTHONLEGACYWINDOWSFSENCODINGenvironment variable instead.
-
The undocumented and unused
TarFile.tarfileattribute has been deprecated since Python 3.13.
计划在未来版本中移除¶
以下API将会被移除,尽管具体时间还未确定。
argparse: 嵌套参数分组和嵌套互斥分组的做法已被弃用。-
bool(NotImplemented)。生成器:
throw(type, exc, tb)和athrow(type, exc, tb)签名已被弃用:请改用throw(exc)和athrow(exc),即单参数签名。目前 Python 接受数字类字面值后面紧跟关键字的写法,例如
0in x,1or x,0if 1else 2。 它允许像[0x1for x in y]这样令人困惑且有歧义的表达式 (它可以被解读为[0x1 for x in y]或者[0x1f or x in y])。 如果数字类字面值后面紧跟关键字and,else,for,if,in,is和or中的一个将会引发语法警告。 在未来的版本中它将改为语法错误。 (gh-87999)对
__index__()和__int__()方法返回非 int 类型的支持:将要求这些方法必须返回int的子类的实例。对
__complex__()方法返回complex的子类的支持:将要求这些方法必须返回complex的实例。将
int()委托给__trunc__()方法。传入一个复数作为
complex()构造器中的 real 或 imag 参数的做法现在已被弃用;它应当仅作为单个位置参数被传入。 (由 Serhiy Storchaka 在 gh-109218 中贡献。).)
calendar:calendar.January和calendar.February常量已被弃用并由calendar.JANUARY和calendar.FEBRUARY替代。 (由 Prince Roshan 在 gh-103636 中贡献。)-
utcnow(): 使用datetime.datetime.now(tz=datetime.UTC)。utcfromtimestamp(): 使用datetime.datetime.fromtimestamp(timestamp, tz=datetime.UTC)。
gettext: 复数值必须是一个整数。-
load_module()方法:改用exec_module()。cache_from_source()debug_override 形参已被弃用:改用 optimization 形参。
-
EntryPoints元组接口。返回值中隐式的
None。
mailbox: 对 StringIO 输入和文本模式的使用已被弃用,改用 BytesIO 和二进制模式。os: 在多线程的进程中调用os.register_at_fork()。pydoc.ErrorDuringImport: 使用元组值作为 exc_info 形参的做法已被弃用,应使用异常实例。re: 现在对于正则表达式中的数字分组引用和分组名称将应用更严格的规则。 现在只接受 ASCII 数字序列作为数字引用。 字节串模式和替换字符串中的分组名称现在只能包含 ASCII 字母和数字以及下划线。 (由 Serhiy Storchaka 在 gh-91760 中贡献。)sre_compile,sre_constants和sre_parse模块。shutil:rmtree()的 onerror 形参在 Python 3.12 中已被弃用;请改用 onexc 形参。ssl选项和协议:ssl.SSLContext不带 protocol 参数的做法已被弃用。ssl.SSLContext:set_npn_protocols()和selected_npn_protocol()已被弃用:请改用 ALPN。ssl.OP_NO_SSL*选项ssl.OP_NO_TLS*选项ssl.PROTOCOL_SSLv3ssl.PROTOCOL_TLSssl.PROTOCOL_TLSv1ssl.PROTOCOL_TLSv1_1ssl.PROTOCOL_TLSv1_2ssl.TLSVersion.SSLv3ssl.TLSVersion.TLSv1ssl.TLSVersion.TLSv1_1
sysconfig.is_python_build()check_home 形参已被弃用并会被忽略。threading的方法:threading.Condition.notifyAll(): 使用notify_all()。threading.Event.isSet(): 使用is_set()。threading.Thread.isDaemon(),threading.Thread.setDaemon(): 使用threading.Thread.daemon属性。threading.Thread.getName(),threading.Thread.setName(): 使用threading.Thread.name属性。threading.currentThread(): 使用threading.current_thread()。threading.activeCount(): 使用threading.active_count()。
unittest.IsolatedAsyncioTestCase: 从测试用例返回不为None的值的做法已被弃用。urllib.parse函数已被弃用:改用urlparse()splitattr()splithost()splitnport()splitpasswd()splitport()splitquery()splittag()splittype()splituser()splitvalue()to_bytes()
urllib.request: 发起请求的URLopener和FancyURLopener方式已被弃用。 改用更新urlopen()函数和方法。wsgiref:SimpleHandler.stdout.write()不应执行部分写入。xml.etree.ElementTree: 对Element的真值测试已被弃用。 在未来的发布版中它将始终返回True。 建议改用显式的len(elem)或elem is not None测试。
C API 的弃用项¶
Python 3.15 中的待移除功能¶
捆绑的
libmpdecimal副本。The
PyImport_ImportModuleNoBlock(): UsePyImport_ImportModule()instead.PyWeakref_GetObject()andPyWeakref_GET_OBJECT(): UsePyWeakref_GetRef()instead.Py_UNICODEtype and thePy_UNICODE_WIDEmacro: Usewchar_tinstead.Python 初始化函数
PySys_ResetWarnOptions(): Clearsys.warnoptionsandwarnings.filtersinstead.Py_GetExecPrefix(): Getsys.exec_prefixinstead.Py_GetPath(): Getsys.pathinstead.Py_GetPrefix(): Getsys.prefixinstead.Py_GetProgramFullPath(): Getsys.executableinstead.Py_GetProgramName(): Getsys.executableinstead.Py_GetPythonHome(): GetPyConfig.homeor thePYTHONHOMEenvironment variable instead.
计划在未来版本中移除¶
以下 API 已被弃用,将被移除,但目前尚未确定移除日期。
Py_TPFLAGS_HAVE_FINALIZE: Unneeded since Python 3.8.PyErr_Fetch(): UsePyErr_GetRaisedException()instead.PyErr_NormalizeException(): UsePyErr_GetRaisedException()instead.PyErr_Restore(): UsePyErr_SetRaisedException()instead.PyModule_GetFilename(): UsePyModule_GetFilenameObject()instead.PyOS_AfterFork(): UsePyOS_AfterFork_Child()instead.PySlice_GetIndicesEx(): UsePySlice_Unpack()andPySlice_AdjustIndices()instead.PyUnicode_AsDecodedObject(): UsePyCodec_Decode()instead.PyUnicode_AsDecodedUnicode(): UsePyCodec_Decode()instead.PyUnicode_AsEncodedObject(): UsePyCodec_Encode()instead.PyUnicode_AsEncodedUnicode(): UsePyCodec_Encode()instead.PyUnicode_READY(): Unneeded since Python 3.12PyErr_Display(): UsePyErr_DisplayException()instead._PyErr_ChainExceptions(): Use_PyErr_ChainExceptions1()instead.PyBytesObject.ob_shash成员:改为调用PyObject_Hash()。PyDictObject.ma_version_tag成员。线程本地存储 (TLS) API:
PyThread_create_key(): UsePyThread_tss_alloc()instead.PyThread_delete_key(): UsePyThread_tss_free()instead.PyThread_set_key_value(): UsePyThread_tss_set()instead.PyThread_get_key_value(): UsePyThread_tss_get()instead.PyThread_delete_key_value(): UsePyThread_tss_delete()instead.PyThread_ReInitTLS(): Unneeded since Python 3.7.