Skip to content

Commit 838e37c

Browse files
committed
Python: ObjectAPI to ValueAPI: ReturnValueIgnore: Moves getAnInferredType to CallableObjectInternal
1 parent 12377ba commit 838e37c

File tree

3 files changed

+9
-40
lines changed

3 files changed

+9
-40
lines changed

python/ql/src/Functions/ReturnValueIgnored.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import python
18+
import semmle.python.objects.Callables
1819

1920
predicate meaningful_return_value(Expr val) {
2021
val instanceof Name
@@ -44,7 +45,7 @@ predicate returns_meaningful_value(FunctionValue f) {
4445
or
4546
/* Is f a builtin function that returns something other than None?
4647
* Ignore __import__ as it is often called purely for side effects */
47-
f.isBuiltin() and f.getAnInferredReturnType() != ClassValue::nonetype() and not f.getName() = "__import__"
48+
f.isBuiltin() and f.(CallableObjectInternal).getAnInferredReturnType() != ClassValue::nonetype() and not f.getName() = "__import__"
4849
)
4950
}
5051

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ abstract class CallableObjectInternal extends ObjectInternal {
4949

5050
/* Callables aren't iterable */
5151
override ObjectInternal getIterNext() { none() }
52+
53+
/** Gets a class that this function may return */
54+
ClassValue getAnInferredReturnType() {
55+
result = TBuiltinClassObject(this.(BuiltinFunctionObjectInternal).getReturnType())
56+
or
57+
result = TBuiltinClassObject(this.(BuiltinMethodObjectInternal).getReturnType())
58+
}
5259
}
5360

5461
/** Class representing Python functions */

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -581,13 +581,6 @@ abstract class FunctionValue extends CallableValue {
581581
exists(Expr expr, AstNode origin | expr.pointsTo(this, origin) | not origin instanceof Lambda)
582582
)
583583
}
584-
585-
/** Gets a class that this function may return */
586-
ClassValue getAnInferredReturnType() {
587-
result = this.(BuiltinFunctionValue).getAReturnType()
588-
or
589-
result = this.(BuiltinMethodValue).getAReturnType()
590-
}
591584
}
592585

593586
/** Class representing Python functions */
@@ -627,29 +620,6 @@ class BuiltinFunctionValue extends FunctionValue {
627620
override int minParameters() { none() }
628621

629622
override int maxParameters() { none() }
630-
631-
ClassValue getAReturnType() {
632-
/* Enumerate the types of a few builtin functions, that the CPython analysis misses.
633-
*/
634-
this = TBuiltinFunctionObject(Builtin::builtin("hex")) and result = ClassValue::str()
635-
or
636-
this = TBuiltinFunctionObject(Builtin::builtin("oct")) and result = ClassValue::str()
637-
or
638-
this = TBuiltinFunctionObject(Builtin::builtin("intern")) and result = ClassValue::str()
639-
or
640-
/* Fix a few minor inaccuracies in the CPython analysis */
641-
exists(Builtin mthd, Builtin cls | this = TBuiltinFunctionObject(mthd) and result = TBuiltinClassObject(cls)
642-
| ext_rettype(mthd, cls)) and
643-
not (
644-
this = TBuiltinFunctionObject(Builtin::builtin("__import__")) and result = ClassValue::nonetype()
645-
or
646-
this = TBuiltinFunctionObject(Builtin::builtin("compile")) and result = ClassValue::nonetype()
647-
or
648-
this = TBuiltinFunctionObject(Builtin::builtin("sum"))
649-
or
650-
this = TBuiltinFunctionObject(Builtin::builtin("filter"))
651-
)
652-
}
653623
}
654624

655625
/** Class representing builtin methods, such as `list.append` or `set.add` */
@@ -667,15 +637,6 @@ class BuiltinMethodValue extends FunctionValue {
667637
override int minParameters() { none() }
668638

669639
override int maxParameters() { none() }
670-
671-
ClassValue getAReturnType() {
672-
exists(Builtin mthd, Builtin cls |
673-
this = TBuiltinMethodObject(mthd) and
674-
result = TBuiltinClassObject(cls)
675-
|
676-
ext_rettype(mthd, cls)
677-
)
678-
}
679640
}
680641

681642
/**

0 commit comments

Comments
 (0)