77 */
88
99// Importing python under the `py` namespace to avoid importing `CallNode` from `Flow.qll` and thereby having a naming conflict with `API::CallNode`.
10- private import python as py
10+ private import python as PY
1111import semmle.python.dataflow.new.DataFlow
12+ private import semmle.python.internal.CachedStages
1213
1314/**
1415 * Provides classes and predicates for working with APIs used in a database.
@@ -427,13 +428,13 @@ module API {
427428 /** An abstract representative for imports of the module called `name`. */
428429 MkModuleImport ( string name ) {
429430 // Ignore the following module name for Python 2, as we alias `__builtin__` to `builtins` elsewhere
430- ( name != "__builtin__" or py :: major_version ( ) = 3 ) and
431+ ( name != "__builtin__" or PY :: major_version ( ) = 3 ) and
431432 (
432433 imports ( _, name )
433434 or
434435 // When we `import foo.bar.baz` we want to create API graph nodes also for the prefixes
435436 // `foo` and `foo.bar`:
436- name = any ( py :: ImportExpr e | not e .isRelative ( ) ) .getAnImportedModuleName ( )
437+ name = any ( PY :: ImportExpr e | not e .isRelative ( ) ) .getAnImportedModuleName ( )
437438 )
438439 or
439440 // The `builtins` module should always be implicitly available
@@ -469,7 +470,7 @@ module API {
469470 * Ignores relative imports, such as `from ..foo.bar import baz`.
470471 */
471472 private predicate imports ( DataFlow:: Node imp , string name ) {
472- exists ( py :: ImportExprNode iexpr |
473+ exists ( PY :: ImportExprNode iexpr |
473474 imp .asCfgNode ( ) = iexpr and
474475 not iexpr .getNode ( ) .isRelative ( ) and
475476 name = iexpr .getNode ( ) .getImportedModuleName ( )
@@ -492,7 +493,7 @@ module API {
492493 *
493494 * `moduleImport("foo").getMember("bar")`
494495 */
495- private TApiNode potential_import_star_base ( py :: Scope s ) {
496+ private TApiNode potential_import_star_base ( PY :: Scope s ) {
496497 exists ( DataFlow:: Node n |
497498 n .asCfgNode ( ) = ImportStar:: potentialImportStarBase ( s ) and
498499 use ( result , n )
@@ -515,17 +516,17 @@ module API {
515516 )
516517 or
517518 // TODO: I had expected `DataFlow::AttrWrite` to contain the attribute writes from a dict, that's how JS works.
518- exists ( py :: Dict dict , py :: KeyValuePair item |
519+ exists ( PY :: Dict dict , PY :: KeyValuePair item |
519520 dict = pred .asExpr ( ) and
520521 dict .getItem ( _) = item and
521- lbl = Label:: member ( item .getKey ( ) .( py :: StrConst ) .getS ( ) ) and
522+ lbl = Label:: member ( item .getKey ( ) .( PY :: StrConst ) .getS ( ) ) and
522523 rhs .asExpr ( ) = item .getValue ( )
523524 )
524525 or
525- exists ( py :: CallableExpr fn | fn = pred .asExpr ( ) |
526+ exists ( PY :: CallableExpr fn | fn = pred .asExpr ( ) |
526527 not fn .getInnerScope ( ) .isAsync ( ) and
527528 lbl = Label:: return ( ) and
528- exists ( py :: Return ret |
529+ exists ( PY :: Return ret |
529530 rhs .asExpr ( ) = ret .getValue ( ) and
530531 ret .getScope ( ) = fn .getInnerScope ( )
531532 )
@@ -568,7 +569,7 @@ module API {
568569 // Subclassing a node
569570 lbl = Label:: subclass ( ) and
570571 exists ( DataFlow:: Node superclass | pred .flowsTo ( superclass ) |
571- ref .asExpr ( ) .( py :: ClassExpr ) .getABase ( ) = superclass .asExpr ( )
572+ ref .asExpr ( ) .( PY :: ClassExpr ) .getABase ( ) = superclass .asExpr ( )
572573 )
573574 or
574575 // awaiting
@@ -579,7 +580,7 @@ module API {
579580 )
580581 )
581582 or
582- exists ( DataFlow:: Node def , py :: CallableExpr fn |
583+ exists ( DataFlow:: Node def , PY :: CallableExpr fn |
583584 rhs ( base , def ) and fn = trackDefNode ( def ) .asExpr ( )
584585 |
585586 exists ( int i |
@@ -598,7 +599,7 @@ module API {
598599 lbl = Label:: member ( any ( string name | ref = Builtins:: likelyBuiltin ( name ) ) )
599600 or
600601 // Unknown variables that may belong to a module imported with `import *`
601- exists ( py :: Scope s |
602+ exists ( PY :: Scope s |
602603 base = potential_import_star_base ( s ) and
603604 lbl =
604605 Label:: member ( any ( string name |
@@ -618,7 +619,7 @@ module API {
618619 )
619620 or
620621 // Ensure the Python 2 `__builtin__` module gets the name of the Python 3 `builtins` module.
621- py :: major_version ( ) = 2 and
622+ PY :: major_version ( ) = 2 and
622623 nd = MkModuleImport ( "builtins" ) and
623624 imports ( ref , "__builtin__" )
624625 or
@@ -683,6 +684,7 @@ module API {
683684 */
684685 cached
685686 DataFlow:: LocalSourceNode trackUseNode ( DataFlow:: LocalSourceNode src ) {
687+ Stages:: TypeTracking:: ref ( ) and
686688 result = trackUseNode ( src , DataFlow:: TypeTracker:: end ( ) ) and
687689 not result instanceof DataFlow:: ModuleVariableNode
688690 }
@@ -759,18 +761,18 @@ module API {
759761 exists ( Builtins:: likelyBuiltin ( member ) ) or
760762 ImportStar:: namePossiblyDefinedInImportStar ( _, member , _) or
761763 Impl:: prefix_member ( _, member , _) or
762- member = any ( py :: Dict d ) .getAnItem ( ) .( py :: KeyValuePair ) .getKey ( ) .( py :: StrConst ) .getS ( )
764+ member = any ( PY :: Dict d ) .getAnItem ( ) .( PY :: KeyValuePair ) .getKey ( ) .( PY :: StrConst ) .getS ( )
763765 } or
764766 MkLabelUnknownMember ( ) or
765767 MkLabelParameter ( int i ) {
766768 exists ( any ( DataFlow:: CallCfgNode c ) .getArg ( i ) )
767769 or
768- exists ( any ( py :: Function f ) .getArg ( i ) )
770+ exists ( any ( PY :: Function f ) .getArg ( i ) )
769771 } or
770772 MkLabelKeywordParameter ( string name ) {
771773 exists ( any ( DataFlow:: CallCfgNode c ) .getArgByName ( name ) )
772774 or
773- exists ( any ( py :: Function f ) .getArgByName ( name ) )
775+ exists ( any ( PY :: Function f ) .getArgByName ( name ) )
774776 } or
775777 MkLabelReturn ( ) or
776778 MkLabelSubclass ( ) or
0 commit comments