2019-07-22 03:05:03 +00:00
|
|
|
#![feature(specialization)]
|
|
|
|
|
2019-02-23 17:01:22 +00:00
|
|
|
use pyo3::class::basic::CompareOp;
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::class::*;
|
2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
2019-06-13 09:09:17 +00:00
|
|
|
use pyo3::py_run;
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2020-03-29 13:40:20 +00:00
|
|
|
struct UnaryArithmetic {
|
|
|
|
inner: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnaryArithmetic {
|
|
|
|
fn new(value: f64) -> Self {
|
|
|
|
UnaryArithmetic { inner: value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyproto]
|
|
|
|
impl PyObjectProtocol for UnaryArithmetic {
|
|
|
|
fn __repr__(&self) -> PyResult<String> {
|
|
|
|
Ok(format!("UA({})", self.inner))
|
|
|
|
}
|
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyNumberProtocol for UnaryArithmetic {
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __neg__(&self) -> PyResult<Self> {
|
|
|
|
Ok(Self::new(-self.inner))
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __pos__(&self) -> PyResult<Self> {
|
|
|
|
Ok(Self::new(self.inner))
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __abs__(&self) -> PyResult<Self> {
|
|
|
|
Ok(Self::new(self.inner.abs()))
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __round__(&self, _ndigits: Option<u32>) -> PyResult<Self> {
|
|
|
|
Ok(Self::new(self.inner.round()))
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unary_arithmetic() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-29 22:12:26 +00:00
|
|
|
let c = PyCell::new(py, UnaryArithmetic::new(2.7)).unwrap();
|
|
|
|
py_run!(py, c, "assert repr(-c) == 'UA(-2.7)'");
|
|
|
|
py_run!(py, c, "assert repr(+c) == 'UA(2.7)'");
|
|
|
|
py_run!(py, c, "assert repr(abs(c)) == 'UA(2.7)'");
|
2020-03-29 13:40:20 +00:00
|
|
|
py_run!(py, c, "assert repr(round(c)) == 'UA(3)'");
|
|
|
|
py_run!(py, c, "assert repr(round(c, 1)) == 'UA(3)'");
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-09-02 21:33:45 +00:00
|
|
|
struct BinaryArithmetic {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyObjectProtocol for BinaryArithmetic {
|
|
|
|
fn __repr__(&self) -> PyResult<&'static str> {
|
|
|
|
Ok("BA")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct InPlaceOperations {
|
|
|
|
value: u32,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyObjectProtocol for InPlaceOperations {
|
|
|
|
fn __repr__(&self) -> PyResult<String> {
|
|
|
|
Ok(format!("IPO({:?})", self.value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyNumberProtocol for InPlaceOperations {
|
|
|
|
fn __iadd__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value += other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __isub__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value -= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __imul__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value *= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __ilshift__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value <<= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __irshift__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value >>= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __iand__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value &= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __ixor__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value ^= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __ior__(&mut self, other: u32) -> PyResult<()> {
|
|
|
|
self.value |= other;
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-29 13:40:20 +00:00
|
|
|
|
2020-03-29 18:01:44 +00:00
|
|
|
fn __ipow__(&mut self, other: u32) -> PyResult<()> {
|
2020-03-29 13:40:20 +00:00
|
|
|
self.value = self.value.pow(other);
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inplace_operations() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2018-06-15 19:21:12 +00:00
|
|
|
let init = |value, code| {
|
2020-03-28 15:26:11 +00:00
|
|
|
let c = PyCell::new(py, InPlaceOperations { value }).unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py_run!(py, c, code);
|
|
|
|
};
|
|
|
|
|
|
|
|
init(0, "d = c; c += 1; assert repr(c) == repr(d) == 'IPO(1)'");
|
|
|
|
init(10, "d = c; c -= 1; assert repr(c) == repr(d) == 'IPO(9)'");
|
|
|
|
init(3, "d = c; c *= 3; assert repr(c) == repr(d) == 'IPO(9)'");
|
|
|
|
init(3, "d = c; c <<= 2; assert repr(c) == repr(d) == 'IPO(12)'");
|
|
|
|
init(12, "d = c; c >>= 2; assert repr(c) == repr(d) == 'IPO(3)'");
|
|
|
|
init(12, "d = c; c &= 10; assert repr(c) == repr(d) == 'IPO(8)'");
|
|
|
|
init(12, "d = c; c |= 3; assert repr(c) == repr(d) == 'IPO(15)'");
|
|
|
|
init(12, "d = c; c ^= 5; assert repr(c) == repr(d) == 'IPO(9)'");
|
2020-03-29 13:40:20 +00:00
|
|
|
init(3, "d = c; c **= 4; assert repr(c) == repr(d) == 'IPO(81)'");
|
|
|
|
init(
|
|
|
|
3,
|
|
|
|
"d = c; c.__ipow__(4); assert repr(c) == repr(d) == 'IPO(81)'",
|
|
|
|
);
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyNumberProtocol for BinaryArithmetic {
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __add__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} + {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __sub__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} - {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __mul__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} * {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __lshift__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} << {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __rshift__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} >> {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __and__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} & {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __xor__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} ^ {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __or__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
Ok(format!("{:?} | {:?}", lhs, rhs))
|
|
|
|
}
|
2020-03-29 13:40:20 +00:00
|
|
|
|
|
|
|
fn __pow__(lhs: &PyAny, rhs: &PyAny, mod_: Option<u32>) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} ** {:?} (mod: {:?})", lhs, rhs, mod_))
|
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn binary_arithmetic() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
let c = PyCell::new(py, BinaryArithmetic {}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, c, "assert c + c == 'BA + BA'");
|
2020-03-28 08:19:36 +00:00
|
|
|
py_run!(py, c, "assert c.__add__(c) == 'BA + BA'");
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, c, "assert c + 1 == 'BA + 1'");
|
|
|
|
py_run!(py, c, "assert 1 + c == '1 + BA'");
|
|
|
|
py_run!(py, c, "assert c - 1 == 'BA - 1'");
|
|
|
|
py_run!(py, c, "assert 1 - c == '1 - BA'");
|
|
|
|
py_run!(py, c, "assert c * 1 == 'BA * 1'");
|
|
|
|
py_run!(py, c, "assert 1 * c == '1 * BA'");
|
|
|
|
|
|
|
|
py_run!(py, c, "assert c << 1 == 'BA << 1'");
|
|
|
|
py_run!(py, c, "assert 1 << c == '1 << BA'");
|
|
|
|
py_run!(py, c, "assert c >> 1 == 'BA >> 1'");
|
|
|
|
py_run!(py, c, "assert 1 >> c == '1 >> BA'");
|
|
|
|
py_run!(py, c, "assert c & 1 == 'BA & 1'");
|
|
|
|
py_run!(py, c, "assert 1 & c == '1 & BA'");
|
|
|
|
py_run!(py, c, "assert c ^ 1 == 'BA ^ 1'");
|
|
|
|
py_run!(py, c, "assert 1 ^ c == '1 ^ BA'");
|
|
|
|
py_run!(py, c, "assert c | 1 == 'BA | 1'");
|
|
|
|
py_run!(py, c, "assert 1 | c == '1 | BA'");
|
2020-03-29 13:40:20 +00:00
|
|
|
py_run!(py, c, "assert c ** 1 == 'BA ** 1 (mod: None)'");
|
|
|
|
py_run!(py, c, "assert 1 ** c == '1 ** BA (mod: None)'");
|
|
|
|
|
|
|
|
py_run!(py, c, "assert pow(c, 1, 100) == 'BA ** 1 (mod: Some(100))'");
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 03:05:03 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct RhsArithmetic {}
|
|
|
|
|
|
|
|
#[pyproto]
|
|
|
|
impl PyNumberProtocol for RhsArithmetic {
|
|
|
|
fn __radd__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} + RA", other))
|
|
|
|
}
|
2020-03-28 08:19:36 +00:00
|
|
|
|
|
|
|
fn __rsub__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} - RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rmul__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} * RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rlshift__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} << RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rrshift__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} >> RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rand__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} & RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rxor__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} ^ RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __ror__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} | RA", other))
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __rpow__(&self, other: &PyAny, _mod: Option<&'p PyAny>) -> PyResult<String> {
|
2020-03-28 08:19:36 +00:00
|
|
|
Ok(format!("{:?} ** RA", other))
|
|
|
|
}
|
2019-07-22 03:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rhs_arithmetic() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
let c = PyCell::new(py, RhsArithmetic {}).unwrap();
|
2019-07-22 03:05:03 +00:00
|
|
|
py_run!(py, c, "assert c.__radd__(1) == '1 + RA'");
|
2020-03-28 08:19:36 +00:00
|
|
|
py_run!(py, c, "assert 1 + c == '1 + RA'");
|
|
|
|
py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'");
|
|
|
|
py_run!(py, c, "assert 1 - c == '1 - RA'");
|
|
|
|
py_run!(py, c, "assert c.__rmul__(1) == '1 * RA'");
|
|
|
|
py_run!(py, c, "assert 1 * c == '1 * RA'");
|
|
|
|
py_run!(py, c, "assert c.__rlshift__(1) == '1 << RA'");
|
|
|
|
py_run!(py, c, "assert 1 << c == '1 << RA'");
|
|
|
|
py_run!(py, c, "assert c.__rrshift__(1) == '1 >> RA'");
|
|
|
|
py_run!(py, c, "assert 1 >> c == '1 >> RA'");
|
|
|
|
py_run!(py, c, "assert c.__rand__(1) == '1 & RA'");
|
|
|
|
py_run!(py, c, "assert 1 & c == '1 & RA'");
|
|
|
|
py_run!(py, c, "assert c.__rxor__(1) == '1 ^ RA'");
|
|
|
|
py_run!(py, c, "assert 1 ^ c == '1 ^ RA'");
|
|
|
|
py_run!(py, c, "assert c.__ror__(1) == '1 | RA'");
|
|
|
|
py_run!(py, c, "assert 1 | c == '1 | RA'");
|
|
|
|
py_run!(py, c, "assert c.__rpow__(1) == '1 ** RA'");
|
|
|
|
py_run!(py, c, "assert 1 ** c == '1 ** RA'");
|
2019-07-22 03:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct LhsAndRhsArithmetic {}
|
|
|
|
|
|
|
|
#[pyproto]
|
|
|
|
impl PyNumberProtocol for LhsAndRhsArithmetic {
|
|
|
|
fn __radd__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} + RA", other))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __rsub__(&self, other: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} - RA", other))
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:40:20 +00:00
|
|
|
fn __rpow__(&self, other: &PyAny, _mod: Option<&'p PyAny>) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} ** RA", other))
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
fn __add__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} + {:?}", lhs, rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __sub__(lhs: &PyAny, rhs: &PyAny) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} - {:?}", lhs, rhs))
|
|
|
|
}
|
2020-03-29 13:40:20 +00:00
|
|
|
|
|
|
|
fn __pow__(lhs: &PyAny, rhs: &PyAny, _mod: Option<u32>) -> PyResult<String> {
|
|
|
|
Ok(format!("{:?} ** {:?}", lhs, rhs))
|
|
|
|
}
|
2020-03-28 15:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pyproto]
|
|
|
|
impl PyObjectProtocol for LhsAndRhsArithmetic {
|
|
|
|
fn __repr__(&self) -> PyResult<&'static str> {
|
|
|
|
Ok("BA")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lhs_override_rhs() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let c = PyCell::new(py, LhsAndRhsArithmetic {}).unwrap();
|
2020-03-29 13:40:20 +00:00
|
|
|
// Not overrided
|
|
|
|
py_run!(py, c, "assert c.__radd__(1) == '1 + RA'");
|
|
|
|
py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'");
|
|
|
|
py_run!(py, c, "assert c.__rpow__(1) == '1 ** RA'");
|
|
|
|
// Overrided
|
2020-03-28 15:26:11 +00:00
|
|
|
py_run!(py, c, "assert 1 + c == '1 + BA'");
|
|
|
|
py_run!(py, c, "assert 1 - c == '1 - BA'");
|
2020-03-29 13:40:20 +00:00
|
|
|
py_run!(py, c, "assert 1 ** c == '1 ** BA'");
|
2020-03-28 15:26:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-09-02 21:33:45 +00:00
|
|
|
struct RichComparisons {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyObjectProtocol for RichComparisons {
|
|
|
|
fn __repr__(&self) -> PyResult<&'static str> {
|
|
|
|
Ok("RC")
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __richcmp__(&self, other: &PyAny, op: CompareOp) -> PyResult<String> {
|
2018-05-02 18:49:40 +00:00
|
|
|
match op {
|
|
|
|
CompareOp::Lt => Ok(format!("{} < {:?}", self.__repr__().unwrap(), other)),
|
|
|
|
CompareOp::Le => Ok(format!("{} <= {:?}", self.__repr__().unwrap(), other)),
|
|
|
|
CompareOp::Eq => Ok(format!("{} == {:?}", self.__repr__().unwrap(), other)),
|
|
|
|
CompareOp::Ne => Ok(format!("{} != {:?}", self.__repr__().unwrap(), other)),
|
|
|
|
CompareOp::Gt => Ok(format!("{} > {:?}", self.__repr__().unwrap(), other)),
|
2018-06-15 19:21:12 +00:00
|
|
|
CompareOp::Ge => Ok(format!("{} >= {:?}", self.__repr__().unwrap(), other)),
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-11-12 12:59:19 +00:00
|
|
|
struct RichComparisons2 {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyproto]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl PyObjectProtocol for RichComparisons2 {
|
|
|
|
fn __repr__(&self) -> PyResult<&'static str> {
|
|
|
|
Ok("RC2")
|
|
|
|
}
|
|
|
|
|
2019-03-04 04:50:43 +00:00
|
|
|
fn __richcmp__(&self, _other: &PyAny, op: CompareOp) -> PyResult<PyObject> {
|
2018-11-12 12:59:19 +00:00
|
|
|
let gil = GILGuard::acquire();
|
2018-05-02 18:49:40 +00:00
|
|
|
match op {
|
2018-11-12 12:59:19 +00:00
|
|
|
CompareOp::Eq => Ok(true.to_object(gil.python())),
|
|
|
|
CompareOp::Ne => Ok(false.to_object(gil.python())),
|
|
|
|
_ => Ok(gil.python().NotImplemented()),
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rich_comparisons() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
let c = PyCell::new(py, RichComparisons {}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, c, "assert (c < c) == 'RC < RC'");
|
|
|
|
py_run!(py, c, "assert (c < 1) == 'RC < 1'");
|
|
|
|
py_run!(py, c, "assert (1 < c) == 'RC > 1'");
|
|
|
|
py_run!(py, c, "assert (c <= c) == 'RC <= RC'");
|
|
|
|
py_run!(py, c, "assert (c <= 1) == 'RC <= 1'");
|
|
|
|
py_run!(py, c, "assert (1 <= c) == 'RC >= 1'");
|
|
|
|
py_run!(py, c, "assert (c == c) == 'RC == RC'");
|
|
|
|
py_run!(py, c, "assert (c == 1) == 'RC == 1'");
|
|
|
|
py_run!(py, c, "assert (1 == c) == 'RC == 1'");
|
|
|
|
py_run!(py, c, "assert (c != c) == 'RC != RC'");
|
|
|
|
py_run!(py, c, "assert (c != 1) == 'RC != 1'");
|
|
|
|
py_run!(py, c, "assert (1 != c) == 'RC != 1'");
|
|
|
|
py_run!(py, c, "assert (c > c) == 'RC > RC'");
|
|
|
|
py_run!(py, c, "assert (c > 1) == 'RC > 1'");
|
|
|
|
py_run!(py, c, "assert (1 > c) == 'RC < 1'");
|
|
|
|
py_run!(py, c, "assert (c >= c) == 'RC >= RC'");
|
|
|
|
py_run!(py, c, "assert (c >= 1) == 'RC >= 1'");
|
|
|
|
py_run!(py, c, "assert (1 >= c) == 'RC <= 1'");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rich_comparisons_python_3_type_error() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-28 15:26:11 +00:00
|
|
|
let c2 = PyCell::new(py, RichComparisons2 {}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
py_expect_exception!(py, c2, "c2 < c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 < 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 < c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 <= c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 <= 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 <= c2", TypeError);
|
|
|
|
py_run!(py, c2, "assert (c2 == c2) == True");
|
|
|
|
py_run!(py, c2, "assert (c2 == 1) == True");
|
|
|
|
py_run!(py, c2, "assert (1 == c2) == True");
|
|
|
|
py_run!(py, c2, "assert (c2 != c2) == False");
|
|
|
|
py_run!(py, c2, "assert (c2 != 1) == False");
|
|
|
|
py_run!(py, c2, "assert (1 != c2) == False");
|
|
|
|
py_expect_exception!(py, c2, "c2 > c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 > 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 > c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 >= c2", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "c2 >= 1", TypeError);
|
|
|
|
py_expect_exception!(py, c2, "1 >= c2", TypeError);
|
2018-06-15 19:21:12 +00:00
|
|
|
}
|