Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dfc0a80

Browse files
committed
Python points-to: Handle old-style classes correctly.
1 parent 3ca4524 commit dfc0a80

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

python/ql/src/semmle/python/objects/ObjectInternal.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ module ObjectInternal {
410410
result = TBuiltinClassObject(Builtin::special("NoneType"))
411411
}
412412

413+
ObjectInternal type() {
414+
result = TBuiltinClassObject(Builtin::special("type"))
415+
}
416+
413417
ObjectInternal property() {
414418
result = TBuiltinClassObject(Builtin::special("property"))
415419
}

python/ql/src/semmle/python/objects/TObject.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ library class ClassDecl extends @py_object {
331331
result = "ClassDecl"
332332
}
333333

334-
private Class getClass() {
334+
Class getClass() {
335335
result = this.(ControlFlowNode).getNode().(ClassExpr).getInnerScope()
336336
}
337337

python/ql/src/semmle/python/pointsto/PointsTo.qll

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,21 +1610,45 @@ cached module Types {
16101610
}
16111611

16121612
cached predicate isOldStyle(ClassObjectInternal cls) {
1613-
//To do...
1614-
none()
1613+
newStylePython2(cls, 0) = false
16151614
}
16161615

16171616
cached predicate isNewStyle(ClassObjectInternal cls) {
1618-
//To do...
1619-
any()
1617+
major_version() = 3
1618+
or
1619+
cls.isBuiltin()
1620+
or
1621+
newStylePython2(cls, 0) = true
1622+
}
1623+
1624+
private boolean newStylePython2(ClassObjectInternal cls, int n) {
1625+
major_version() = 2 and
1626+
(
1627+
hasDeclaredMetaclass(cls) = false and
1628+
exists(Class pycls |
1629+
pycls = cls.getClassDeclaration().getClass() and
1630+
n = count(pycls.getABase()) and result = false
1631+
)
1632+
or
1633+
hasDeclaredMetaclass(cls) = false and
1634+
exists(ClassObjectInternal base |
1635+
base = getBase(cls, n) |
1636+
isOldStyle(base) and result = newStylePython2(cls, n+1)
1637+
or
1638+
isNewStyle(base) and result = true
1639+
)
1640+
or
1641+
getMro(declaredMetaClass(cls)).contains(ObjectInternal::type()) and
1642+
n = 0 and result = true
1643+
)
16201644
}
16211645

16221646
cached ClassList getMro(ClassObjectInternal cls) {
16231647
isNewStyle(cls) and
16241648
result = Mro::newStyleMro(cls)
16251649
or
1626-
// To do, old-style
1627-
none()
1650+
isOldStyle(cls) and
1651+
result = Mro::oldStyleMro(cls)
16281652
}
16291653

16301654
cached predicate declaredAttribute(ClassObjectInternal cls, string name, ObjectInternal value, CfgOrigin origin) {

0 commit comments

Comments
 (0)