mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Python: Add wrapper for isPreferredModuleForName
We talked about how it's annoying that we in 4 places have the same fix `isPreferredModuleForName(<module>.getFile(), <name> + ["", ".__init__"])` , and that it would be nice to have a simple wrapper predicate that ensures we never forget to do the `+ ["", ".__init__"]` dance... I had trouble coming up with a name for this (ironically), but I think `getModuleFromName` is good enough.
This commit is contained in:
@@ -167,9 +167,22 @@ module ImportResolution {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the (most likely) module for the name `name`, if any.
|
||||
*
|
||||
* Handles the fact that for the name `<pkg>` representing a package the actual module
|
||||
* is `<pkg>.__init__`.
|
||||
*
|
||||
* See `isPreferredModuleForName` for more details on what "most likely" module means.
|
||||
*/
|
||||
pragma[inline]
|
||||
private Module getModuleFromName(string name) {
|
||||
isPreferredModuleForName(result.getFile(), name + ["", ".__init__"])
|
||||
}
|
||||
|
||||
/** Gets the module from which attributes are imported by `i`. */
|
||||
Module getModuleImportedByImportStar(ImportStar i) {
|
||||
isPreferredModuleForName(result.getFile(), i.getImportedModuleName() + ["", ".__init__"])
|
||||
result = getModuleFromName(i.getImportedModuleName())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,7 +237,7 @@ module ImportResolution {
|
||||
exists(string module_name | result = getReferenceToModuleName(module_name) |
|
||||
// Depending on whether the referenced module is a package or not, we may need to add a
|
||||
// trailing `.__init__` to the module name.
|
||||
isPreferredModuleForName(m.getFile(), module_name + ["", ".__init__"])
|
||||
m = getModuleFromName(module_name)
|
||||
or
|
||||
// Module defined via `sys.modules`
|
||||
m = sys_modules_module_with_name(module_name)
|
||||
@@ -235,7 +248,7 @@ module ImportResolution {
|
||||
ar.accesses(getModuleReference(p), attr_name) and
|
||||
result = ar
|
||||
|
|
||||
isPreferredModuleForName(m.getFile(), p.getPackageName() + "." + attr_name + ["", ".__init__"])
|
||||
m = getModuleFromName(p.getPackageName() + "." + attr_name)
|
||||
)
|
||||
or
|
||||
// This is also true for attributes that come from reexports.
|
||||
@@ -249,8 +262,7 @@ module ImportResolution {
|
||||
exists(string submodule, Module package |
|
||||
SsaSource::init_module_submodule_defn(result.asVar().getSourceVariable(),
|
||||
package.getEntryNode()) and
|
||||
isPreferredModuleForName(m.getFile(),
|
||||
package.getPackageName() + "." + submodule + ["", ".__init__"])
|
||||
m = getModuleFromName(package.getPackageName() + "." + submodule)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user