@@ -318,7 +318,7 @@ class Message(MetaObject):
318318
319319 class Implementation (Packet ):
320320
321- def __init__ (self , meta , authentication , resolution , distribution , destination , payload , conversion = None , candidate = None , source = u"unknown" , packet = "" , packet_id = 0 , sign = True ):
321+ def __init__ (self , meta , authentication , resolution , distribution , destination , payload , conversion = None , candidate = None , source = u"unknown" , packet = "" , packet_id = 0 ):
322322 from .conversion import Conversion
323323 assert isinstance (meta , Message ), "META has invalid type '%s'" % type (meta )
324324 assert isinstance (authentication , meta .authentication .Implementation ), "AUTHENTICATION has invalid type '%s'" % type (authentication )
@@ -358,16 +358,24 @@ def __init__(self, meta, authentication, resolution, distribution, destination,
358358 else :
359359 self ._conversion = meta .community .get_conversion_for_message (self )
360360
361- if not packet :
362- self ._packet = self ._conversion .encode_message (self , sign = sign )
361+ def initialize_packet (self , sign ):
362+ """
363+ Must be called if packet was None in the constructor.
364+ Args:
365+ sign: The verify sign for the packet.
366+
367+ """
368+ self ._packet = self ._conversion .encode_message (self , sign = sign )
369+
370+ if __debug__ : # attempt to decode the message when running in debug
371+ try :
372+ self ._conversion .decode_message (LoopbackCandidate (), self ._packet , verify = sign ,
373+ allow_empty_signature = True )
374+ except DropPacket :
375+ from binascii import hexlify
376+ self ._logger .error ("Could not decode message created by me, hex '%s'" , hexlify (self ._packet ))
377+ raise
363378
364- if __debug__ : # attempt to decode the message when running in debug
365- try :
366- self ._conversion .decode_message (LoopbackCandidate (), self ._packet , verify = sign , allow_empty_signature = True )
367- except DropPacket :
368- from binascii import hexlify
369- self ._logger .error ("Could not decode message created by me, hex '%s'" , hexlify (self ._packet ))
370- raise
371379
372380 @property
373381 def conversion (self ):
@@ -522,8 +530,11 @@ def impl(self, authentication=(), resolution=(), distribution=(), destination=()
522530 distribution_impl = self ._distribution .Implementation (self ._distribution , * distribution )
523531 destination_impl = self ._destination .Implementation (self ._destination , * destination )
524532 payload_impl = self ._payload .Implementation (self ._payload , * payload )
525- return self .Implementation (self , authentication_impl , resolution_impl , distribution_impl , destination_impl , payload_impl , * args , ** kargs )
526-
533+ impl = self .Implementation (self , authentication_impl , resolution_impl , distribution_impl , destination_impl , payload_impl , * args , ** kargs )
534+ if not "packet" in kargs :
535+ sign = kargs ["sign" ] if "sign" in kargs else True
536+ impl .initialize_packet (sign )
537+ return impl
527538 except (TypeError , DropPacket ):
528539 self ._logger .error ("message name: %s" , self ._name )
529540 self ._logger .error ("authentication: %s.Implementation" , self ._authentication .__class__ .__name__ )
0 commit comments