Skip to content

Commit e1f79b6

Browse files
author
gdhameeja
committed
Check invalid operations for -k
`KeywordMapping` returns a bool on lookup which when passed to eval fail on certain operations such as index access and attribute access. Check has been added to raise a UsageError if such operations are passed to -k
1 parent bd7e332 commit e1f79b6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/_pytest/mark/legacy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def matchkeyword(colitem, keywordexpr):
9292
any item, as well as names directly assigned to test functions.
9393
"""
9494
mapping = KeywordMapping.from_item(colitem)
95+
_check_invalid_operations(keywordexpr)
9596
if " " not in keywordexpr:
9697
# special case to allow for simple "-k pass" and "-k 1.3"
9798
return mapping[keywordexpr]
@@ -108,3 +109,13 @@ def matchkeyword(colitem, keywordexpr):
108109
return eval(keywordexpr, {}, mapping)
109110
except SyntaxError:
110111
raise UsageError("Wrong expression passed to '-k': {}".format(keywordexpr))
112+
except TypeError:
113+
raise UsageError("Attribute access and indexing is not allowed in '-k'")
114+
115+
116+
def _check_invalid_operations(keywordexpr):
117+
"""`KeywordMapping` returns boolean on lookups, hence certain
118+
operations fail in eval, like index access, attribute access"""
119+
msg = "Attribute access and indexing is not allowed in '-k'"
120+
if "." in keywordexpr or '[' in keywordexpr:
121+
raise UsageError(msg)

0 commit comments

Comments
 (0)