Repository navigation
Closed as not planned
Description
我们在开发过程中难免会有一些版本兼容代码,但这些代码应当在旧版本退场时进行清理,这里记录一些我个人阅读代码、开发过程中所了解到的一些版本兼容代码,欢迎大家补充
已退场版本
Python 3.7
- AST 动转静 origin_info 单测相关适配逻辑 Fix test_origin_info to be compatible with PY3.8, because ast module is different in PY3.8 #27201([Dy2St] pir dy2st unittest verification - Part 15 #59569 已移除)
Python 3.8
- copysign 宏,3.9 及以上版本可以安全删除 [Fix Compile Error] Fix copysign compile error on Windows #50387 ([3.8] Remove
undef copysignmacros #72856 已清理) - eval_frame 中的 3.8 从 CPython 中 copy 出来的 internal 代码 ([SOT] clean code in eval frame and fix dy2st uts #57662 已清理)
Paddle/paddle/fluid/pybind/eval_frame.h
Line 26 in 9985598
#if PY_VERSION_HEX >= 0x03080000 && PY_VERSION_HEX < 0x3090000 - eval_frame 3.8 相关适配逻辑([SOT][3.8] Cleanup
eval_framepython3.9- code #72868 已清理) - SOT 3.9 退场的字节码(🗓️ Python 各版本字节码差异表格 PaddleSOT#118)([SOT][3.8] Cleanup Python 3.8-specific bytecode implementations #72855 已清理)
- 第三方库 astor,AST 动转静只使用了 unparse 的功能,但这可以使用 Python 3.9 自带的更稳定的
ast.unparse取代,Retireastor, asast.unparseexists, and migrate it into API-compatible shims ofastfor older versions of python berkerpeksag/astor#204 也有相关讨论([3.8] Remove unused dependencyastor#72870 已清理) - [Typing] tensor.prototype.pyi 添加额外的 import 并对 Literal 进行转换 #65377 因为 3.8
Literal反编译回字符串时出 bug 了,因此加了调整的逻辑,3.9 可以安全清理([Typing][3.8] RemoveLiteralconversion in stubgen for python 3.8 #72873 已清理) - 动转静单测
check_approval.py里为 3.8 加的 unparse 逻辑 [Infra][Dy2St] Remove manual unparse logic for Python 3.8 in check approval script #72560Paddle/test/dygraph_to_static/check_approval.py
Lines 55 to 82 in 7d0dee3
def ast_to_source_code(node: ast.AST): if sys.version_info >= (3, 9): ast.fix_missing_locations(node) return ast.unparse(node) if isinstance(node, ast.Name): return node.id elif isinstance(node, ast.Attribute): return f'{ast_to_source_code(node.value)}.{node.attr}' elif isinstance(node, ast.Tuple): return f'({", ".join(ast_to_source_code(elt) for elt in node.elts)})' elif isinstance(node, ast.List): return f'[{", ".join(ast_to_source_code(elt) for elt in node.elts)}]' elif isinstance(node, ast.Set): return f'{{{", ".join(ast_to_source_code(elt) for elt in node.elts)}}}' elif isinstance(node, ast.Dict): return f'{{{", ".join(f"{ast_to_source_code(k)}: {ast_to_source_code(v)}" for k, v in zip(node.keys, node.values))}}}' return f'{ast_to_source_code(node.value)}[{ast_to_source_code(node.slice)}]' elif isinstance(node, ast.Constant): return repr(node.value) elif isinstance(node, ast.Call): unparsed_args = [ast_to_source_code(arg) for arg in node.args] unparsed_keywords = [ f'{kw.arg}={ast_to_source_code(kw.value)}' for kw in node.keywords ] return f'{ast_to_source_code(node.func)}({", ".join(unparsed_args + unparsed_keywords)})' else: raise NotImplementedError(f'Unsupported node type: {type(node)}') - 文档《Python 类型提示标注规范》中 Python 3.8 相关内容([Typing] Update minimum supported version to Python 3.9 docs#7307 已更新)
- Ruff B005 引入并修复相关 bug(因为 Python 3.9
removesuffix和removeprefix已经 ready),见 [CodeStyle][B005] Using.strip()with multi-character strings is misleading the reader #52103 (comment) ([CodeStyle][Ruff] Introduce ruleB005and fix discovered bugs #72896 已清理) - Python 3.9 可以使用 dict union1,因此可以用 ast-grep 搜
{**$A, **$B}搜索并替换下,SOT 里的(后者先不要修改了,union_free_vars也没有存在的必要了,相关地方直接用A | B | C即可union_free_vars语义会清晰些,而且在 C++ guard 里并不会存在,在退掉 py guard 时自然就清理掉了)([CodeStyle][3.8] Replace{**A, **B}withA | B(PEP 584) #72891 已修改)
Python 3.9
- SOT
co_lnotab生成逻辑,3.10 及之后会使用co_linetable([CodeStyle] Clean up Python 3.9 compatibility code #79290 已清理)def calc_lnotab(starts_line: int, code_length: int):
Python 3.10
- SOT
co_linetable3.10 生成逻辑def calc_linetable_py310(starts_line: int, code_length: int): - SOT 中各种 3.11 与之前的适配逻辑,主要包含字节码模拟执行和字节码生成逻辑,字节码可参考 🗓️ Python 各版本字节码差异表格 #69134
-
eval_frame中各种 3.11 与之前的适配逻辑 - 3.10- 专有的异常处理逻辑
Python 3.11
- SOT
PRECALL字节码相关逻辑,3.12 中该字节码已经被移除 - SOT 其余字节码(3.12 也删了不少字节码),可参考 🗓️ Python 各版本字节码差异表格 #69134 清理
Python 3.12
- SOT
NULL fn布局的处理,Python 3.13 中全部使用fn NULL布局,包括NullVariable的__call__、gen_load_null_variable等操作应该也不具备实际意义,均可清理
Python 3.13
- PEP 563
from __future__ import annotations2,Python 3.14 中已经被 PEP 6493 所取代 -
test/sot/test_26_t_string.py奇怪的写法可以修改下了,用正常的写法即可
Footnotes
Reacted by gouzil and Ryan
Metadata
Metadata
Assignees
Labels
No labels
Activity