@staticmethod<EOL><INDENT>def _encode_files(files, data):<DEDENT>
if (not files):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>elif isinstance(data, basestring):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>new_fields = []<EOL>fields = to_key_val_list(data or {})<EOL>files = to_key_val_list(files or {})<EOL>for field, val in fields:<EOL><INDENT>if isinstance(val, basestring) or not hasattr(val, '<STR_LIT>'):<EOL><INDENT>val = [val]<EOL><DEDENT>for v in val:<EOL><INDENT>if v is not None:<EOL><INDENT>if not isinstance(v, bytes):<EOL><INDENT>v = str(v)<EOL><DEDENT>new_fields.append(<EOL>(field.decode('<STR_LIT:utf-8>') if isinstance(field, bytes) else field,<EOL>v.encode('<STR_LIT:utf-8>') if isinstance(v, str) else v))<EOL><DEDENT><DEDENT><DEDENT>for (k, v) in files:<EOL><INDENT>ft = None<EOL>fh = None<EOL>if isinstance(v, (tuple, list)):<EOL><INDENT>if len(v) == <NUM_LIT:2>:<EOL><INDENT>fn, fp = v<EOL><DEDENT>elif len(v) == <NUM_LIT:3>:<EOL><INDENT>fn, fp, ft = v<EOL><DEDENT>else:<EOL><INDENT>fn, fp, ft, fh = v<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fn = guess_filename(v) or k<EOL>fp = v<EOL><DEDENT>if isinstance(fp, str):<EOL><INDENT>fp = StringIO(fp)<EOL><DEDENT>if isinstance(fp, bytes):<EOL><INDENT>fp = BytesIO(fp)<EOL><DEDENT>rf = RequestField(name=k, data=fp.read(),<EOL>filename=fn, headers=fh)<EOL>rf.make_multipart(content_type=ft)<EOL>new_fields.append(rf)<EOL><DEDENT>body, content_type = encode_multipart_formdata(new_fields)<EOL>return body, content_type<EOL>
Build the body for a multipart/form-data request.

        Will successfully encode files when passed as a dict or a list of
        2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
        if parameters are supplied as a dict.