File tree Expand file tree Collapse file tree 2 files changed +24
-8
lines changed Expand file tree Collapse file tree 2 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -1811,19 +1811,20 @@ class XStrField(StrField):
18111811
18121812 def i2repr (self , pkt , x ):
18131813 # type: (Optional[Packet], bytes) -> str
1814- if x is None :
1815- return repr ( x )
1816- return bytes_hex ( x ). decode ( )
1814+ if isinstance ( x , bytes ) :
1815+ return bytes_hex ( x ). decode ( )
1816+ return super ( XStrField , self ). i2repr ( pkt , x )
18171817
18181818
18191819class _XStrLenField :
18201820 def i2repr (self , pkt , x ):
18211821 # type: (Optional[Packet], bytes) -> str
1822- if not x :
1823- return repr (x )
1824- return bytes_hex (
1825- x [:(self .length_from or (lambda x : 0 ))(pkt )] # type: ignore
1826- ).decode ()
1822+ if isinstance (x , bytes ):
1823+ return bytes_hex (
1824+ x [:(self .length_from or (lambda x : 0 ))(pkt )] # type: ignore
1825+ ).decode ()
1826+ # cannot use super() since _XStrLenField does not inherit from Field
1827+ return Field .i2repr (self , pkt , x )
18271828
18281829
18291830class XStrLenField (_XStrLenField , StrLenField ):
Original file line number Diff line number Diff line change @@ -2102,3 +2102,18 @@ assert isinstance(p.packet.short1, RandShort)
21022102assert isinstance(p.packet.byte, RandByte)
21032103assert isinstance(p.packet.long, RandLong)
21042104assert isinstance(p.short2, RandShort)
2105+
2106+ ############
2107+ ############
2108+ + XStr(*)Field tests
2109+
2110+ = i2repr
2111+ ~ field xstrfield
2112+
2113+ from collections import namedtuple
2114+ MockPacket = namedtuple('MockPacket', ['type'])
2115+
2116+ mp = MockPacket(0)
2117+ f = XStrField('test', None)
2118+ x = f.i2repr(mp, RandBin())
2119+ assert x == '<RandBin>'
You can’t perform that action at this time.
0 commit comments