def __convert_args(args, shell, cmd_encoding): if isinstance(args, six.string_types): # input as string if not SHOULD_NOT_ENCODE_ARGS: args = to_bytes(args, cmd_encoding) if SHOULD_NOT_USE_BYTES: args = [args] else: # input as list if shell and sys.platform != 'win32': args = [to_bytes(subprocess.list2cmdline(args), cmd_encoding)] else: if not SHOULD_NOT_ENCODE_ARGS: args = [to_bytes(a, cmd_encoding) for a in args] return args
def to_hash_string(self): # ignore work directory data = b''.join( to_bytes(s) for s in [self.cmd, sorted(self.meta.env.items())]) return hashlib.md5(data).hexdigest()
def print_safe(str_or_bytes, encoding='utf-8', errors='ignore', output=sys.stdout, newline='\n'): """ Print unicode or bytes universally. :param str_or_bytes: string :param encoding: encoding :param output: output file handler :param errors: error handling scheme. Refer to codecs.register_error. """ writer = output.buffer if hasattr(output, 'buffer') else output # When the input type is bytes, verify it can be decoded with the specified encoding. decoded = str_or_bytes if is_unicode(str_or_bytes) else to_unicode(str_or_bytes, encoding, errors) encoded = to_bytes(decoded, encoding, errors) writer.write(encoded + to_bytes(newline, encoding, errors)) output.flush()
def print_safe(str_or_bytes, encoding='utf-8', errors='ignore', output=sys.stdout, newline='\n'): """ Print unicode or bytes universally. :param str_or_bytes: string :param encoding: encoding :param output: output file handler :param errors: error handling scheme. Refer to codecs.register_error. """ writer = output.buffer if hasattr(output, 'buffer') else output # When the input type is bytes, verify it can be decoded with the specified encoding. decoded = str_or_bytes if is_unicode(str_or_bytes) else to_unicode( str_or_bytes, encoding, errors) encoded = to_bytes(decoded, encoding, errors) writer.write(encoded + to_bytes(newline, encoding, errors)) output.flush()
def write(self, s, encoding='utf-8', errors='strict'): six.BytesIO.write(self, to_bytes(s, encoding, errors))
def writelines(self, lines, encoding='utf-8', errors='strict'): self._buffer += b''.join(to_bytes(s, encoding, errors) for s in lines)
def write(self, s, encoding='utf-8', errors='strict'): self._buffer += to_bytes(s, encoding, errors)
def to_hash_string(self): # ignore work directory data = b''.join(to_bytes(s) for s in [self.cmd, sorted(self.meta.env.items())]) return hashlib.md5(data).hexdigest()
def test_to_bytes(self): self.assertEqual(string.to_bytes(b'abc'), b'abc') self.assertEqual(string.to_bytes('あいう'), 'あいう'.encode('utf-8')) self.assertEqual(string.to_bytes(1.23), b'1.23')