Skip to content

Commit b95fdda

Browse files
committed
Correct Python2 b'..' comparisons
1 parent 5883405 commit b95fdda

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

automata.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,12 @@ def __repr__( self ):
279279
return '<%s>' % ( self )
280280

281281
def __call__( self, machine=None, source=None, path=None, data=None ):
282-
return self.execute(
283-
truth=self.predicate( machine=machine, source=source, path=path, data=data ),
284-
machine=machine, source=source, path=path, data=data )
282+
truth = self.predicate( machine=machine, source=source, path=path, data=data )
283+
return self.execute( truth, machine=machine, source=source, path=path, data=data )
285284

286285
def execute( self, truth, machine=None, source=None, path=None, data=None ):
287286
target = self.state if truth else None
288-
#log.debug( "%s %-13.13s -> %10s w/ data: %r", machine.name_centered(), self, target, data )
287+
#log.debug( "%s %-13.13s -> %10s w/ truth: %r data: %r", machine.name_centered(), self, target, truth, data )
289288
return target
290289

291290

@@ -777,7 +776,7 @@ def transition( self, source, machine=None, path=None, data=None, ending=None ):
777776
"""
778777
limited = ending is not None and source.sent >= ending
779778
while not self.terminal or self.greedy:
780-
inp = None if limited else source.peek()# raise TypeError to force stoppage
779+
inp = None if limited else source.peek() # raise TypeError to force stoppage
781780
try:
782781
choice = self.__getitem__( inp )
783782
except KeyError:
@@ -792,25 +791,25 @@ def transition( self, source, machine=None, path=None, data=None, ending=None ):
792791
yield machine,None # 0+ non-transitions...
793792
continue
794793
break # No other transition possible; done
794+
if type( choice ) is not list:
795+
choice = [ choice ]
795796

796797
# Found the transition or choice list (could be a state, a decide or decide, ...,
797798
# decide[, state]). Evaluate each target state/decide instance, 'til we find a
798799
# state/None. Even decides could end up yielding None, if all decide evaluate to None,
799800
# and no non-None default state .
800-
for potential in ( choice if type( choice ) is list else [ choice ] ):
801+
for potential in choice:
801802
if potential is None or isinstance( potential, state ):
802803
target = potential
803804
break
804805
try:
805-
#log.debug( "%s <selfdecide> on %s", self.name_centered(), choice )
806+
#log.debug( "%s <selfdecide> on %s", self.name_centered(), potential )
806807
target = potential( # this decision is made in our parent machine's context
807808
source=source, machine=machine, path=path, data=data )
808809
except Exception as exc:
809-
log.warning( "%s <selfdecide> on %s failed: %r", self.name_centered(),
810-
potential, exc )
810+
log.warning( "%s <selfdecide> on %s failed: %r", self.name_centered(), potential, exc )
811811
raise
812-
#log.debug( "%s <selfdecide> on %s == %s", self.name_centered(),
813-
# potential, target )
812+
#log.debug( "%s <selfdecide> on %s == %s", self.name_centered(), potential, target )
814813
if target:
815814
break
816815

server/enip/parser.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,36 +1246,36 @@ def __init__( self, name=None, **kwds ):
12461246
# carry the same server == 0xD2 and status coded.
12471247
def is_uerr( path=None, data=None, source=None, **kwds ):
12481248
log.isEnabledFor( logging.INFO ) and log.info(
1249-
"%s -- checking data[%r] (kwds %r) for unconnected_send error (%5s): %s",
1250-
self, path, kwds, data[path+'..length'] <= 6, enip_format( data ),
1249+
"{} -- checking data[{!r}] (kwds {!r}) for unconnected_send error ({:5}) ({}, next {!r}): {}".format(
1250+
self, path, kwds, data[path+'..length'] <= 6, source, source.peek(), enip_format( data ) )
12511251
)
1252-
if data[path+'..length'] > 6:
1253-
return False
1254-
# Might be a Unconnected Send error, perhaps with a remaining path size; peek at the
1255-
# error code and extended status; if the code is < 0x10 and there is NO extended status,
1256-
# then it must *not* be a Read Tag Fragmented error code, as ALL of its <0x10 status
1257-
# codes are required to have an extended status (which may be 0x0000, but must be
1258-
# there). HOWEVER, there are many (many) devices that DO NOT comply with the subtleties
1259-
# of the ODVA EtherNet/IP CIP spec -- and simply return eg. a status = 0x05 WITHOUT a
1260-
# 0x0000 extended status code. This makes it *impossible* to determine whether or not a
1261-
# Read Tag Fragmented response status belongs to the encapsulating Unconnected Send, or
1262-
# not. The only possible option is for the CLIENT to avoid sending Read Tag Fragmented
1263-
# requests in an Unconnected Send, or use a Multiple Service packet to encapsulate them.
1264-
svc = next( source )
1265-
pad = next( source )
1266-
sts = next( source )
1267-
ext_siz = next( source )
1268-
source.push( ext_siz )
1269-
source.push( sts )
1270-
source.push( pad )
1271-
source.push( svc )
1272-
log.isEnabledFor( logging.DETAIL ) and log.detail(
1273-
"{} -- found an Unconnected Send response error status 0x{:02x}: {}".format(
1274-
self, sts, enip_format( data ))
1275-
)
1276-
return sts < 0x10 and ext_siz == 0
1252+
if data[path+'..length'] <= 6:
1253+
# Might be a Unconnected Send error, perhaps with a remaining path size; peek at the
1254+
# error code and extended status; if the code is < 0x10 and there is NO extended status,
1255+
# then it must *not* be a Read Tag Fragmented error code, as ALL of its <0x10 status
1256+
# codes are required to have an extended status (which may be 0x0000, but must be
1257+
# there). HOWEVER, there are many (many) devices that DO NOT comply with the subtleties
1258+
# of the ODVA EtherNet/IP CIP spec -- and simply return eg. a status = 0x05 WITHOUT a
1259+
# 0x0000 extended status code. This makes it *impossible* to determine whether or not a
1260+
# Read Tag Fragmented response status belongs to the encapsulating Unconnected Send, or
1261+
# not. The only possible option is for the CLIENT to avoid sending Read Tag Fragmented
1262+
# requests in an Unconnected Send, or use a Multiple Service packet to encapsulate them.
1263+
svc = next( source )
1264+
pad = next( source )
1265+
sts = next( source )
1266+
ext_siz = next( source )
1267+
source.push( ext_siz )
1268+
source.push( sts )
1269+
source.push( pad )
1270+
source.push( svc )
1271+
log.isEnabledFor( logging.DETAIL ) and log.detail(
1272+
"{} -- found an Unconnected Send response error status {!r} ({}, next {!r}): {}".format(
1273+
self, sts, source, source.peek(), enip_format( data ) )
1274+
)
1275+
if sts < b'\x10'[0] and ext_siz == b'\x00'[0]: # Python 2/3 compatible b'..' comparisons
1276+
return True
12771277

1278-
othr = octets( context='request', terminal=True )
1278+
othr = octets( 'usnd_req', context='request', terminal=True )
12791279
othr[True] = othr
12801280

12811281
slct[b'\x52'[0]] = usnd

0 commit comments

Comments
 (0)