MCPcopy
hub / github.com/redis/redis-py / pack_command

Method pack_command

redis/asyncio/connection.py:827–871  ·  view source on GitHub ↗

Pack a series of arguments into the Redis protocol

(self, *args: EncodableT)

Source from the content-addressed store, hash-verified

825 return response
826
827 def pack_command(self, *args: EncodableT) -> List[bytes]:
828 """Pack a series of arguments into the Redis protocol"""
829 output = []
830 # the client might have included 1 or more literal arguments in
831 # the command name, e.g., 'CONFIG GET'. The Redis server expects these
832 # arguments to be sent separately, so split the first argument
833 # manually. These arguments should be bytestrings so that they are
834 # not encoded.
835 assert not isinstance(args[0], float)
836 if isinstance(args[0], str):
837 args = tuple(args[0].encode().split()) + args[1:]
838 elif b" " in args[0]:
839 args = tuple(args[0].split()) + args[1:]
840
841 buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
842
843 buffer_cutoff = self._buffer_cutoff
844 for arg in map(self.encoder.encode, args):
845 # to avoid large string mallocs, chunk the command into the
846 # output list if we're sending large values or memoryviews
847 arg_length = len(arg)
848 if (
849 len(buff) > buffer_cutoff
850 or arg_length > buffer_cutoff
851 or isinstance(arg, memoryview)
852 ):
853 buff = SYM_EMPTY.join(
854 (buff, SYM_DOLLAR, str(arg_length).encode(), SYM_CRLF)
855 )
856 output.append(buff)
857 output.append(arg)
858 buff = SYM_CRLF
859 else:
860 buff = SYM_EMPTY.join(
861 (
862 buff,
863 SYM_DOLLAR,
864 str(arg_length).encode(),
865 SYM_CRLF,
866 arg,
867 SYM_CRLF,
868 )
869 )
870 output.append(buff)
871 return output
872
873 def pack_commands(self, commands: Iterable[Iterable[EncodableT]]) -> List[bytes]:
874 """Pack multiple commands into the Redis protocol"""

Callers 3

send_commandMethod · 0.95
pack_commandsMethod · 0.95
execute_commandMethod · 0.45

Calls 2

encodeMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected