Merge pull request #835 from markshannon/python-compare-is-enum

Python: Fix 'comparison using is' query to account for enum members.
This commit is contained in:
Taus
2019-01-28 16:22:57 +01:00
committed by GitHub
3 changed files with 34 additions and 1 deletions

View File

@@ -107,6 +107,20 @@ predicate invalid_portable_is_comparison(Compare comp, Cmpop op, ClassObject cls
left.refersTo(obj) and right.refersTo(obj) and
exists(ImmutableLiteral il | il.getLiteralObject() = obj)
)
and
/* OK to use 'is' when comparing with a member of an enum */
not exists(Expr left, Expr right, AstNode origin |
comp.compares(left, op, right) and
enum_member(origin) |
left.refersTo(_, origin) or right.refersTo(_, origin)
)
}
private predicate enum_member(AstNode obj) {
exists(ClassObject cls, AssignStmt asgn |
cls.getASuperType().getName() = "Enum" |
cls.getPyClass() = asgn.getScope() and
asgn.getValue() = obj
)
}

View File

@@ -24,7 +24,7 @@
#ODASA-4519
#OK as we are using identity tests for unique objects
V2 = "v2"
V3 = "v3"
@@ -85,3 +85,21 @@ def both_sides_known(zero_based="auto", query_id=False):
if zero_based is False: # False positive here
pass
#Avoid depending on enum back port for Python 2 tests:
class Enum(object):
pass
class MyEnum(Enum):
memberA = None
memberB = 10
memberC = ("Hello", "World")
def comp_enum(x):
if x is MyEnum.memberA:
return
if x is MyEnum.memberB:
return
if x is MyEnum.memberC:
return