Skip to content

Commit 06bf752

Browse files
committed
Switch to ruff, fix flagged issues
1 parent 3d0ed55 commit 06bf752

9 files changed

Lines changed: 150 additions & 115 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ on:
1010
- cron: '17 3 * * 0'
1111

1212
jobs:
13-
flake8:
14-
name: Flake8
13+
ruff:
14+
name: Ruff
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v4
18-
-
19-
uses: actions/setup-python@v5
20-
with:
21-
python-version: '3.8'
18+
- uses: actions/setup-python@v5
2219
- name: "Main Script"
2320
run: |
24-
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
25-
. ./prepare-and-run-flake8.sh ./pycparserext ./test
21+
pip install ruff
22+
ruff check
2623
2724
pytest:
2825
name: Pytest on Py${{ matrix.python-version }}

.gitlab-ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Python 3:
1111
reports:
1212
junit: test/pytest.xml
1313

14-
Flake8:
14+
Ruff:
1515
script:
16-
- curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
17-
- ". ./prepare-and-run-flake8.sh pycparserext test"
16+
- pipx install ruff
17+
- ruff check
1818
tags:
19-
- python3
19+
- docker-runner
2020
except:
2121
- tags

pycparserext/ext_c_generator.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
import pycparser.c_ast as c_ast
12
from pycparser.c_generator import CGenerator as CGeneratorBaseBuggy
3+
24
from pycparserext.ext_c_parser import FuncDeclExt, TypeDeclExt
3-
import pycparser.c_ast as c_ast
45

56

67
class CGeneratorBase(CGeneratorBaseBuggy):
78
# bug fix
89
def visit_UnaryOp(self, n):
910
operand = self._parenthesize_unless_simple(n.expr)
10-
if n.op == 'p++':
11-
return '%s++' % operand
12-
elif n.op == 'p--':
13-
return '%s--' % operand
14-
elif n.op == 'sizeof':
11+
if n.op == "p++":
12+
return "%s++" % operand
13+
elif n.op == "p--":
14+
return "%s--" % operand
15+
elif n.op == "sizeof":
1516
# Always parenthesize the argument of sizeof since it can be
1617
# a name.
17-
return 'sizeof(%s)' % self.visit(n.expr)
18+
return "sizeof(%s)" % self.visit(n.expr)
1819
else:
1920
# avoid merging of "- - x" or "__real__varname"
20-
return '%s %s' % (n.op, operand)
21+
return "%s %s" % (n.op, operand)
2122

2223

2324
class AsmAndAttributesMixin(object):
@@ -48,64 +49,64 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
4849
if modifiers is None:
4950
modifiers = []
5051
typ = type(n)
51-
#~ print(n, modifiers)
52+
# print(n, modifiers)
5253

5354
if typ in (c_ast.TypeDecl, TypeDeclExt):
54-
s = ''
55+
s = ""
5556
if n.quals:
56-
s += ' '.join(n.quals) + ' '
57+
s += " ".join(n.quals) + " "
5758
s += self.visit(n.type)
5859

59-
nstr = n.declname if n.declname and emit_declname else ''
60+
nstr = n.declname if n.declname and emit_declname else ""
6061
# Resolve modifiers.
6162
# Wrap in parens to distinguish pointer to array and pointer to
6263
# function syntax.
6364
#
6465
for i, modifier in enumerate(modifiers):
6566
if isinstance(modifier, c_ast.ArrayDecl):
6667
if i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl):
67-
nstr = '(' + nstr + ')'
68+
nstr = "(" + nstr + ")"
6869

6970
# BUG FIX: pycparser ignores quals
70-
dim_quals = (' '.join(modifier.dim_quals) + ' '
71-
if modifier.dim_quals else '')
71+
dim_quals = (" ".join(modifier.dim_quals) + " "
72+
if modifier.dim_quals else "")
7273

73-
nstr += '[' + dim_quals + self.visit(modifier.dim) + ']'
74+
nstr += "[" + dim_quals + self.visit(modifier.dim) + "]"
7475

7576
elif isinstance(modifier, c_ast.FuncDecl):
7677
if i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl):
77-
nstr = '(' + nstr + ')'
78-
nstr += '(' + self.visit(modifier.args) + ')'
78+
nstr = "(" + nstr + ")"
79+
nstr += "(" + self.visit(modifier.args) + ")"
7980

