Skip to content

Commit ba2bbf1

Browse files
committed
Python: Extend Value API.
Adds - `StringValue` as a new class, - `Value::booleanValue` which returns the boolean interpretation of the given value, and - `ClassValue::str` which returns the value of the `str` class, depending on the Python version.
1 parent 007b079 commit ba2bbf1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ class Value extends TObject {
117117
my_class.getABaseType+() = other_class
118118
)
119119
}
120+
121+
/** Gets the boolean value of this value. */
122+
boolean booleanValue() {
123+
result = this.(ObjectInternal).booleanValue()
124+
}
120125
}
121126

122127
/** Class representing modules in the Python program
@@ -594,6 +599,22 @@ class TupleValue extends SequenceValue {
594599

595600
}
596601

602+
/** A class representing strings, either present in the source as a literal, or
603+
in a builtin as a value. */
604+
605+
class StringValue extends Value {
606+
StringValue() {
607+
this instanceof BytesObjectInternal or
608+
this instanceof UnicodeObjectInternal
609+
}
610+
611+
string getText() {
612+
result = this.(BytesObjectInternal).strValue()
613+
or
614+
result = this.(UnicodeObjectInternal).strValue()
615+
}
616+
}
617+
597618
/** A method-resolution-order sequence of classes */
598619
class MRO extends TClassList {
599620

@@ -684,6 +705,15 @@ module ClassValue {
684705
result = TBuiltinClassObject(Builtin::special("unicode"))
685706
}
686707

708+
/** Get the `ClassValue` for the `str` class. This is `bytes` in Python 2,
709+
and `str` in Python 3. */
710+
ClassValue str() {
711+
if major_version() = 2 then
712+
result = bytes()
713+
else
714+
result = unicode()
715+
}
716+
687717
/** Get the `ClassValue` for the `classmethod` class. */
688718
ClassValue classmethod() {
689719
result = TBuiltinClassObject(Builtin::special("ClassMethod"))

0 commit comments

Comments
 (0)