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

Skip to content

Commit 66e6461

Browse files
committed
Python points-to: Improve qldoc and internal API a bit.
1 parent e161488 commit 66e6461

6 files changed

Lines changed: 18 additions & 22 deletions

File tree

python/ql/src/semmle/python/Flow.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ class BasicBlock extends @py_flow_node {
10141014
result = this.getLastNode().getAFalseSuccessor().getBasicBlock()
10151015
}
10161016

1017-
/** Gets an exceptional successor to this basic block */
1017+
/** Gets an unconditional successor to this basic block */
10181018
BasicBlock getAnUnconditionalSuccessor() {
10191019
result = this.getASuccessor() and
10201020
not result = this.getATrueSuccessor() and

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ abstract class ClassObjectInternal extends ObjectInternal {
1515
result = this.getClassDeclaration().getName()
1616
}
1717

18+
/** Holds if this is a class whose instances we treat specially, rather than as a generic instance.
19+
* For example, `type` or `int`.
20+
*/
1821
boolean isSpecial() {
19-
result = Types::getMro(this).isSpecial()
22+
result = Types::getMro(this).containsSpecial()
2023
}
2124

2225
/** Looks up the attribute `name` on this class.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class SpecificInstanceInternal extends TSpecificInstance, InstanceObject {
5757
result = this.getOrigin().getNode().toString()
5858
}
5959

60-
/** The boolean value of this object, if it has one */
6160
override boolean booleanValue() {
6261
//result = this.getClass().instancesBooleanValue()
6362
result = maybe()
@@ -261,7 +260,6 @@ class UnknownInstanceInternal extends TUnknownInstance, ObjectInternal {
261260
result = "instance of " + this.getClass().(ClassObjectInternal).getName()
262261
}
263262

264-
/** The boolean value of this object, if it has one */
265263
override boolean booleanValue() {
266264
result = maybe()
267265
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ abstract class SequenceObjectInternal extends ObjectInternal {
1616
/** Gets the `n`th item of this sequence, if one exists. */
1717
abstract ObjectInternal getItem(int n);
1818

19-
/** The boolean value of this object, this may be both
20-
* true and false if the "object" represents a set of possible objects. */
2119
override boolean booleanValue() {
2220
this.length() = 0 and result = false
2321
or
@@ -37,13 +35,11 @@ abstract class SequenceObjectInternal extends ObjectInternal {
3735
abstract class TupleObjectInternal extends SequenceObjectInternal {
3836

3937
override string toString() {
40-
this.length() = 0 and result = "()"
41-
or
4238
result = "(" + this.contents(0) + ")"
4339
}
4440

4541
private string contents(int n) {
46-
n = this.length() - 1 and result = this.getItem(n).toString()
42+
n = this.length() and result = ""
4743
or
4844
result = this.getItem(n).toString() + ", " + this.contents(n+1)
4945
}
@@ -149,6 +145,9 @@ class PythonTupleObjectInternal extends TPythonTuple, TupleObjectInternal {
149145

150146
}
151147

148+
/** The `sys.version_info` object. We treat this specially to prevent premature pruning and
149+
* false positives when we are unsure of the actual version of Python that the code is expecting.
150+
*/
152151
class SysVersionInfoObjectInternal extends TSysVersionInfo, SequenceObjectInternal {
153152

154153
override string toString() {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,18 @@ class ClassList extends TClassList {
231231
reverse_step(this, Empty(), result)
232232
}
233233

234-
boolean isSpecial() {
234+
/** Holds if this MRO contains a class whose instances we treat specially, rather than as a generic instance.
235+
* For example, `type` or `int`.
236+
*/
237+
boolean containsSpecial() {
235238
this = Empty() and result = false
236239
or
237240
exists(ClassDecl decl |
238241
decl = this.getHead().getClassDeclaration() |
239242
if decl.isSpecial() then
240243
result = true
241244
else
242-
result = this.getTail().isSpecial()
245+
result = this.getTail().containsSpecial()
243246
)
244247
}
245248

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ cached module PointsToInternal {
205205
f.(PointsToExtension).pointsTo(context, value, origin)
206206
}
207207

208-
/** Holds if the attribute `name` is required for `obj` */
208+
/** Holds if the attribute `name` is required for `obj`
209+
* For object `x` and attribute `name` it means that there exists somewhere in the code
210+
* `x.name` or `getattr(x, "name")`.
211+
*/
209212
cached predicate attributeRequired(ObjectInternal obj, string name) {
210213
pointsTo(any(AttrNode a).getObject(name), _, obj, _)
211214
or
@@ -689,16 +692,6 @@ module InterModulePointsTo {
689692
)
690693
}
691694

692-
private predicate importsByImportStar(ModuleObjectInternal mod, ModuleObjectInternal imported) {
693-
exists(ImportStarNode isn |
694-
PointsToInternal::pointsTo(isn.getModule(), _, imported, _) and
695-
isn.getScope() = mod.getSourceModule()
696-
)
697-
or exists(ModuleObjectInternal mid |
698-
importsByImportStar(mod, mid) and importsByImportStar(mid, imported)
699-
)
700-
}
701-
702695
predicate ofInterestInExports(ModuleObjectInternal mod, string name) {
703696
exists(ImportStarNode imp, ImportStarRefinement def |
704697
imp = def.getDefiningNode() and

0 commit comments

Comments
 (0)