8081
elif isinstance(modifier, FuncDeclExt):
8182
if i != 0 and isinstance(modifiers[i - 1], c_ast.PtrDecl):
82-
nstr = '(' + nstr + ')'
83-
nstr += '(' + self.visit(modifier.args) + ')'
83+
nstr = "(" + nstr + ")"
84+
nstr += "(" + self.visit(modifier.args) + ")"
8485

8586
if modifier.asm is not None:
8687
nstr += " " + self.visit(modifier.asm)
8788

8889
if modifier.attributes.exprs:
8990
nstr += (
90-
' __attribute__(('
91+
" __attribute__(("
9192
+ self.visit(modifier.attributes)
92-
+ '))')
93+
+ "))")
9394

9495
elif isinstance(modifier, c_ast.PtrDecl):
9596
# BUG FIX: pycparser ignores quals
96-
quals = ' '.join(modifier.quals)
97+
quals = " ".join(modifier.quals)
9798
if quals:
98-
quals = quals + ' '
99-
nstr = '*' + quals + nstr
99+
quals = quals + " "
100+
nstr = "*" + quals + nstr
100101

101102
if hasattr(n, "asm") and n.asm:
102103
nstr += self.visit(n.asm)
103104

104105
if hasattr(n, "attributes") and n.attributes.exprs:
105-
nstr += ' __attribute__((' + self.visit(n.attributes) + '))'
106+
nstr += " __attribute__((" + self.visit(n.attributes) + "))"
106107

107108
if nstr:
108-
s += ' ' + nstr
109+
s += " " + nstr
109110
return s
110111

111112
elif typ == c_ast.Decl:
@@ -115,7 +116,7 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
115116
return self._generate_type(n.type, emit_declname=emit_declname)
116117

117118
elif typ == c_ast.IdentifierType:
118-
return ' '.join(n.names) + ' '
119+
return " ".join(n.names) + " "
119120

120121
elif typ in (c_ast.ArrayDecl, c_ast.PtrDecl, c_ast.FuncDecl, FuncDeclExt):
121122
return self._generate_type(
@@ -127,7 +128,7 @@ def _generate_type(self, n, modifiers=None, emit_declname=True):
127128
def _generate_decl(self, n):
128129
""" Generation from a Decl node.
129130
"""
130-
s = ''
131+
s = ""
131132

132133
def funcspec_to_str(i):
133134
if isinstance(i, c_ast.Node):
@@ -136,14 +137,14 @@ def funcspec_to_str(i):
136137
return i
137138

138139
if n.funcspec:
139-
s = ' '.join(funcspec_to_str(i) for i in n.funcspec) + ' '
140+
s = " ".join(funcspec_to_str(i) for i in n.funcspec) + " "
140141
if n.storage:
141-
s += ' '.join(n.storage) + ' '
142+
s += " ".join(n.storage) + " "
142143
s += self._generate_type(n.type)
143144
return s
144145

145146
def visit_AttributeSpecifier(self, n):
146-
return ' __attribute__((' + self.visit(n.exprlist) + '))'
147+
return " __attribute__((" + self.visit(n.exprlist) + "))"
147148

148149

149150
class GnuCGenerator(AsmAndAttributesMixin, CGeneratorBase):
@@ -154,10 +155,10 @@ def visit_TypeOfExpression(self, n):
154155
return "%s(%s)" % (n.typeof_keyword, self.visit(n.expr))
155156

156157
def visit_TypeList(self, n):
157-
return ', '.join(self.visit(ch) for ch in n.types)
158+
return ", ".join(self.visit(ch) for ch in n.types)
158159

159160
def visit_RangeExpression(self, n):
160-
return '%s ... %s' % (self.visit(n.first), self.visit(n.last))
161+
return "%s ... %s" % (self.visit(n.first), self.visit(n.last))
161162

162163

163164
class GNUCGenerator(GnuCGenerator):
@@ -169,13 +170,13 @@ def __init__(self):
169170

170171
class OpenCLCGenerator(AsmAndAttributesMixin, CGeneratorBase):
171172
def visit_FileAST(self, n):
172-
s = ''
173+
s = ""
173174
from pycparserext.ext_c_parser import PreprocessorLine
174175
for ext in n.ext:
175176
if isinstance(ext, (c_ast.FuncDef, PreprocessorLine)):
176177
s += self.visit(ext)
177178
else:
178-
s += self.visit(ext) + ';\n'
179+
s += self.visit(ext) + ";\n"
179180
return s
180181

181182
def visit_PreprocessorLine(self, n):

pycparserext/ext_c_lexer.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from pycparser.c_lexer import CLexer as CLexerBase
2+
3+
24
try:
35
from pycparser.ply.lex import TOKEN
46
except ImportError:
@@ -8,16 +10,16 @@
810
class GnuCLexer(CLexerBase):
911
# support '3i' for imaginary literal
1012
floating_constant = (
11-
'(((('
12-
+ CLexerBase.fractional_constant+')'
13-
+ CLexerBase.exponent_part+'?)|([0-9]+'
14-
+ CLexerBase.exponent_part+'))i?[FfLl]?)')
13+
"(((("
14+
+ CLexerBase.fractional_constant+")"
15+
+ CLexerBase.exponent_part+"?)|([0-9]+"
16+
+ CLexerBase.exponent_part+"))i?[FfLl]?)")
1517

1618
@TOKEN(floating_constant)
17-
def t_FLOAT_CONST(self, t):
19+
def t_FLOAT_CONST(self, t): # noqa: N802
1820
return t
1921

20-
t_pppragma_ignore = ' \t<>.-{}();+-*/$%@&^~!?:,0123456789='
22+
t_pppragma_ignore = " \t<>.-{}();+-*/$%@&^~!?:,0123456789="
2123

2224

2325
class GNUCLexer(GnuCLexer):
@@ -30,21 +32,21 @@ def __init__(self, *args, **kwargs):
3032

3133

3234
class OpenCLCLexer(CLexerBase):
33-
tokens = CLexerBase.tokens + ('LINECOMMENT',)
35+
tokens = CLexerBase.tokens + ("LINECOMMENT",)
3436
states = (
3537
# ('comment', 'exclusive'),
3638
# ('preproc', 'exclusive'),
37-
('ppline', 'exclusive'), # unused
38-
('pppragma', 'exclusive'), # unused
39+
("ppline", "exclusive"), # unused
40+
("pppragma", "exclusive"), # unused
3941
)
4042

41-
def t_LINECOMMENT(self, t):
42-
r'\/\/([^\n]+)\n'
43+
def t_LINECOMMENT(self, t): # noqa: N802
44+
r"\/\/([^\n]+)\n"
4345
t.lexer.lineno += t.value.count("\n")
4446

4547
# overrides pycparser, must have same name
46-
def t_PPHASH(self, t):
47-
r'[ \t]*\#([^\n]|\\\n)+[^\n\\]\n'
48+
def t_PPHASH(self, t): # noqa: N802
49+
r"[ \t]*\#([^\n]|\\\n)+[^\n\\]\n"
4850
t.lexer.lineno += t.value.count("\n")
4951
return t
5052

@@ -61,25 +63,25 @@ def add_lexer_keywords(cls, keywords):
6163

6264

6365
_COMMON_KEYWORDS = [
64-
'__attribute__', '__attribute',
65-
'__asm__', '__asm', 'asm']
66+
"__attribute__", "__attribute",
67+
"__asm__", "__asm", "asm"]
6668

6769
_GNU_KEYWORDS = [
68-
'__typeof__', 'typeof', '__typeof',
69-
'__real__', '__imag__',
70-
'__builtin_types_compatible_p',
71-
'__const',
72-
'__restrict__', '__restrict',
73-
'__inline__', '__inline',
74-
'__extension__',
75-
'__volatile', '__volatile__']
70+
"__typeof__", "typeof", "__typeof",
71+
"__real__", "__imag__",
72+
"__builtin_types_compatible_p",
73+
"__const",
74+
"__restrict__", "__restrict",
75+
"__inline__", "__inline",
76+
"__extension__",
77+
"__volatile", "__volatile__"]
7678

7779
add_lexer_keywords(GnuCLexer, _COMMON_KEYWORDS + _GNU_KEYWORDS)
7880

7981
# These will be added as unadorned keywords and keywords with '__' prepended
8082
_CL_BASE_KEYWORDS = [
81-
'kernel', 'constant', 'global', 'local', 'private',
82-
'read_only', 'write_only', 'read_write']
83+
"kernel", "constant", "global", "local", "private",
84+
"read_only", "write_only", "read_write"]
8385

8486
_CL_KEYWORDS = _COMMON_KEYWORDS
8587
_CL_KEYWORDS += _CL_BASE_KEYWORDS + ["__"+kw for kw in _CL_BASE_KEYWORDS]

0 commit comments

Comments
 (0)