Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf738505

Browse files
committed
IComparable and IEquatable implementations for PyInt, PyFloat, and PyString for primitive .NET types
1 parent9d18a24 commitf738505

File tree

10 files changed

+331
-4
lines changed

10 files changed

+331
-4
lines changed

‎CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1111

1212
- Added`ToPythonAs<T>()` extension method to allow for explicit conversion using a specific type. ([#2311][i2311])
1313

14+
- Added`IComparable` and`IEquatable` implementations to`PyInt`,`PyFloat`, and`PyString`
15+
to compare with primitive .NET types like`long`.
16+
1417
###Changed
1518

1619
###Fixed

‎src/embed_tests/TestPyFloat.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,32 @@ public void AsFloatBad()
126126
StringAssert.StartsWith("could not convert string to float",ex.Message);
127127
Assert.IsNull(a);
128128
}
129+
130+
[Test]
131+
publicvoidCompareTo()
132+
{
133+
varv=newPyFloat(42);
134+
135+
Assert.AreEqual(0,v.CompareTo(42f));
136+
Assert.AreEqual(0,v.CompareTo(42d));
137+
138+
Assert.AreEqual(1,v.CompareTo(41f));
139+
Assert.AreEqual(1,v.CompareTo(41d));
140+
141+
Assert.AreEqual(-1,v.CompareTo(43f));
142+
Assert.AreEqual(-1,v.CompareTo(43d));
143+
}
144+
145+
[Test]
146+
publicvoidEquals()
147+
{
148+
varv=newPyFloat(42);
149+
150+
Assert.IsTrue(v.Equals(42f));
151+
Assert.IsTrue(v.Equals(42d));
152+
153+
Assert.IsFalse(v.Equals(41f));
154+
Assert.IsFalse(v.Equals(41d));
155+
}
129156
}
130157
}

‎src/embed_tests/TestPyInt.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,76 @@ public void ToBigInteger()
210210
CollectionAssert.AreEqual(expected,actual);
211211
}
212212

213+
[Test]
214+
publicvoidCompareTo()
215+
{
216+
varv=newPyInt(42);
217+
218+
#region Signed
219+
Assert.AreEqual(0,v.CompareTo(42L));
220+
Assert.AreEqual(0,v.CompareTo(42));
221+
Assert.AreEqual(0,v.CompareTo((short)42));
222+
Assert.AreEqual(0,v.CompareTo((sbyte)42));
223+
224+
Assert.AreEqual(1,v.CompareTo(41L));
225+
Assert.AreEqual(1,v.CompareTo(41));
226+
Assert.AreEqual(1,v.CompareTo((short)41));
227+
Assert.AreEqual(1,v.CompareTo((sbyte)41));
228+
229+
Assert.AreEqual(-1,v.CompareTo(43L));
230+
Assert.AreEqual(-1,v.CompareTo(43));
231+
Assert.AreEqual(-1,v.CompareTo((short)43));
232+
Assert.AreEqual(-1,v.CompareTo((sbyte)43));
233+
#endregion Signed
234+
235+
#region Unsigned
236+
Assert.AreEqual(0,v.CompareTo(42UL));
237+
Assert.AreEqual(0,v.CompareTo(42U));
238+
Assert.AreEqual(0,v.CompareTo((ushort)42));
239+
Assert.AreEqual(0,v.CompareTo((byte)42));
240+
241+
Assert.AreEqual(1,v.CompareTo(41UL));
242+
Assert.AreEqual(1,v.CompareTo(41U));
243+
Assert.AreEqual(1,v.CompareTo((ushort)41));
244+
Assert.AreEqual(1,v.CompareTo((byte)41));
245+
246+
Assert.AreEqual(-1,v.CompareTo(43UL));
247+
Assert.AreEqual(-1,v.CompareTo(43U));
248+
Assert.AreEqual(-1,v.CompareTo((ushort)43));
249+
Assert.AreEqual(-1,v.CompareTo((byte)43));
250+
#endregion Unsigned
251+
}
252+
253+
[Test]
254+
publicvoidEquals()
255+
{
256+
varv=newPyInt(42);
257+
258+
#region Signed
259+
Assert.True(v.Equals(42L));
260+
Assert.True(v.Equals(42));
261+
Assert.True(v.Equals((short)42));
262+
Assert.True(v.Equals((sbyte)42));
263+
264+
Assert.False(v.Equals(41L));
265+
Assert.False(v.Equals(41));
266+
Assert.False(v.Equals((short)41));
267+
Assert.False(v.Equals((sbyte)41));
268+
#endregion Signed
269+
270+
#region Unsigned
271+
Assert.True(v.Equals(42UL));
272+
Assert.True(v.Equals(42U));
273+
Assert.True(v.Equals((ushort)42));
274+
Assert.True(v.Equals((byte)42));
275+
276+
Assert.False(v.Equals(41UL));
277+
Assert.False(v.Equals(41U));
278+
Assert.False(v.Equals((ushort)41));
279+
Assert.False(v.Equals((byte)41));
280+
#endregion Unsigned
281+
}
282+
213283
[Test]
214284
publicvoidToBigIntegerLarge()
215285
{

‎src/embed_tests/TestPyString.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,24 @@ public void TestUnicodeSurrogate()
112112
Assert.AreEqual(4,actual.Length());
113113
Assert.AreEqual(expected,actual.ToString());
114114
}
115+
116+
[Test]
117+
publicvoidCompareTo()
118+
{
119+
vara=newPyString("foo");
120+
121+
Assert.AreEqual(0,a.CompareTo("foo"));
122+
Assert.AreEqual("foo".CompareTo("bar"),a.CompareTo("bar"));
123+
Assert.AreEqual("foo".CompareTo("foz"),a.CompareTo("foz"));
124+
}
125+
126+
[Test]
127+
publicvoidEquals()
128+
{
129+
vara=newPyString("foo");
130+
131+
Assert.True(a.Equals("foo"));
132+
Assert.False(a.Equals("bar"));
133+
}
115134
}
116135
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
usingSystem;
2+
3+
namespacePython.Runtime;
4+
5+
partialclassPyFloat:IComparable<double>,IComparable<float>
6+
,IEquatable<double>,IEquatable<float>
7+
,IComparable<PyFloat?>,IEquatable<PyFloat?>
8+
{
9+
publicoverrideboolEquals(objecto)
10+
{
11+
usingvar_=Py.GIL();
12+
returnoswitch
13+
{
14+
doublef64=>this.Equals(f64),
15+
floatf32=>this.Equals(f32),
16+
_=>base.Equals(o),
17+
};
18+
}
19+
20+
publicintCompareTo(doubleother)=>this.ToDouble().CompareTo(other);
21+
22+
publicintCompareTo(floatother)=>this.ToDouble().CompareTo(other);
23+
24+
publicboolEquals(doubleother)=>this.ToDouble().Equals(other);
25+
26+
publicboolEquals(floatother)=>this.ToDouble().Equals(other);
27+
28+
publicintCompareTo(PyFloat?other)
29+
{
30+
returnotherisnull?1:this.CompareTo(other.BorrowNullable());
31+
}
32+
33+
publicboolEquals(PyFloat?other)=>base.Equals(other);
34+
}

