Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2802ac2

Browse files
committed
Python: Add NumericValue
Since `IntObjectInternal` extends `TInt`, and `TInt` is defined for all instances of `Builtin.intValue`, and `Builtin.intValue` includes both `int` and `long`, we don't need to handles Longs in a special manner, as we did in NumericObject.
1 parent d30e6d2 commit 2802ac2

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ module Value {
241241
name = "False" and result = TFalse()
242242
}
243243

244-
/** Gets the `Value` for the integer constant `i`, if it exists.
245-
* There will be no `Value` for most integers, but the following are
244+
/** Gets the `NumericValue` for the integer constant `i`, if it exists.
245+
* There will be no `NumericValue` for most integers, but the following are
246246
* guaranteed to exist:
247247
* * From zero to 511 inclusive.
248248
* * All powers of 2 (up to 2**30)
249249
* * Any integer explicitly mentioned in the source program.
250250
*/
251-
Value forInt(int i) {
251+
NumericValue forInt(int i) {
252252
result.(IntObjectInternal).intValue() = i
253253
}
254254

@@ -620,6 +620,26 @@ class StringValue extends Value {
620620
}
621621
}
622622

623+
/** A class representing numbers (ints and floats), either present in the source as a literal,
624+
* or in a builtin as a value.
625+
*/
626+
class NumericValue extends Value {
627+
NumericValue() {
628+
this instanceof IntObjectInternal or
629+
this instanceof FloatObjectInternal
630+
}
631+
632+
/** Gets the integer-value if it is a constant integer, and it fits in a QL int */
633+
int getIntValue() {
634+
result = this.(IntObjectInternal).intValue()
635+
}
636+
637+
/** Gets the float-value if it is a constant float */
638+
int getFloatValue() {
639+
result = this.(FloatObjectInternal).floatValue()
640+
}
641+
}
642+
623643
/** A method-resolution-order sequence of classes */
624644
class MRO extends TClassList {
625645

0 commit comments

Comments
 (0)