ANTLR 4 版本发布全流程指南:从 git 分支管理到多语言运行时部署
2026/9/20 21:46:09
在真实项目中,Debug 代码通常包括:
print()logging.debug()logging.info()logger.debug()debug()、pprint())if DEBUG:块👉手动删除不现实,正则又极易误伤
👉AST 是唯一靠谱、可维护的方案
本文教你如何用Python AST 自动、安全地移除 Debug 代码。
错误示例:
# 误删print=my_printprint("hello")# 不该删text="print(x)"# 字符串正则不知道「语义」,而 AST 知道。
| 类型 | 示例 |
|---|---|
print(x) | |
| logging.debug | logging.debug(x) |
| logging.info | logging.info(x) |
| logger.debug | logger.debug(x) |
| if DEBUG | if DEBUG: ... |
关键工具:
👉ast.NodeTransformer
importastimportastor DEBUG_FUNC_NAMES={"print","pprint","debug",}LOGGING_METHODS={"debug","info",}classRemoveDebugTransformer(ast.NodeTransformer):defvisit_Expr(self,node):""" 处理: - print(...) - logging.debug(...) - logger.debug(...) """call=node.valueifnotisinstance(call,ast.Call):returnnode func=call.func# print(...)ifisinstance(func,ast.Name):iffunc.idinDEBUG_FUNC_NAMES:returnNone# logging.debug(...) / logger.debug(...)ifisinstance(func,ast.Attribute):iffunc.attrinLOGGING_METHODS:returnNonereturnnodedefvisit_If(self,node):""" 处理: if DEBUG: ... """# if DEBUG:ifisinstance(node.test,ast.Name)andnode.test.id=="DEBUG":returnNonereturnself.generic_visit(node)defremove_debug_code(code:str)->str:tree=ast.parse(code)transformer=RemoveDebugTransformer()tree=transformer.visit(tree)ast.fix_missing_locations(tree)returnastor.to_source(tree)importlogging DEBUG=Trueprint("hello")logging.debug("debug log")logging.info("info log")logger.debug("logger debug")x=10ifDEBUG:print("only debug")print("done")code=""" import logging DEBUG = True def foo(x): print("foo x =", x) logging.debug("debug foo") logging.info("info foo") if DEBUG: print("only in debug") return x * 2 print("program start") result = foo(10) print("result =", result) """new_code=remove_debug_code(code)print(new_code)importlogging x=10print("done")✅ Debug 代码全部移除
✅ 正常业务代码保留
✅ 不影响 import / 变量 / 逻辑
ifos.getenv("ENV")=="prod":code=remove_debug_code(code)只需修改:
LOGGING_METHODS={"debug","info"}defvisit_Assert(self,node):returnNonefrompathlibimportPathforfileinPath("src").rglob("*.py"):code=file.read_text(encoding="utf-8")new_code=remove_debug_code(code)file.write_text(new_code,encoding="utf-8")| 方案 | 安全性 | 可维护 | 可扩展 |
|---|---|---|---|
| 正则 | ❌ | ❌ | ❌ |
| 手动删 | ❌ | ❌ | ❌ |
| AST | ✅ | ✅ | ✅ |
AST 的优势是:
👉按语义删代码,而不是按字符串