‎src/runtime/PythonTypes/PyFloat.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Python.Runtime
88
/// PY3: https://docs.python.org/3/c-api/float.html
99
/// for details.
1010
/// </summary>
11-
publicclassPyFloat:PyNumber
11+
publicpartialclassPyFloat:PyNumber
1212
{
1313
internalPyFloat(inStolenReferenceptr):base(ptr)
1414
{
@@ -100,6 +100,8 @@ public static PyFloat AsFloat(PyObject value)
100100
returnnewPyFloat(op.Steal());
101101
}
102102

103+
publicdoubleToDouble()=>Runtime.PyFloat_AsDouble(obj);
104+
103105
publicoverrideTypeCodeGetTypeCode()=>TypeCode.Double;
104106
}
105107
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
usingSystem;
2+
3+
namespacePython.Runtime;
4+
5+
partialclassPyInt:IComparable<long>,IComparable<int>,IComparable<sbyte>,IComparable<short>
6+
,IComparable<ulong>,IComparable<uint>,IComparable<ushort>,IComparable<byte>
7+
,IEquatable<long>,IEquatable<int>,IEquatable<short>,IEquatable<sbyte>
8+
,IEquatable<ulong>,IEquatable<uint>,IEquatable<ushort>,IEquatable<byte>
9+
,IComparable<PyInt?>,IEquatable<PyInt?>
10+
{
11+
publicoverrideboolEquals(objecto)
12+
{
13+
usingvar_=Py.GIL();
14+
returnoswitch
15+
{
16+
longi64=>this.Equals(i64),
17+
inti32=>this.Equals(i32),
18+
shorti16=>this.Equals(i16),
19+
sbytei8=>this.Equals(i8),
20+
21+
ulongu64=>this.Equals(u64),
22+
uintu32=>this.Equals(u32),
23+
ushortu16=>this.Equals(u16),
24+
byteu8=>this.Equals(u8),
25+
26+
_=>base.Equals(o),
27+
};
28+
}
29+
30+
#region Signed
31+
publicintCompareTo(longother)
32+
{
33+
usingvarpyOther=Runtime.PyInt_FromInt64(other);
34+
returnthis.CompareTo(pyOther.BorrowOrThrow());
35+
}
36+
37+
publicintCompareTo(intother)
38+
{
39+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
40+
returnthis.CompareTo(pyOther.BorrowOrThrow());
41+
}
42+
43+
publicintCompareTo(shortother)
44+
{
45+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
46+
returnthis.CompareTo(pyOther.BorrowOrThrow());
47+
}
48+
49+
publicintCompareTo(sbyteother)
50+
{
51+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
52+
returnthis.CompareTo(pyOther.BorrowOrThrow());
53+
}
54+
55+
publicboolEquals(longother)
56+
{
57+
usingvarpyOther=Runtime.PyInt_FromInt64(other);
58+
returnthis.Equals(pyOther.BorrowOrThrow());
59+
}
60+
61+
publicboolEquals(intother)
62+
{
63+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
64+
returnthis.Equals(pyOther.BorrowOrThrow());
65+
}
66+
67+
publicboolEquals(shortother)
68+
{
69+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
70+
returnthis.Equals(pyOther.BorrowOrThrow());
71+
}
72+
73+
publicboolEquals(sbyteother)
74+
{
75+
usingvarpyOther=Runtime.PyInt_FromInt32(other);
76+
returnthis.Equals(pyOther.BorrowOrThrow());
77+
}
78+
#endregion Signed
79+
80+
#region Unsigned
81+
publicintCompareTo(ulongother)
82+
{
83+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
84+
returnthis.CompareTo(pyOther.BorrowOrThrow());
85+
}
86+
87+
publicintCompareTo(uintother)
88+
{
89+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
90+
returnthis.CompareTo(pyOther.BorrowOrThrow());
91+
}
92+
93+
publicintCompareTo(ushortother)
94+
{
95+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
96+
returnthis.CompareTo(pyOther.BorrowOrThrow());
97+
}
98+
99+
publicintCompareTo(byteother)
100+
{
101+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
102+
returnthis.CompareTo(pyOther.BorrowOrThrow());
103+
}
104+
105+
publicboolEquals(ulongother)
106+
{
107+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
108+
returnthis.Equals(pyOther.BorrowOrThrow());
109+
}
110+
111+
publicboolEquals(uintother)
112+
{
113+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
114+
returnthis.Equals(pyOther.BorrowOrThrow());
115+
}
116+
117+
publicboolEquals(ushortother)
118+
{
119+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
120+
returnthis.Equals(pyOther.BorrowOrThrow());
121+
}
122+
123+
publicboolEquals(byteother)
124+
{
125+
usingvarpyOther=Runtime.PyLong_FromUnsignedLongLong(other);
126+
returnthis.Equals(pyOther.BorrowOrThrow());
127+
}
128+
#endregion Unsigned
129+
130+
publicintCompareTo(PyInt?other)
131+
{
132+
returnotherisnull?1:this.CompareTo(other.BorrowNullable());
133+
}
134+
135+
publicboolEquals(PyInt?other)=>base.Equals(other);
136+
}

