- Notifications
You must be signed in to change notification settings - Fork750
Support comparison operators#1347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
lostmsu merged 12 commits intopythonnet:masterfromchristabella:feat/comparison-operatorsJan 12, 2021
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
12 commits Select commitHold shift + click to select a range
9a8cb96
Add comparison operators
christabellabcbace3
Add tests for logical (bool) comparison operators
christabella31f3ed1
Finish comparison operator impl and add tests
christabellaae7e774
Add eq and ineq operators, disallow reverse for comp
christabella5685727
Minor formatting (whitespace, ordering)
christabella03a73c0
Minor: use `out var` instead of declaring separately.
christabella67928e1
Implement .Equals and .GetHashCode to fix warning
christabella6a64678
Style (TryGetValue), formatting, and minor changes
christabella9dd9162
Use a separate map for comparison operators
christabellaa8c6591
Add tuple comparison operator tests (2/6)
christabella6d4dec7
Add reverse case for tuple comparison op test
christabella7a2b5e2
Simplify PyObj casting, logic of if blocks.
christabellaFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
191 changes: 190 additions & 1 deletionsrc/embed_tests/TestOperator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -25,6 +25,17 @@ public class OperableObject | ||
{ | ||
public int Num { get; set; } | ||
public override int GetHashCode() | ||
{ | ||
return unchecked(159832395 + Num.GetHashCode()); | ||
} | ||
public override bool Equals(object obj) | ||
{ | ||
return obj is OperableObject @object && | ||
Num == @object.Num; | ||
} | ||
public OperableObject(int num) | ||
{ | ||
Num = num; | ||
@@ -149,6 +160,103 @@ public OperableObject(int num) | ||
return new OperableObject(a.Num ^ b); | ||
} | ||
public static bool operator ==(int a, OperableObject b) | ||
{ | ||
return (a == b.Num); | ||
} | ||
public static bool operator ==(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num == b.Num); | ||
} | ||
public static bool operator ==(OperableObject a, int b) | ||
{ | ||
return (a.Num == b); | ||
} | ||
public static bool operator !=(int a, OperableObject b) | ||
{ | ||
return (a != b.Num); | ||
} | ||
public static bool operator !=(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num != b.Num); | ||
} | ||
public static bool operator !=(OperableObject a, int b) | ||
{ | ||
return (a.Num != b); | ||
} | ||
public static bool operator <=(int a, OperableObject b) | ||
{ | ||
return (a <= b.Num); | ||
} | ||
public static bool operator <=(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num <= b.Num); | ||
} | ||
public static bool operator <=(OperableObject a, int b) | ||
{ | ||
return (a.Num <= b); | ||
} | ||
public static bool operator >=(int a, OperableObject b) | ||
{ | ||
return (a >= b.Num); | ||
} | ||
public static bool operator >=(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num >= b.Num); | ||
} | ||
public static bool operator >=(OperableObject a, int b) | ||
{ | ||
return (a.Num >= b); | ||
} | ||
public static bool operator >=(OperableObject a, PyObject b) | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
// Assuming b is a tuple, take the first element. | ||
lostmsu marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
int bNum = b[0].As<int>(); | ||
return a.Num >= bNum; | ||
} | ||
} | ||
public static bool operator <=(OperableObject a, PyObject b) | ||
{ | ||
using (Py.GIL()) | ||
{ | ||
// Assuming b is a tuple, take the first element. | ||
int bNum = b[0].As<int>(); | ||
return a.Num <= bNum; | ||
} | ||
} | ||
public static bool operator <(int a, OperableObject b) | ||
{ | ||
return (a < b.Num); | ||
} | ||
public static bool operator <(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num < b.Num); | ||
} | ||
public static bool operator <(OperableObject a, int b) | ||
{ | ||
return (a.Num < b); | ||
} | ||
public static bool operator >(int a, OperableObject b) | ||
{ | ||
return (a > b.Num); | ||
} | ||
public static bool operator >(OperableObject a, OperableObject b) | ||
{ | ||
return (a.Num > b.Num); | ||
} | ||
public static bool operator >(OperableObject a, int b) | ||
{ | ||
return (a.Num > b); | ||
} | ||
public static OperableObject operator <<(OperableObject a, int offset) | ||
{ | ||
return new OperableObject(a.Num << offset); | ||
@@ -161,7 +269,7 @@ public OperableObject(int num) | ||
} | ||
[Test] | ||
public voidSymmetricalOperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
@@ -206,6 +314,24 @@ public void OperatorOverloads() | ||
c = a ^ b | ||
assert c.Num == a.Num ^ b.Num | ||
c = a == b | ||
assert c == (a.Num == b.Num) | ||
c = a != b | ||
assert c == (a.Num != b.Num) | ||
c = a <= b | ||
assert c == (a.Num <= b.Num) | ||
c = a >= b | ||
assert c == (a.Num >= b.Num) | ||
c = a < b | ||
assert c == (a.Num < b.Num) | ||
c = a > b | ||
assert c == (a.Num > b.Num) | ||
"); | ||
} | ||
@@ -263,6 +389,51 @@ public void ForwardOperatorOverloads() | ||
c = a ^ b | ||
assert c.Num == a.Num ^ b | ||
c = a == b | ||
assert c == (a.Num == b) | ||
c = a != b | ||
assert c == (a.Num != b) | ||
c = a <= b | ||
assert c == (a.Num <= b) | ||
c = a >= b | ||
assert c == (a.Num >= b) | ||
c = a < b | ||
assert c == (a.Num < b) | ||
c = a > b | ||
assert c == (a.Num > b) | ||
"); | ||
} | ||
[Test] | ||
public void TupleComparisonOperatorOverloads() | ||
{ | ||
string name = string.Format("{0}.{1}", | ||
typeof(OperableObject).DeclaringType.Name, | ||
typeof(OperableObject).Name); | ||
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace; | ||
PythonEngine.Exec($@" | ||
from {module} import * | ||
cls = {name} | ||
a = cls(2) | ||
b = (1, 2) | ||
c = a >= b | ||
assert c == (a.Num >= b[0]) | ||
c = a <= b | ||
assert c == (a.Num <= b[0]) | ||
c = b >= a | ||
assert c == (b[0] >= a.Num) | ||
c = b <= a | ||
assert c == (b[0] <= a.Num) | ||
"); | ||
} | ||
@@ -304,6 +475,24 @@ public void ReverseOperatorOverloads() | ||
c = a ^ b | ||
assert c.Num == a ^ b.Num | ||
c = a == b | ||
assert c == (a == b.Num) | ||
c = a != b | ||
assert c == (a != b.Num) | ||
c = a <= b | ||
assert c == (a <= b.Num) | ||
c = a >= b | ||
assert c == (a >= b.Num) | ||
c = a < b | ||
assert c == (a < b.Num) | ||
c = a > b | ||
assert c == (a > b.Num) | ||
"); | ||
} | ||
34 changes: 34 additions & 0 deletionssrc/runtime/classbase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletionssrc/runtime/classmanager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletionssrc/runtime/methodbinder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.