Skip to content

Commit 7499130

Browse files
committed
Merge pull request #3278 from wisp3rwind/fix-ast-py38
functemplate: Adapt ast syntax to PEP570 changes on python3.8
2 parents 0ad084a + ade1df5 commit 7499130

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

beets/util/functemplate.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,30 @@ def compile_func(arg_names, statements, name='_the_func', debug=False):
118118
bytecode of the compiled function.
119119
"""
120120
if six.PY2:
121-
func_def = ast.FunctionDef(
122-
name=name.encode('utf-8'),
123-
args=ast.arguments(
124-
args=[ast.Name(n, ast.Param()) for n in arg_names],
125-
vararg=None,
126-
kwarg=None,
127-
defaults=[ex_literal(None) for _ in arg_names],
128-
),
129-
body=statements,
130-
decorator_list=[],
121+
name = name.encode('utf-8')
122+
args = ast.arguments(
123+
args=[ast.Name(n, ast.Param()) for n in arg_names],
124+
vararg=None,
125+
kwarg=None,
126+
defaults=[ex_literal(None) for _ in arg_names],
131127
)
132128
else:
133-
func_def = ast.FunctionDef(
134-
name=name,
135-
args=ast.arguments(
136-
args=[ast.arg(arg=n, annotation=None) for n in arg_names],
137-
kwonlyargs=[],
138-
kw_defaults=[],
139-
defaults=[ex_literal(None) for _ in arg_names],
140-
),
141-
body=statements,
142-
decorator_list=[],
143-
)
129+
args_fields = {
130+
'args': [ast.arg(arg=n, annotation=None) for n in arg_names],
131+
'kwonlyargs': [],
132+
'kw_defaults': [],
133+
'defaults': [ex_literal(None) for _ in arg_names],
134+
}
135+
if 'posonlyargs' in ast.arguments._fields: # Added in Python 3.8.
136+
args_fields['posonlyargs'] = []
137+
args = ast.arguments(**args_fields)
138+
139+
func_def = ast.FunctionDef(
140+
name=name,
141+
args=args,
142+
body=statements,
143+
decorator_list=[],
144+
)
144145

145146
# The ast.Module signature changed in 3.8 to accept a list of types to
146147
# ignore.

0 commit comments

Comments
 (0)