‎src/runtime/PythonTypes/PyInt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Python.Runtime
99
/// Represents a Python integer object.
1010
/// See the documentation at https://docs.python.org/3/c-api/long.html
1111
/// </summary>
12-
publicclassPyInt:PyNumber,IFormattable
12+
publicpartialclassPyInt:PyNumber,IFormattable
1313
{
1414
internalPyInt(inStolenReferenceptr):base(ptr)
1515
{

‎src/runtime/PythonTypes/PyObject.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,23 @@ public long Refcount
11361136
}
11371137
}
11381138

1139+
internalintCompareTo(BorrowedReferenceother)
1140+
{
1141+
intgreater=Runtime.PyObject_RichCompareBool(this.Reference,other,Runtime.Py_GT);
1142+
Debug.Assert(greater!=-1);
1143+
if(greater>0)
1144+
return1;
1145+
intless=Runtime.PyObject_RichCompareBool(this.Reference,other,Runtime.Py_LT);
1146+
Debug.Assert(less!=-1);
1147+
returnless>0?-1:0;
1148+
}
1149+
1150+
internalboolEquals(BorrowedReferenceother)
1151+
{
1152+
intequal=Runtime.PyObject_RichCompareBool(this.Reference,other,Runtime.Py_EQ);
1153+
Debug.Assert(equal!=-1);
1154+
returnequal>0;
1155+
}
11391156

11401157
publicoverrideboolTryGetMember(GetMemberBinderbinder,outobject?result)
11411158
{
@@ -1325,7 +1342,7 @@ private bool TryCompare(PyObject arg, int op, out object @out)
13251342
}
13261343
returntrue;
13271344
}
1328-
1345+
13291346
publicoverrideboolTryBinaryOperation(BinaryOperationBinderbinder,objectarg,outobject?result)
13301347
{
13311348
usingvar_=Py.GIL();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp