Skip to content

Commit 72fddaf

Browse files
authored
Merge pull request #2733 from tausbn/python-add-stringvalue
Python: Extend `Value` API.
2 parents 7855a0b + ba2bbf1 commit 72fddaf

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
@@ -599,6 +604,22 @@ class TupleValue extends SequenceValue {
599604

600605
}
601606

607+
/** A class representing strings, either present in the source as a literal, or
608+
in a builtin as a value. */
609+
610+
class StringValue extends Value {
611+
StringValue() {
612+
this instanceof BytesObjectInternal or
613+
this instanceof UnicodeObjectInternal
614+
}
615+
616+
string getText() {
617+
result = this.(BytesObjectInternal).strValue()
618+
or
619+
result = this.(UnicodeObjectInternal).strValue()
620+
}
621+
}
622+
602623
/** A method-resolution-order sequence of classes */
603624
class MRO extends TClassList {
604625

@@ -694,6 +715,15 @@ module ClassValue {
694715
result = TBuiltinClassObject(Builtin::special("unicode"))
695716
}
696717

718+
/** Get the `ClassValue` for the `str` class. This is `bytes` in Python 2,
719+
and `str` in Python 3. */
720+
ClassValue str() {
721+
if major_version() = 2 then
722+
result = bytes()
723+
else
724+
result = unicode()
725+
}
726+
697727
/** Get the `ClassValue` for the `classmethod` class. */
698728
ClassValue classmethod() {
699729
result = TBuiltinClassObject(Builtin::special("ClassMethod"))

0 commit comments

Comments
 (0)