mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
Python: Add test for ClassValue.isSequence() and isMapping()
For Python 3.6
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
| mapping | builtin-class collections.defaultdict |
|
||||
| mapping | builtin-class dict |
|
||||
| neither sequence nor mapping | builtin-class set |
|
||||
| sequence | builtin-class bytes |
|
||||
| sequence | builtin-class collections.OrderedDict |
|
||||
| sequence | builtin-class list |
|
||||
| sequence | builtin-class str |
|
||||
| sequence | builtin-class tuple |
|
||||
| sequence | class OrderedDict |
|
||||
@@ -0,0 +1,20 @@
|
||||
import python
|
||||
|
||||
from ClassValue cls, string res
|
||||
where
|
||||
exists(CallNode call |
|
||||
call.getFunction().(NameNode).getId() = "test" and
|
||||
call.getAnArg().pointsTo(cls)
|
||||
) and
|
||||
(
|
||||
cls.isSequence() and
|
||||
cls.isMapping() and
|
||||
res = "IS BOTH. SHOULD NOT HAPPEN. THEY ARE MUTUALLY EXCLUSIVE."
|
||||
or
|
||||
cls.isSequence() and not cls.isMapping() and res = "sequence"
|
||||
or
|
||||
not cls.isSequence() and cls.isMapping() and res = "mapping"
|
||||
or
|
||||
not cls.isSequence() and not cls.isMapping() and res = "neither sequence nor mapping"
|
||||
)
|
||||
select res, cls.toString()
|
||||
@@ -0,0 +1 @@
|
||||
semmle-extractor-options: --max-import-depth=2
|
||||
@@ -0,0 +1,27 @@
|
||||
from collections import OrderedDict, defaultdict
|
||||
import collections.abc
|
||||
|
||||
def test(*args):
|
||||
pass
|
||||
|
||||
test(
|
||||
list,
|
||||
tuple,
|
||||
str,
|
||||
bytes,
|
||||
set,
|
||||
dict,
|
||||
OrderedDict,
|
||||
defaultdict,
|
||||
)
|
||||
|
||||
for seq_cls in (list, tuple, str, bytes):
|
||||
assert issubclass(seq_cls, collections.abc.Sequence)
|
||||
assert not issubclass(seq_cls, collections.abc.Mapping)
|
||||
|
||||
for map_cls in (dict, OrderedDict, defaultdict):
|
||||
assert not issubclass(map_cls, collections.abc.Sequence)
|
||||
assert issubclass(map_cls, collections.abc.Mapping)
|
||||
|
||||
assert not issubclass(set, collections.abc.Sequence)
|
||||
assert not issubclass(set, collections.abc.Mapping)
|
||||
Reference in New Issue
Block a user