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

Skip to content

Commit 48c0cbe

Browse files
committed
Python: improve handling of __all__
1 parent 3bb61e7 commit 48c0cbe

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ abstract class ModuleObjectInternal extends ObjectInternal {
4747

4848
override predicate subscriptUnknown() { any() }
4949

50+
predicate isInitModule() {
51+
any(PackageObjectInternal package).getInitModule() = this
52+
}
53+
5054
}
5155

5256
class BuiltinModuleObjectInternal extends ModuleObjectInternal, TBuiltinModuleObject {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ class ModuleValue extends Value {
6565
}
6666

6767
predicate exports(string name) {
68-
not this.(ModuleObjectInternal).attribute("__all__", _, _) and this.(ModuleObjectInternal).attribute(name, _, _)
68+
not this.(ModuleObjectInternal).attribute("__all__", _, _) and exists(this.attr(name))
69+
and not name.charAt(0) = "_"
70+
or
71+
py_exports(this.getScope(), name)
6972
}
7073

7174
string getName() {

python/ql/test/3/library-tests/modules/package_members/module_exports.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| Module test_package | module2 |
99
| Module test_package | module3 |
1010
| Module test_package | module4 |
11+
| Module test_package | module5 |
1112
| Module test_package | p |
1213
| Module test_package | q |
1314
| Module test_package | r |
@@ -36,6 +37,7 @@
3637
| Module test_star | module2 |
3738
| Module test_star | module3 |
3839
| Module test_star | module4 |
40+
| Module test_star | module5 |
3941
| Module test_star | p |
4042
| Module test_star | q |
4143
| Module test_star | r |

0 commit comments

Comments
 (0)