Correct notes on customising Python builtin comparison operators
Also add example for python:compare feature
This commit is contained in:
parent
3b6f4af15c
commit
4f777b181c
3 changed files with 52 additions and 12 deletions
|
|
@ -45,3 +45,26 @@ if is_python_builtin():
|
|||
del d
|
||||
if cvar.Dealloc3CalledCount != 1:
|
||||
raise RuntimeError("count should be 1")
|
||||
|
||||
# Test 5 for python:compare feature
|
||||
m10 = MyClass(10)
|
||||
m20 = MyClass(20)
|
||||
m15 = MyClass(15)
|
||||
|
||||
if not m10 < m15:
|
||||
raise RuntimeError("m10 < m15")
|
||||
if not m10 < m20:
|
||||
raise RuntimeError("m10 < m20")
|
||||
if not m15 < m20:
|
||||
raise RuntimeError("m15 < m20")
|
||||
|
||||
if m10 > m15:
|
||||
raise RuntimeError("m10 > m15")
|
||||
if m10 > m20:
|
||||
raise RuntimeError("m10 > m20")
|
||||
if m15 > m20:
|
||||
raise RuntimeError("m15 > m20")
|
||||
|
||||
if MyClass.less_than_counts != 6:
|
||||
raise RuntimeError("python:compare feature not working")
|
||||
|
||||
|
|
|
|||
|
|
@ -112,3 +112,19 @@ void Dealloc2Destroyer(PyObject *v) {
|
|||
}
|
||||
%}
|
||||
|
||||
// Test 5 for python:compare feature
|
||||
%feature("python:compare", "Py_LT") MyClass::lessThan;
|
||||
|
||||
%inline %{
|
||||
class MyClass {
|
||||
public:
|
||||
MyClass(int val = 0) : val(val) {}
|
||||
bool lessThan(const MyClass& other) const {
|
||||
less_than_counts++;
|
||||
return val < other.val;
|
||||
}
|
||||
int val;
|
||||
static int less_than_counts;
|
||||
};
|
||||
int MyClass::less_than_counts = 0;
|
||||
%}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue