Skip to content

Commit 9d78779

Browse files
authored
Merge pull request #964 from markshannon/python-locations-for-packages
Python: Make sure packages have locations.
2 parents 681ff0f + d46467f commit 9d78779

File tree

7 files changed

+18
-4
lines changed

7 files changed

+18
-4
lines changed

python/ql/src/semmle/python/Files.qll

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,14 @@ class Location extends @location {
391391

392392
/** Gets the file for this location */
393393
File getFile() {
394+
result = this.getPath()
395+
}
396+
397+
private Container getPath() {
394398
locations_default(this, result, _, _, _, _)
395399
or
396400
exists(Module m | locations_ast(this, m, _, _, _, _) |
397-
result = m.getFile()
401+
result = m.getPath()
398402
)
399403
}
400404

@@ -423,7 +427,7 @@ class Location extends @location {
423427
}
424428

425429
string toString() {
426-
result = this.getFile().getName() + ":" + this.getStartLine().toString()
430+
result = this.getPath().getName() + ":" + this.getStartLine().toString()
427431
}
428432

429433
predicate hasLocationInfo(string filepath, int bl, int bc, int el, int ec) {

python/ql/src/semmle/python/Module.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class Module extends Module_, Scope, AstNode {
115115

116116
override Location getLocation() {
117117
py_scope_location(result, this)
118+
or
119+
not py_scope_location(_, this) and
120+
locations_ast(result, this, 0, 0, 0, 0)
118121
}
119122

120123
/** Gets a child module or package of this package */

python/ql/test/library-tests/PointsTo/new/PointsToWithContext.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@
403403
| i_imports.py:18 | ControlFlowNode for sys | Module sys | builtin-class module | 17 | import |
404404
| i_imports.py:23 | ControlFlowNode for ImportExpr | Module code | builtin-class module | 23 | import |
405405
| i_imports.py:23 | ControlFlowNode for code | Module code | builtin-class module | 23 | import |
406+
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package | builtin-class module | 0 | import |
406407
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package.x | builtin-class module | 0 | import |
407408
| i_imports.py:24 | ControlFlowNode for code | Module code | builtin-class module | 23 | import |
408409
| i_imports.py:27 | ControlFlowNode for ImportExpr | Module code.test_package | builtin-class module | 27 | import |

python/ql/test/library-tests/PointsTo/new/PointsToWithType.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@
512512
| i_imports.py:18 | ControlFlowNode for sys | Module sys | builtin-class module | 17 |
513513
| i_imports.py:23 | ControlFlowNode for ImportExpr | Module code | builtin-class module | 23 |
514514
| i_imports.py:23 | ControlFlowNode for code | Module code | builtin-class module | 23 |
515+
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package | builtin-class module | 0 |
515516
| i_imports.py:24 | ControlFlowNode for Attribute | Module code.package.x | builtin-class module | 0 |
516517
| i_imports.py:24 | ControlFlowNode for code | Module code | builtin-class module | 23 |
517518
| i_imports.py:27 | ControlFlowNode for ImportExpr | Module code.test_package | builtin-class module | 27 |

python/ql/test/library-tests/PointsTo/new/Sanity.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ predicate ssa_sanity(string clsname, string problem, string what) {
9595
not exists(m.getName()) and
9696
problem = "does not have a name"
9797
or
98+
not m.isPackage() and
9899
not exists(Variable v | v.getId() = "__name__" and v.getScope() = m) and
99100
problem = "does not have a __name__ variable"
100101
or
102+
not m.isPackage() and
101103
not exists(PyNodeDefinition def |
102104
def.getDefiningNode().getScope() = m and
103105
def.getVariable().getName() = "__name__"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

python/ql/test/query-tests/analysis/Sanity/test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ def method(self):
2121
self.inst_attr
2222
self.shadowing
2323

24-
#ODASA-3836
24+
2525
def comprehensions_and_generators(seq):
2626
[y*y for y in seq]
2727
(y*y for y in seq)
2828
{y*y for y in seq}
2929
{y:y*y for y in seq}
3030

31-
#ODASA-5391
31+
3232
@decorator(x)
3333
class Decorated(object):
3434
pass
3535

3636
d = Decorated()
37+
38+
import package

0 commit comments

Comments
 (0)