Skip to content

Commit 0438b60

Browse files
committed
Merge branch 'objectapi-to-valueapi-truncateddivision' of github.com:BekaValentine/ql into objectapi-to-valueapi-truncateddivision
2 parents 28be3b4 + 8e80e9f commit 0438b60

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,62 @@ class NumericValue extends Value {
200200

201201
}
202202

203+
/** Numeric values (ints and floats).
204+
* Includes those occurring in the source as a literal
205+
* or in a builtin module as a value.
206+
*/
207+
class NumericValue extends Value {
208+
209+
NumericValue() {
210+
this.asBuiltin().getClass() = theIntType().asBuiltin() or
211+
this.asBuiltin().getClass() = theLongType().asBuiltin() or
212+
this.asBuiltin().getClass() = theFloatType().asBuiltin()
213+
}
214+
215+
/** Gets the Boolean value that this object
216+
* would evaluate to in a Boolean context,
217+
* such as `bool(x)` or `if x: ...`
218+
*/
219+
override boolean booleanValue() {
220+
this.intValue() != 0 and result = true
221+
or
222+
this.intValue() = 0 and result = false
223+
or
224+
this.floatValue() != 0 and result = true
225+
or
226+
this.floatValue() = 0 and result = false
227+
}
228+
229+
/** Gets the value of this object if it is a constant integer and it fits in a QL int */
230+
int intValue() {
231+
(
232+
this.asBuiltin().getClass() = theIntType().asBuiltin() or
233+
this.asBuiltin().getClass() = theLongType().asBuiltin()
234+
)
235+
and
236+
result = this.asBuiltin().getName().toInt()
237+
}
238+
239+
/** Gets the value of this object if it is a constant float */
240+
float floatValue() {
241+
this.asBuiltin().getClass() = theFloatType().asBuiltin()
242+
and
243+
result = this.asBuiltin().getName().toFloat()
244+
}
245+
246+
/** Gets the string representation of this object, equivalent to calling repr() in Python */
247+
string repr() {
248+
exists(string s |
249+
s = this.asBuiltin().getName() |
250+
if this.asBuiltin().getClass() = theLongType().asBuiltin() then
251+
result = s + "L"
252+
else
253+
result = s
254+
)
255+
}
256+
257+
}
258+
203259
/** Class representing modules in the Python program
204260
* Each `ModuleValue` represents a module object in the Python program.
205261
*/

0 commit comments

Comments
 (0)