Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 8b9240d

Browse files
committed
Allow casting from UBInt() to native int()
1 parent 6303886 commit 8b9240d

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

pyof/foundation/base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333

3434
# This will determine the order on sphinx documentation.
3535
__all__ = ('GenericStruct', 'GenericMessage', 'GenericType', 'GenericBitMask',
36-
'MetaStruct', 'MetaBitMask')
36+
'MetaStruct', 'MetaBitMask', 'UBIntBase')
3737

3838
# Classes
3939

4040

4141
class GenericType:
4242
"""Foundation class for all custom attributes.
4343
44-
Base class for :class:`~.UBInt8`, :class:`~.Char`
44+
Base class for :class:`~.UBIntBase`, :class:`~.Char`
4545
and others.
4646
"""
4747

@@ -259,6 +259,15 @@ def is_bitmask(self):
259259
return self._value and issubclass(type(self._value), GenericBitMask)
260260

261261

262+
class UBIntBase(GenericType):
263+
"""Base class for UBInt{8,16,32,64,128}."""
264+
def __int__(self):
265+
"""Allow converting an UBInt() back to an int()."""
266+
# Skip GenericType's checking if this is an Enum ou BitMask
267+
# (because it won't be), and convert directly from _value
268+
return int(self._value)
269+
270+
262271
class MetaStruct(type):
263272
"""MetaClass that dynamically handles openflow version of class attributes.
264273

pyof/foundation/basic_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Local source tree imports
88
from pyof.foundation import exceptions
9-
from pyof.foundation.base import GenericStruct, GenericType
9+
from pyof.foundation.base import GenericStruct, GenericType, UBIntBase
1010

1111
__all__ = ('BinaryData', 'Char', 'ConstantTypeList', 'FixedTypeList',
1212
'IPAddress', 'DPID', 'HWAddress', 'Pad', 'UBInt8', 'UBInt16',
@@ -77,7 +77,7 @@ def __deepcopy__(self, memo):
7777
return Pad(length=self._length)
7878

7979

80-
class UBInt8(GenericType):
80+
class UBInt8(UBIntBase):
8181
"""Format character for an Unsigned Char.
8282
8383
Class for an 8-bit (1-byte) Unsigned Integer.
@@ -86,7 +86,7 @@ class UBInt8(GenericType):
8686
_fmt = "!B"
8787

8888

89-
class UBInt16(GenericType):
89+
class UBInt16(UBIntBase):
9090
"""Format character for an Unsigned Short.
9191
9292
Class for an 16-bit (2-byte) Unsigned Integer.
@@ -95,7 +95,7 @@ class UBInt16(GenericType):
9595
_fmt = "!H"
9696

9797

98-
class UBInt32(GenericType):
98+
class UBInt32(UBIntBase):
9999
"""Format character for an Unsigned Int.
100100
101101
Class for an 32-bit (4-byte) Unsigned Integer.
@@ -104,7 +104,7 @@ class UBInt32(GenericType):
104104
_fmt = "!I"
105105

106106

107-
class UBInt64(GenericType):
107+
class UBInt64(UBIntBase):
108108
"""Format character for an Unsigned Long Long.
109109
110110
Class for an 64-bit (8-byte) Unsigned Integer.
@@ -113,7 +113,7 @@ class UBInt64(GenericType):
113113
_fmt = "!Q"
114114

115115

116-
class UBInt128(GenericType):
116+
class UBInt128(UBIntBase):
117117
"""Format character for an Unsigned Long Long.
118118
119119
Class for an 128-bit (16-byte) Unsigned Integer.

tests/unit/test_foundation/test_basic_types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def test_pack_error(self):
3232
u = basic_types.UBInt8(256)
3333
self.assertRaises(PackException, u.pack)
3434

35+
def test_cast_to_int(self):
36+
self.assertEqual(255, int(self.ubint8))
37+
3538

3639
class TestUBInt16(unittest.TestCase):
3740
"""Test of UBInt16 BasicType."""

0 commit comments

Comments
 (0)