MCPcopy Index your code
hub / github.com/aws/aws-cli / determine_content_length

Function determine_content_length

awscli/botocore/utils.py:3036–3062  ·  view source on GitHub ↗
(body)

Source from the content-addressed store, hash-verified

3034
3035
3036def determine_content_length(body):
3037 # No body, content length of 0
3038 if not body:
3039 return 0
3040
3041 # Try asking the body for it's length
3042 try:
3043 return len(body)
3044 except (AttributeError, TypeError):
3045 pass
3046
3047 # Try getting the length from a seekable stream
3048 if hasattr(body, 'seek') and hasattr(body, 'tell'):
3049 try:
3050 orig_pos = body.tell()
3051 body.seek(0, 2)
3052 end_file_pos = body.tell()
3053 body.seek(orig_pos)
3054 return end_file_pos - orig_pos
3055 except io.UnsupportedOperation:
3056 # in case when body is, for example, io.BufferedIOBase object
3057 # it has "seek" method which throws "UnsupportedOperation"
3058 # exception in such case we want to fall back to "chunked"
3059 # encoding
3060 pass
3061 # Failed to determine the length
3062 return None
3063
3064
3065def get_encoding_from_headers(headers, default='ISO-8859-1'):

Callers 9

test_basic_bytesMethod · 0.90
test_empty_bytesMethod · 0.90
test_buffered_io_baseMethod · 0.90
test_noneMethod · 0.90
test_basic_len_objMethod · 0.90
test_seekable_fileobjMethod · 0.90
_get_body_sizeFunction · 0.90

Calls 2

tellMethod · 0.45
seekMethod · 0.45

Tested by 7

test_basic_bytesMethod · 0.72
test_empty_bytesMethod · 0.72
test_buffered_io_baseMethod · 0.72
test_noneMethod · 0.72
test_basic_len_objMethod · 0.72
test_seekable_fileobjMethod · 0.72