Merge pull request #1621 from pythonnet/fix-enum-codec

Fix enum codec
This commit is contained in:
Benedikt Reinartz 2022-05-04 09:38:14 +02:00 коммит произвёл GitHub
Родитель 37f12359e0 7d6e27a23e
Коммит ac75e0ccc3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 60 добавлений и 16 удалений

Просмотреть файл

@ -55,11 +55,11 @@ namespace Python.Runtime.Codecs
try
{
return new PyInt((long)value);
return new PyInt(Convert.ToInt64(value));
}
catch (InvalidCastException)
catch (OverflowException)
{
return new PyInt((ulong)value);
return new PyInt(Convert.ToUInt64(value));
}
}

Просмотреть файл

@ -1,36 +1,50 @@
# -*- coding: utf-8 -*-
"""Test conversions using codecs from client python code"""
import clr
import System
import pytest
import Python.Runtime
import Python.Test as Test
from Python.Test import ListConversionTester, ListMember, CodecResetter
class int_iterable():
@pytest.fixture(autouse=True)
def reset():
yield
CodecResetter.Reset()
class int_iterable:
def __init__(self):
self.counter = 0
def __iter__(self):
return self
def __next__(self):
if self.counter == 3:
raise StopIteration
self.counter = self.counter + 1
return self.counter
class obj_iterable():
class obj_iterable:
def __init__(self):
self.counter = 0
def __iter__(self):
return self
def __next__(self):
if self.counter == 3:
raise StopIteration
self.counter = self.counter + 1
return ListMember(self.counter, "Number " + str(self.counter))
def test_iterable():
"""Test that a python iterable can be passed into a function that takes an IEnumerable<object>"""
"""Test that a python iterable can be passed into a function that takes an
IEnumerable<object>"""
# Python.Runtime.Codecs.ListDecoder.Register()
# Python.Runtime.Codecs.SequenceDecoder.Register()
@ -43,7 +57,6 @@ def test_iterable():
iterable2 = obj_iterable()
assert 3 == ob.GetLength2(iterable2)
CodecResetter.Reset()
def test_sequence():
Python.Runtime.Codecs.SequenceDecoder.Register()
@ -55,7 +68,6 @@ def test_sequence():
tup2 = (ListMember(1, "one"), ListMember(2, "two"), ListMember(3, "three"))
assert 3 == ob.GetLength(tup2)
CodecResetter.Reset()
def test_list():
Python.Runtime.Codecs.SequenceDecoder.Register()
@ -67,4 +79,36 @@ def test_list():
l2 = [ListMember(1, "one"), ListMember(2, "two"), ListMember(3, "three")]
assert 3 == ob.GetLength(l2)
CodecResetter.Reset()
def test_enum():
Python.Runtime.PyObjectConversions.RegisterEncoder(
Python.Runtime.Codecs.EnumPyIntCodec.Instance
)
assert Test.ByteEnum.Zero == 0
assert Test.ByteEnum.One == 1
assert Test.ByteEnum.Two == 2
assert Test.SByteEnum.Zero == 0
assert Test.SByteEnum.One == 1
assert Test.SByteEnum.Two == 2
assert Test.ShortEnum.Zero == 0
assert Test.ShortEnum.One == 1
assert Test.ShortEnum.Two == 2
assert Test.UShortEnum.Zero == 0
assert Test.UShortEnum.One == 1
assert Test.UShortEnum.Two == 2
assert Test.IntEnum.Zero == 0
assert Test.IntEnum.One == 1
assert Test.IntEnum.Two == 2
assert Test.UIntEnum.Zero == 0
assert Test.UIntEnum.One == 1
assert Test.UIntEnum.Two == 2
assert Test.LongEnum.Zero == 0
assert Test.LongEnum.One == 1
assert Test.LongEnum.Two == 2
assert Test.ULongEnum.Zero == 0
assert Test.ULongEnum.One == 1
assert Test.ULongEnum.Two == 2
assert Test.LongEnum.Max == 9223372036854775807
assert Test.LongEnum.Min == -9223372036854775808
assert int(Test.ULongEnum.Max) == 18446744073709551615