Typing: *args to _getencoder, _getdecoder, tobytes, frombytes

This commit is contained in:
Eric Soroos 2018-01-03 11:38:28 +00:00
parent a0666ccb7c
commit 3697f87be1

View File

@ -443,7 +443,7 @@ def init():
# Codec factories (used by tobytes/frombytes and ImageFile.load) # Codec factories (used by tobytes/frombytes and ImageFile.load)
def _getdecoder(mode, decoder_name, args, extra=()): def _getdecoder(mode, decoder_name, args, extra=()):
# type: (Mode, Text, Optional[Tuple], Tuple) -> PyDecoder # type: (Mode, Text, Optional[Union[str, Tuple, List]], Tuple) -> PyDecoder
# tweak arguments # tweak arguments
if args is None: if args is None:
@ -466,7 +466,7 @@ def _getdecoder(mode, decoder_name, args, extra=()):
def _getencoder(mode, encoder_name, args, extra=()): def _getencoder(mode, encoder_name, args, extra=()):
# type: (Mode, Text, Optional[Tuple], Tuple) -> PyEncoder # type: (Mode, Text, Optional[Union[str, Tuple, List]], Tuple) -> PyEncoder
# tweak arguments # tweak arguments
if args is None: if args is None:
@ -742,21 +742,23 @@ class Image(object):
:param encoder_name: What encoder to use. The default is to :param encoder_name: What encoder to use. The default is to
use the standard "raw" encoder. use the standard "raw" encoder.
:param args: Extra arguments to the encoder. :param *args: Extra arguments to the encoder. May be one tuple,
or individual arguments
:rtype: A bytes object. :rtype: A bytes object.
""" """
encoder_args = args # type: Union[str, Tuple[Any,...], List[Any]]
# may pass tuple instead of argument list # may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple): if len(args) == 1 and isinstance(args[0], tuple):
args = args[0] encoder_args = args[0]
if encoder_name == "raw" and args == (): if encoder_name == "raw" and args == ():
args = self.mode encoder_args = self.mode
self.load() self.load()
# unpack data # unpack data
e = _getencoder(self.mode, encoder_name, args) e = _getencoder(self.mode, encoder_name, encoder_args)
e.setimage(self.im) e.setimage(self.im)
bufsize = max(65536, self.size[0] * 4) # see RawEncode.c bufsize = max(65536, self.size[0] * 4) # see RawEncode.c
@ -808,16 +810,17 @@ class Image(object):
but loads data into this image instead of creating a new image object. but loads data into this image instead of creating a new image object.
""" """
decoder_args = args # type: Union[str, Tuple[Any,...], List[Any]]
# may pass tuple instead of argument list # may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple): if len(args) == 1 and isinstance(args[0], tuple):
args = args[0] decoder_args = args[0]
# default format # default format
if decoder_name == "raw" and args == (): if decoder_name == "raw" and args == ():
args = self.mode decoder_args = self.mode
# unpack data # unpack data
d = _getdecoder(self.mode, decoder_name, args) d = _getdecoder(self.mode, decoder_name, decoder_args)
d.setimage(self.im) d.setimage(self.im)
s = d.decode(data) s = d.decode(data)