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

Commitac75e0c

Browse files
authored
Merge pull request#1621 from pythonnet/fix-enum-codec
Fix enum codec
2 parents37f1235 +7d6e27a commitac75e0c

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

‎src/runtime/Codecs/EnumPyIntCodec.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public bool TryDecode<T>(PyObject pyObj, out T? value)
5555

5656
try
5757
{
58-
returnnewPyInt((long)value);
58+
returnnewPyInt(Convert.ToInt64(value));
5959
}
60-
catch(InvalidCastException)
60+
catch(OverflowException)
6161
{
62-
returnnewPyInt((ulong)value);
62+
returnnewPyInt(Convert.ToUInt64(value));
6363
}
6464
}
6565

‎tests/test_codec.py

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
11
# -*- coding: utf-8 -*-
22

33
"""Test conversions using codecs from client python code"""
4-
importclr
5-
importSystem
4+
65
importpytest
76
importPython.Runtime
7+
importPython.TestasTest
88
fromPython.TestimportListConversionTester,ListMember,CodecResetter
99

10-
classint_iterable():
10+
11+
@pytest.fixture(autouse=True)
12+
defreset():
13+
yield
14+
CodecResetter.Reset()
15+
16+
17+
classint_iterable:
1118
def__init__(self):
1219
self.counter=0
20+
1321
def__iter__(self):
1422
returnself
23+
1524
def__next__(self):
1625
ifself.counter==3:
1726
raiseStopIteration
1827
self.counter=self.counter+1
1928
returnself.counter
2029

21-
classobj_iterable():
30+
31+
classobj_iterable:
2232
def__init__(self):
2333
self.counter=0
34+
2435
def__iter__(self):
2536
returnself
37+
2638
def__next__(self):
2739
ifself.counter==3:
2840
raiseStopIteration
2941
self.counter=self.counter+1
3042
returnListMember(self.counter,"Number "+str(self.counter))
3143

44+
3245
deftest_iterable():
33-
"""Test that a python iterable can be passed into a function that takes an IEnumerable<object>"""
46+
"""Test that a python iterable can be passed into a function that takes an
47+
IEnumerable<object>"""
3448

35-
#Python.Runtime.Codecs.ListDecoder.Register()
36-
#Python.Runtime.Codecs.SequenceDecoder.Register()
49+
#Python.Runtime.Codecs.ListDecoder.Register()
50+
#Python.Runtime.Codecs.SequenceDecoder.Register()
3751
Python.Runtime.Codecs.IterableDecoder.Register()
3852
ob=ListConversionTester()
3953

@@ -43,28 +57,58 @@ def test_iterable():
4357
iterable2=obj_iterable()
4458
assert3==ob.GetLength2(iterable2)
4559

46-
CodecResetter.Reset()
4760

4861
deftest_sequence():
4962
Python.Runtime.Codecs.SequenceDecoder.Register()
5063
ob=ListConversionTester()
5164

52-
tup= (1,2,3)
65+
tup= (1,2,3)
5366
assert3==ob.GetLength(tup)
5467

5568
tup2= (ListMember(1,"one"),ListMember(2,"two"),ListMember(3,"three"))
5669
assert3==ob.GetLength(tup2)
5770

58-
CodecResetter.Reset()
5971

6072
deftest_list():
6173
Python.Runtime.Codecs.SequenceDecoder.Register()
6274
ob=ListConversionTester()
6375

64-
l= [1,2,3]
76+
l= [1,2,3]
6577
assert3==ob.GetLength(l)
6678

6779
l2= [ListMember(1,"one"),ListMember(2,"two"),ListMember(3,"three")]
6880
assert3==ob.GetLength(l2)
6981

70-
CodecResetter.Reset()
82+
83+
deftest_enum():
84+
Python.Runtime.PyObjectConversions.RegisterEncoder(
85+
Python.Runtime.Codecs.EnumPyIntCodec.Instance
86+
)
87+
88+
assertTest.ByteEnum.Zero==0
89+
assertTest.ByteEnum.One==1
90+
assertTest.ByteEnum.Two==2
91+
assertTest.SByteEnum.Zero==0
92+
assertTest.SByteEnum.One==1
93+
assertTest.SByteEnum.Two==2
94+
assertTest.ShortEnum.Zero==0
95+
assertTest.ShortEnum.One==1
96+
assertTest.ShortEnum.Two==2
97+
assertTest.UShortEnum.Zero==0
98+
assertTest.UShortEnum.One==1
99+
assertTest.UShortEnum.Two==2
100+
assertTest.IntEnum.Zero==0
101+
assertTest.IntEnum.One==1
102+
assertTest.IntEnum.Two==2
103+
assertTest.UIntEnum.Zero==0
104+
assertTest.UIntEnum.One==1
105+
assertTest.UIntEnum.Two==2
106+
assertTest.LongEnum.Zero==0
107+
assertTest.LongEnum.One==1
108+
assertTest.LongEnum.Two==2
109+
assertTest.ULongEnum.Zero==0
110+
assertTest.ULongEnum.One==1
111+
assertTest.ULongEnum.Two==2
112+
assertTest.LongEnum.Max==9223372036854775807
113+
assertTest.LongEnum.Min==-9223372036854775808
114+
assertint(Test.ULongEnum.Max)==18446744073709551615

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp