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

Skip to content

Operator overloads support #1324

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5130aaa
Overload operators except for bit shifts and default args.
christabella Dec 16, 2020
0d0ca87
Remove params array tests since they are unrelated to operator overlo…
christabella Dec 17, 2020
253f9c7
Fix type bug; rename variables pynargs etc.
christabella Dec 17, 2020
8cdb61c
Remove unused variables and add comments.
christabella Dec 17, 2020
c26e589
Add comments and remove unused in operatormethod.cs
christabella Dec 18, 2020
3222a54
Address review by @lostmsu; Add forward operator test.
christabella Dec 20, 2020
550ff31
Add forward operator tests (OpObj, int).
christabella Dec 21, 2020
eab8edc
Address @amos402's comment on raising branch.
christabella Dec 21, 2020
c2be3f1
Add operator overload and rename pynargs, clrnargs
christabella Dec 21, 2020
581a047
Revert variable renames
christabella Dec 22, 2020
e11327f
Update AUTHORS and CHANGELOG
christabella Dec 22, 2020
35be4bc
Revert rename to pynargs
christabella Dec 23, 2020
f19c281
Address @tminka's comments.
christabella Dec 23, 2020
d7f52d2
Remove whitespace
christabella Dec 23, 2020
5855a1b
Fix nits
christabella Dec 24, 2020
e7da0bc
Support reverse binary operations
christabella Dec 28, 2020
a376838
Use reverse instead of forward (semantics)
christabella Dec 29, 2020
6923a78
Address @tminka's comments
christabella Dec 30, 2020
4c992d8
Support unary neg and pos operators
christabella Dec 30, 2020
8cce61d
Remove isOperator from MatchesArgumentCount (simplify), add test.
christabella Jan 4, 2021
09a2047
Revert "Remove isOperator from MatchesArgumentCount (simplify)"
christabella Jan 4, 2021
41bd07f
Properly remove isOperator from MatchesArgumentCount (@tminka comment)
christabella Jan 4, 2021
10ccf1e
Update changelog.
christabella Jan 4, 2021
5682e0c
Add ones complement and modulo tests (@tminka comment)
christabella Jan 4, 2021
5f45c70
Merge branch 'master' into feat/operator-overloads
lostmsu Jan 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Benoît Hudson ([@benoithudson](https://github.com/benoithudson))
- Bradley Friedman ([@leith-bartrich](https://github.com/leith-bartrich))
- Callum Noble ([@callumnoble](https://github.com/callumnoble))
- Christabella Irwanto([@christabella](https://github.com/christabella))
- Christian Heimes ([@tiran](https://github.com/tiran))
- Christoph Gohlke ([@cgohlke](https://github.com/cgohlke))
- Christopher Bremner ([@chrisjbremner](https://github.com/chrisjbremner))
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
### Added

- Ability to instantiate new .NET arrays using `Array[T](dim1, dim2, ...)` syntax
- Python operator method will call C# operator method for supported binary and unary operators ([#1324][p1324]).

### Changed
- Drop support for Python 2, 3.4, and 3.5
Expand Down
332 changes: 332 additions & 0 deletions src/embed_tests/TestOperator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
using NUnit.Framework;

using Python.Runtime;

using System.Linq;
using System.Reflection;

namespace Python.EmbeddingTest
{
public class TestOperator
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

public class OperableObject
{
public int Num { get; set; }

public OperableObject(int num)
{
Num = num;
}

public static OperableObject operator ~(OperableObject a)
{
return new OperableObject(~a.Num);
}

public static OperableObject operator +(OperableObject a)
{
return new OperableObject(+a.Num);
}

public static OperableObject operator -(OperableObject a)
{
return new OperableObject(-a.Num);
}

public static OperableObject operator +(int a, OperableObject b)
{
return new OperableObject(a + b.Num);
}
public static OperableObject operator +(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num + b.Num);
}
public static OperableObject operator +(OperableObject a, int b)
{
return new OperableObject(a.Num + b);
}

public static OperableObject operator -(int a, OperableObject b)
{
return new OperableObject(a - b.Num);
}
public static OperableObject operator -(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num - b.Num);
}
public static OperableObject operator -(OperableObject a, int b)
{
return new OperableObject(a.Num - b);
}

public static OperableObject operator *(int a, OperableObject b)
{
return new OperableObject(a * b.Num);
}
public static OperableObject operator *(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num * b.Num);
}
public static OperableObject operator *(OperableObject a, int b)
{
return new OperableObject(a.Num * b);
}

public static OperableObject operator /(int a, OperableObject b)
{
return new OperableObject(a / b.Num);
}
public static OperableObject operator /(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num / b.Num);
}
public static OperableObject operator /(OperableObject a, int b)
{
return new OperableObject(a.Num / b);
}

public static OperableObject operator %(int a, OperableObject b)
{
return new OperableObject(a % b.Num);
}
public static OperableObject operator %(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num % b.Num);
}
public static OperableObject operator %(OperableObject a, int b)
{
return new OperableObject(a.Num % b);
}

public static OperableObject operator &(int a, OperableObject b)
{
return new OperableObject(a & b.Num);
}
public static OperableObject operator &(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num & b.Num);
}
public static OperableObject operator &(OperableObject a, int b)
{
return new OperableObject(a.Num & b);
}

public static OperableObject operator |(int a, OperableObject b)
{
return new OperableObject(a | b.Num);
}
public static OperableObject operator |(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num | b.Num);
}
public static OperableObject operator |(OperableObject a, int b)
{
return new OperableObject(a.Num | b);
}

public static OperableObject operator ^(int a, OperableObject b)
{
return new OperableObject(a ^ b.Num);
}
public static OperableObject operator ^(OperableObject a, OperableObject b)
{
return new OperableObject(a.Num ^ b.Num);
}
public static OperableObject operator ^(OperableObject a, int b)
{
return new OperableObject(a.Num ^ b);
}

public static OperableObject operator <<(OperableObject a, int offset)
{
return new OperableObject(a.Num << offset);
}

public static OperableObject operator >>(OperableObject a, int offset)
{
return new OperableObject(a.Num >> offset);
}
}

[Test]
public void OperatorOverloads()
{
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 = cls(10)
c = ~a
assert c.Num == ~a.Num

c = +a
assert c.Num == +a.Num

a = cls(2)
c = -a
assert c.Num == -a.Num

c = a + b
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, both of these calls will succeed (and do the same thing):

a.op_Addition(b)
a.op_Addition(a,b)

Is that desirable behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the first statement would have previously failed and now succeeds. I guess this is a question for @lostmsu if we want to let something that's wrongly written, not fail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a.op_Addition(a, b) must be failing for operators, defined in C#.

Copy link
Contributor

@tminka tminka Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code I wrote is Python code. If a.op_Addition(a,b) now fails in Python.NET, then it will break existing code using Python.NET.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking should not be a big deal, since we are making major version bump with (hopefully) a good overhaul.
But I don't think a.op_Addition(a, b) should be working now.

assert c.Num == a.Num + b.Num

c = a - b
assert c.Num == a.Num - b.Num

c = a * b
assert c.Num == a.Num * b.Num

c = a / b
assert c.Num == a.Num // b.Num

c = a % b
assert c.Num == a.Num % b.Num

c = a & b
assert c.Num == a.Num & b.Num

c = a | b
assert c.Num == a.Num | b.Num

c = a ^ b
assert c.Num == a.Num ^ b.Num
");
}

[Test]
public void OperatorOverloadMissingArgument()
{
string name = string.Format("{0}.{1}",
typeof(OperableObject).DeclaringType.Name,
typeof(OperableObject).Name);
string module = MethodBase.GetCurrentMethod().DeclaringType.Namespace;

Assert.Throws<PythonException>(() =>
PythonEngine.Exec($@"
from {module} import *
cls = {name}
a = cls(2)
b = cls(10)
a.op_Addition()
"));
}

[Test]
public void ForwardOperatorOverloads()
{
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 = 10
c = a + b
assert c.Num == a.Num + b

c = a - b
assert c.Num == a.Num - b

c = a * b
assert c.Num == a.Num * b

c = a / b
assert c.Num == a.Num // b

c = a % b
assert c.Num == a.Num % b

c = a & b
assert c.Num == a.Num & b

c = a | b
assert c.Num == a.Num | b

c = a ^ b
assert c.Num == a.Num ^ b
");
}


[Test]
public void ReverseOperatorOverloads()
{
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 = 2
b = cls(10)

c = a + b
assert c.Num == a + b.Num

c = a - b
assert c.Num == a - b.Num

c = a * b
assert c.Num == a * b.Num

c = a / b
assert c.Num == a // b.Num

c = a % b
assert c.Num == a % b.Num

c = a & b
assert c.Num == a & b.Num

c = a | b
assert c.Num == a | b.Num

c = a ^ b
assert c.Num == a ^ b.Num
");

}
[Test]
public void ShiftOperatorOverloads()
{
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 = cls(10)

c = a << b.Num
assert c.Num == a.Num << b.Num

c = a >> b.Num
assert c.Num == a.Num >> b.Num
");
}
}
}
13 changes: 13 additions & 0 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ private static ClassInfo GetClassInfo(Type type)

ob = new MethodObject(type, name, mlist);
ci.members[name] = ob;
if (mlist.Any(OperatorMethod.IsOperatorMethod))
{
string pyName = OperatorMethod.GetPyMethodName(name);
string pyNameReverse = OperatorMethod.ReversePyMethodName(pyName);
MethodInfo[] forwardMethods, reverseMethods;
OperatorMethod.FilterMethods(mlist, out forwardMethods, out reverseMethods);
// Only methods where the left operand is the declaring type.
if (forwardMethods.Length > 0)
ci.members[pyName] = new MethodObject(type, name, forwardMethods);
// Only methods where only the right operand is the declaring type.
if (reverseMethods.Length > 0)
ci.members[pyNameReverse] = new MethodObject(type, name, reverseMethods);
}
}

if (ci.indexer == null && type.IsClass)
Expand Down
Loading