You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Secure-Coding-Guide-for-Python/CWE-697/CWE-595/README.md
+26-8Lines changed: 26 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ You want to implement the `__eq__` method on a class if you believe you ever wan
6
6
7
7
## Non-Compliant Code Example
8
8
9
-
The non-compliant code shows how the default comparison operator compares object references rather than the object values. Furthermore, it displays how this causes issues when comparing lists of objects, although it applies to other types of collections as well. Finally, it shows how the `in` operator also depends on the behavior of the `__eq__` method and, therefore, also returns a non-desirable result.
9
+
The non-compliant code shows how the default comparison operator compares object references rather than the object values. Furthermore, it displays how this causes issues when comparing lists of objects, although it applies to other types of collections as well. Then, it shows how the `in` operator also depends on the behavior of the `__eq__` method and, therefore, also returns a non-desirable result. Finally, it performs the comparison with the `is` operator, which checks as to whether the references point to the same object regardless of the stored value.
# And this is equally this will always be False as well
29
29
print(Integer(12) in [Integer(10), Integer(12)])
30
+
# The 'is' will return True only if both references point to the same object
31
+
a = Integer(12)
32
+
b = a
33
+
# Here, a and b point to the same Integer, so 'is' returns True
34
+
print(a is b)
35
+
36
+
b = Integer(12)
37
+
# Even though b still points to an Integer of the same value, it is a new object, so 'is' returns False
38
+
print(a is b)
30
39
31
40
```
32
41
33
42
## Compliant Solution
34
43
35
-
In this compliant solution the `__eq__` method is implemented and all the comparisons now correctly compares the object values, rather than the object reference.
44
+
In this compliant solution, the `__eq__` method is implemented and the comparisons that not use `is`now correctly compare the object values, rather than the object reference. The `is` operator does not call `__eq__`, hence the last print will still display `False`.
36
45
37
46
[*compliant01.py:*](compliant01.py)
38
47
39
48
```py
40
49
""" Compliant Code Example """
41
-
42
-
50
+
51
+
43
52
classInteger:
44
53
def__init__(self, value):
45
54
self.value = value
46
-
55
+
47
56
def__eq__(self, other):
48
57
ifisinstance(other, type(self)):
49
58
returnself.value == other.value
50
59
ifisinstance(other, int):
51
60
returnself.value == other
52
61
returnFalse
53
-
54
-
62
+
63
+
55
64
#####################
56
65
# exploiting above code example
57
66
#####################
58
67
# All these scenarios will now show True
59
68
print(Integer(12) == Integer(12))
60
69
print([Integer(12)] == [Integer(12)])
61
70
print(Integer(12) in [Integer(10), Integer(12)])
62
-
71
+
63
72
# By adding the handling for int we also support
64
73
print(Integer(12) ==12)
74
+
# The 'is' will return True only if both references point to the same object
75
+
a = Integer(12)
76
+
b = a
77
+
# Here, a and b point to the same Integer, so 'is' returns True
78
+
print(a is b)
79
+
80
+
b = Integer(12)
81
+
# Since the 'is' operator does not call __eq__, print below will still return False
0 commit comments