Skip to content

Commit ac75e0c

Browse files
authored
Merge pull request #1621 from pythonnet/fix-enum-codec
Fix enum codec
2 parents 37f1235 + 7d6e27a commit ac75e0c

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

src/runtime/Codecs/EnumPyIntCodec.cs

+3-3
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-
return new PyInt((long)value);
58+
return new PyInt(Convert.ToInt64(value));
5959
}
60-
catch (InvalidCastException)
60+
catch (OverflowException)
6161
{
62-
return new PyInt((ulong)value);
62+
return new PyInt(Convert.ToUInt64(value));
6363
}
6464
}
6565

tests/test_codec.py

+56-12
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-
import clr
5-
import System
4+
65
import pytest
76
import Python.Runtime
7+
import Python.Test as Test
88
from Python.Test import ListConversionTester, ListMember, CodecResetter
99

10-
class int_iterable():
10+
11+
@pytest.fixture(autouse=True)
12+
def reset():
13+
yield
14+
CodecResetter.Reset()
15+
16+
17+
class int_iterable:
1118
def __init__(self):
1219
self.counter = 0
20+
1321
def __iter__(self):
1422
return self
23+
1524
def __next__(self):
1625
if self.counter == 3:
1726
raise StopIteration
1827
self.counter = self.counter + 1
1928
return self.counter
2029

21-
class obj_iterable():
30+
31+
class obj_iterable:
2232
def __init__(self):
2333
self.counter = 0
34+
2435
def __iter__(self):
2536
return self
37+
2638
def __next__(self):
2739
if self.counter == 3:
2840
raise StopIteration
2941
self.counter = self.counter + 1
3042
return ListMember(self.counter, "Number " + str(self.counter))
3143

44+
3245
def test_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
assert 3 == ob.GetLength2(iterable2)
4559

46-
CodecResetter.Reset()
4760

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

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

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

58-
CodecResetter.Reset()
5971

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

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

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

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

0 commit comments

Comments
 (0)