Skip to content

Commit a798607

Browse files
committed
Fix issues with keyword processing for os.open
1 parent e93e25f commit a798607

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Updated
11+
12+
- Update unit tests for `os.open()`
13+
14+
### Fixed
15+
16+
- Fix issue when processing keyword arguments for `os.open()`
17+
1018
## [1.3.0] - 2021-07-26
1119

1220
### Added

pylint_secure_coding_standard.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ def _is_os_open_allowed_mode(node, allowed_modes):
104104
mode = node.args[2].value
105105
elif node.keywords:
106106
for keyword in node.keywords:
107-
if keyword.arg == 'flags':
107+
if keyword.arg == 'flags' and isinstance(keyword.value, (astroid.Attribute, astroid.BinOp)):
108108
flags = keyword.value # pylint: disable=unused-variable # noqa: F841
109-
if keyword.arg == 'mode':
109+
if keyword.arg == 'mode' and isinstance(keyword.value, astroid.Const):
110110
mode = keyword.value.value
111111
break
112112
if mode is not None:

tests/os_open_test.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,78 @@ def test_os_open_ok(self, arg):
3333
int(0) #@
3434
foo() #@
3535
os.open("file.txt") #@
36+
os.open("file.txt", flags, mode) #@
3637
os.open("file.txt", os.O_RDONLY) #@
38+
os.open("file.txt", os.O_RDONLY, mode) #@
3739
os.open("file.txt", os.O_RDONLY, 0o644) #@
3840
os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW) #@
41+
os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode) #@
3942
os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o644) #@
43+
os.open("file.txt", os.O_RDONLY, mode=mode) #@
44+
os.open("file.txt", os.O_RDONLY, mode=0o644) #@
45+
os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode=mode) #@
46+
os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode=0o644) #@
4047
bla.open("file.txt") #@
4148
bla.open("file.txt", os.O_RDONLY) #@
49+
bla.open("file.txt", flags=os.O_RDONLY) #@
50+
bla.open("file.txt", os.O_RDONLY, mode) #@
4251
bla.open("file.txt", os.O_RDONLY, 0o644) #@
43-
bla.open("file.txt", os.O_RDONLY, 0o755) #@
52+
bla.open("file.txt", os.O_RDONLY, 0o777) #@
4453
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW) #@
54+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode) #@
4555
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o644) #@
46-
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o755) #@
56+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o777) #@
57+
bla.open("file.txt", os.O_RDONLY, mode=mode) #@
58+
bla.open("file.txt", os.O_RDONLY, mode=0o644) #@
59+
bla.open("file.txt", os.O_RDONLY, mode=0o777) #@
60+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW) #@
61+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode=mode) #@
62+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode=0o644) #@
63+
bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode=0o777) #@
4764
with os.open("file.txt") as fd: fd.read() #@
65+
with os.open("file.txt", flags, mode) as fd: fd.read() #@
4866
with os.open("file.txt", os.O_RDONLY) as fd: fd.read() #@
67+
with os.open("file.txt", os.O_RDONLY, mode) as fd: fd.read() #@
4968
with os.open("file.txt", os.O_RDONLY, 0o644) as fd: fd.read() #@
5069
with os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW) as fd: fd.read() #@
70+
with os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode) as fd: fd.read() #@
5171
with os.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o644) as fd: fd.read() #@
72+
with os.open("file.txt", flags=flags, mode=mode) as fd: fd.read() #@
73+
with os.open("file.txt", flags=os.O_RDONLY, mode=mode) as fd: fd.read() #@
74+
with os.open("file.txt", flags=os.O_RDONLY, mode=0o644) as fd: fd.read() #@
75+
with os.open("file.txt", flags=os.O_RDONLY | os.O_NOFOLLOW, mode=mode) as fd: fd.read() #@
76+
with os.open("file.txt", flags=os.O_RDONLY | os.O_NOFOLLOW, mode=0o644) as fd: fd.read() #@
5277
with bla.open("file.txt") as fd: fd.read() #@
5378
with bla.open("file.txt", os.O_RDONLY) as fd: fd.read() #@
79+
with bla.open("file.txt", os.O_RDONLY, mode) as fd: fd.read() #@
5480
with bla.open("file.txt", os.O_RDONLY, 0o644) as fd: fd.read() #@
55-
with bla.open("file.txt", os.O_RDONLY, 0o755) as fd: fd.read() #@
81+
with bla.open("file.txt", os.O_RDONLY, 0o777) as fd: fd.read() #@
5682
with bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW) as fd: fd.read() #@
83+
with bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, mode) as fd: fd.read() #@
5784
with bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o644) as fd: fd.read() #@
58-
with bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o755) as fd: fd.read() #@
85+
with bla.open("file.txt", os.O_RDONLY | os.O_NOFOLLOW, 0o777) as fd: fd.read() #@
86+
with bla.open("file.txt", flags=os.O_RDONLY) as fd: fd.read() #@
87+
with bla.open("file.txt", flags=os.O_RDONLY, mode=mode) as fd: fd.read() #@
88+
with bla.open("file.txt", flags=os.O_RDONLY, mode=0o644) as fd: fd.read() #@
89+
with bla.open("file.txt", flags=os.O_RDONLY, mode=0o777) as fd: fd.read() #@
90+
with bla.open("file.txt", flags=os.O_RDONLY | os.O_NOFOLLOW, mode=mode) as fd: fd.read() #@
91+
with bla.open("file.txt", flags=os.O_RDONLY | os.O_NOFOLLOW, mode=0o644) as fd: fd.read() #@
92+
with bla.open("file.txt", flags=os.O_RDONLY | os.O_NOFOLLOW, mode=0o777) as fd: fd.read() #@
5993
"""
6094
)
6195

6296
self.checker.set_os_open_mode('True')
6397
assert self.checker._prefer_os_open
6498

65-
call_nodes = nodes[:14]
66-
with_nodes = nodes[14:]
99+
call_nodes = []
100+
with_nodes = []
101+
102+
# Find index of first line starting with 'with'
103+
for node in nodes:
104+
if isinstance(node, astroid.With):
105+
with_nodes.append(node)
106+
else:
107+
call_nodes.append(node)
67108

68109
with self.assertNoMessages():
69110
for idx, node in enumerate(call_nodes):

0 commit comments

Comments
 (0)