Skip to content

Commit af86ebc

Browse files
committed
make ast module usage compatible to python 3.14
Python 3.8 deprecated some AST nodes, which got removed in Python 3.14. See https://docs.python.org/3.14/whatsnew/3.14.html#id9
1 parent 322424e commit af86ebc

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

cstruct/c_expr.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import ast
2626
import inspect
2727
import operator
28+
import sys
2829
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Type, Union
2930

3031
from .base import DEFINES, STRUCTS
@@ -160,18 +161,11 @@ def get_cstruct_context() -> Optional["AbstractCStruct"]:
160161
return None
161162

162163

163-
try:
164-
Constant = ast.Constant
165-
except AttributeError: # python < 3.8
166-
Constant = ast.NameConstant
167-
168164
OPS: Dict[Type[ast.AST], Callable[[Any], Any]] = {
169165
ast.Expr: lambda node: eval_node(node.value),
170-
ast.Num: lambda node: node.n,
171166
ast.Name: eval_get,
172167
ast.Call: eval_call,
173-
Constant: lambda node: node.value,
174-
ast.Str: lambda node: node.s, # python < 3.8
168+
ast.Constant: lambda node: node.value,
175169
# and/or
176170
ast.BoolOp: lambda node: OPS[type(node.op)](node), # and/or operator
177171
ast.And: lambda node: all(eval_node(x) for x in node.values), # && operator
@@ -203,3 +197,7 @@ def get_cstruct_context() -> Optional["AbstractCStruct"]:
203197
ast.GtE: operator.ge,
204198
ast.LtE: operator.le,
205199
}
200+
201+
if sys.version_info.major == 3 and sys.version_info.minor < 8:
202+
OPS[ast.Num] = lambda node: node.n
203+
OPS[ast.Str] = lambda node: node.s

0 commit comments

Comments
 (0)