def pack(self):
fields = self._all_fields()<EOL>ctx = PackContext(self, fields)<EOL>for field in fields:<EOL><INDENT>if field.pack_if.deref(ctx):<EOL><INDENT>try:<EOL><INDENT>ctx.output_buffer.set(field.pack_ref.deref(ctx), field.pack_absolute_position_ref.deref(ctx))<EOL><DEDENT>except:<EOL><INDENT>raise chain_exceptions(InstructBufferError("<STR_LIT>", ctx, type(self),<EOL>field.attr_name()))<EOL><DEDENT><DEDENT><DEDENT>result = bytearray(ctx.output_buffer.get())<EOL>static_byte_size = type(self).byte_size<EOL>if static_byte_size:<EOL><INDENT>static_byte_size = int(math.ceil(static_byte_size))<EOL>assert len(result) <= static_byte_size,("<STR_LIT>" +<EOL>"<STR_LIT>").format(type(self), len(result),<EOL>static_byte_size)<EOL>if len(result) < static_byte_size:<EOL><INDENT>result += bytearray(static_byte_size - len(result))<EOL><DEDENT><DEDENT>return result<EOL>
Packs the object and returns a buffer representing the packed object.